2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-06-01 20:14:51 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Artifact struct {
|
|
|
|
// A map of regions to alicloud image IDs.
|
|
|
|
AlicloudImages map[string]string
|
|
|
|
|
|
|
|
// BuilderId is the unique ID for the builder that created this alicloud image
|
|
|
|
BuilderIdValue string
|
|
|
|
|
|
|
|
// Alcloud connection for performing API stuff.
|
2019-04-25 22:37:49 -04:00
|
|
|
Client *ClientWrapper
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) BuilderId() string {
|
|
|
|
return a.BuilderIdValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Artifact) Files() []string {
|
|
|
|
// We have no files
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) Id() string {
|
|
|
|
parts := make([]string, 0, len(a.AlicloudImages))
|
|
|
|
for region, ecsImageId := range a.AlicloudImages {
|
|
|
|
parts = append(parts, fmt.Sprintf("%s:%s", region, ecsImageId))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(parts)
|
|
|
|
return strings.Join(parts, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) String() string {
|
|
|
|
alicloudImageStrings := make([]string, 0, len(a.AlicloudImages))
|
|
|
|
for region, id := range a.AlicloudImages {
|
|
|
|
single := fmt.Sprintf("%s: %s", region, id)
|
|
|
|
alicloudImageStrings = append(alicloudImageStrings, single)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(alicloudImageStrings)
|
|
|
|
return fmt.Sprintf("Alicloud images were created:\n\n%s", strings.Join(alicloudImageStrings, "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) State(name string) interface{} {
|
|
|
|
switch name {
|
|
|
|
case "atlas.artifact.metadata":
|
|
|
|
return a.stateAtlasMetadata()
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) Destroy() error {
|
|
|
|
errors := make([]error, 0)
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
copyingImages := make(map[string]string, len(a.AlicloudImages))
|
|
|
|
sourceImage := make(map[string]*ecs.Image, 1)
|
|
|
|
for regionId, imageId := range a.AlicloudImages {
|
|
|
|
describeImagesRequest := ecs.CreateDescribeImagesRequest()
|
|
|
|
describeImagesRequest.RegionId = regionId
|
|
|
|
describeImagesRequest.ImageId = imageId
|
|
|
|
describeImagesRequest.Status = ImageStatusQueried
|
|
|
|
imagesResponse, err := a.Client.DescribeImages(describeImagesRequest)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
images := imagesResponse.Images.Image
|
2017-03-03 03:56:17 -05:00
|
|
|
if len(images) == 0 {
|
|
|
|
err := fmt.Errorf("Error retrieving details for alicloud image(%s), no alicloud images found", imageId)
|
|
|
|
errors = append(errors, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2019-04-26 03:12:07 -04:00
|
|
|
if images[0].IsCopied && images[0].Status != ImageStatusAvailable {
|
2019-04-25 22:37:49 -04:00
|
|
|
copyingImages[regionId] = imageId
|
|
|
|
} else {
|
|
|
|
sourceImage[regionId] = &images[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for regionId, imageId := range copyingImages {
|
|
|
|
log.Printf("Cancel copying alicloud image (%s) from region (%s)", imageId, regionId)
|
|
|
|
|
|
|
|
errs := a.unsharedAccountsOnImages(regionId, imageId)
|
|
|
|
if errs != nil {
|
|
|
|
errors = append(errors, errs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelImageCopyRequest := ecs.CreateCancelCopyImageRequest()
|
|
|
|
cancelImageCopyRequest.RegionId = regionId
|
|
|
|
cancelImageCopyRequest.ImageId = imageId
|
|
|
|
if _, err := a.Client.CancelCopyImage(cancelImageCopyRequest); err != nil {
|
2017-03-03 03:56:17 -05:00
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
}
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
for regionId, image := range sourceImage {
|
|
|
|
imageId := image.ImageId
|
|
|
|
log.Printf("Delete alicloud image (%s) from region (%s)", imageId, regionId)
|
|
|
|
|
|
|
|
errs := a.unsharedAccountsOnImages(regionId, imageId)
|
|
|
|
if errs != nil {
|
|
|
|
errors = append(errors, errs...)
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
deleteImageRequest := ecs.CreateDeleteImageRequest()
|
|
|
|
deleteImageRequest.RegionId = regionId
|
|
|
|
deleteImageRequest.ImageId = imageId
|
|
|
|
if _, err := a.Client.DeleteImage(deleteImageRequest); err != nil {
|
2017-03-03 03:56:17 -05:00
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
//Delete the snapshot of this images
|
2019-04-25 22:37:49 -04:00
|
|
|
for _, diskDevices := range image.DiskDeviceMappings.DiskDeviceMapping {
|
|
|
|
deleteSnapshotRequest := ecs.CreateDeleteSnapshotRequest()
|
|
|
|
deleteSnapshotRequest.SnapshotId = diskDevices.SnapshotId
|
|
|
|
_, err := a.Client.DeleteSnapshot(deleteSnapshotRequest)
|
|
|
|
if err != nil {
|
2017-03-03 03:56:17 -05:00
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
if len(errors) == 1 {
|
|
|
|
return errors[0]
|
|
|
|
} else {
|
2020-11-19 15:07:02 -05:00
|
|
|
return &packersdk.MultiError{Errors: errors}
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
func (a *Artifact) unsharedAccountsOnImages(regionId string, imageId string) []error {
|
|
|
|
var errors []error
|
|
|
|
|
|
|
|
describeImageShareRequest := ecs.CreateDescribeImageSharePermissionRequest()
|
|
|
|
describeImageShareRequest.RegionId = regionId
|
|
|
|
describeImageShareRequest.ImageId = imageId
|
|
|
|
imageShareResponse, err := a.Client.DescribeImageSharePermission(describeImageShareRequest)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
|
|
|
accountsNumber := len(imageShareResponse.Accounts.Account)
|
|
|
|
if accountsNumber > 0 {
|
|
|
|
accounts := make([]string, accountsNumber)
|
|
|
|
for index, account := range imageShareResponse.Accounts.Account {
|
|
|
|
accounts[index] = account.AliyunId
|
|
|
|
}
|
|
|
|
|
|
|
|
modifyImageShareRequest := ecs.CreateModifyImageSharePermissionRequest()
|
|
|
|
modifyImageShareRequest.RegionId = regionId
|
|
|
|
modifyImageShareRequest.ImageId = imageId
|
|
|
|
modifyImageShareRequest.RemoveAccount = &accounts
|
|
|
|
_, err := a.Client.ModifyImageSharePermission(modifyImageShareRequest)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
func (a *Artifact) stateAtlasMetadata() interface{} {
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
for region, imageId := range a.AlicloudImages {
|
|
|
|
k := fmt.Sprintf("region.%s", region)
|
|
|
|
metadata[k] = imageId
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata
|
|
|
|
}
|