2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2017-05-25 21:49:35 -04:00
|
|
|
"log"
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepDeleteAlicloudImageSnapshots struct {
|
2018-03-13 03:36:38 -04:00
|
|
|
AlicloudImageForceDelete bool
|
|
|
|
AlicloudImageForceDeleteSnapshots bool
|
2017-03-03 03:56:17 -05:00
|
|
|
AlicloudImageName string
|
2018-11-28 08:34:21 -05:00
|
|
|
AlicloudImageDestinationRegions []string
|
|
|
|
AlicloudImageDestinationNames []string
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepDeleteAlicloudImageSnapshots) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2018-09-25 07:21:28 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
// Check for force delete
|
2018-03-13 03:36:38 -04:00
|
|
|
if s.AlicloudImageForceDelete {
|
2018-11-28 08:34:21 -05:00
|
|
|
err := s.deleteImageAndSnapshots(state, s.AlicloudImageName, config.AlicloudRegion)
|
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, "")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2018-09-25 07:21:28 -04:00
|
|
|
|
2018-11-28 08:34:21 -05:00
|
|
|
numberOfName := len(s.AlicloudImageDestinationNames)
|
|
|
|
if numberOfName == 0 {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2018-09-25 07:21:28 -04:00
|
|
|
|
2018-11-28 08:34:21 -05:00
|
|
|
for index, destinationRegion := range s.AlicloudImageDestinationRegions {
|
|
|
|
if destinationRegion == config.AlicloudRegion {
|
2017-03-03 03:56:17 -05:00
|
|
|
continue
|
|
|
|
}
|
2018-11-28 08:34:21 -05:00
|
|
|
|
|
|
|
if index < numberOfName {
|
|
|
|
err = s.deleteImageAndSnapshots(state, s.AlicloudImageDestinationNames[index], destinationRegion)
|
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, "")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2018-11-28 08:34:21 -05:00
|
|
|
} else {
|
|
|
|
break
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-11-28 08:34:21 -05:00
|
|
|
func (s *stepDeleteAlicloudImageSnapshots) deleteImageAndSnapshots(state multistep.StateBag, imageName string, region string) error {
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{
|
|
|
|
RegionId: common.Region(region),
|
|
|
|
ImageName: imageName,
|
|
|
|
})
|
|
|
|
if len(images) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Deleting duplicated image and snapshot in %s: %s", region, imageName))
|
|
|
|
|
|
|
|
for _, image := range images {
|
|
|
|
if image.ImageOwnerAlias != string(ecs.ImageOwnerSelf) {
|
|
|
|
log.Printf("You can not delete non-customized images: %s ", image.ImageId)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.DeleteImage(common.Region(region), image.ImageId)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Failed to delete image: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.AlicloudImageForceDeleteSnapshots {
|
|
|
|
for _, diskDevice := range image.DiskDeviceMappings.DiskDeviceMapping {
|
|
|
|
if err := client.DeleteSnapshot(diskDevice.SnapshotId); err != nil {
|
|
|
|
err := fmt.Errorf("Deleting ECS snapshot failed: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2018-11-28 08:34:21 -05:00
|
|
|
|
|
|
|
func (s *stepDeleteAlicloudImageSnapshots) Cleanup(state multistep.StateBag) {
|
2018-12-04 20:45:37 -05:00
|
|
|
}
|