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 {
|
|
|
|
AlicloudImageForceDetele bool
|
|
|
|
AlicloudImageForceDeteleSnapshots bool
|
|
|
|
AlicloudImageName string
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepDeleteAlicloudImageSnapshots) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-03-03 03:56:17 -05:00
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
config := state.Get("config").(Config)
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say("Deleting image snapshots.")
|
2017-03-03 03:56:17 -05:00
|
|
|
// Check for force delete
|
|
|
|
if s.AlicloudImageForceDetele {
|
|
|
|
images, _, err := client.DescribeImages(&ecs.DescribeImagesArgs{
|
|
|
|
RegionId: common.Region(config.AlicloudRegion),
|
|
|
|
ImageName: s.AlicloudImageName,
|
|
|
|
})
|
|
|
|
if len(images) < 1 {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
for _, image := range images {
|
|
|
|
if image.ImageOwnerAlias != string(ecs.ImageOwnerSelf) {
|
2017-06-01 22:33:12 -04:00
|
|
|
log.Printf("You can only delete instances based on customized images %s ", image.ImageId)
|
2017-03-03 03:56:17 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = client.DeleteImage(common.Region(config.AlicloudRegion), image.ImageId)
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
err := fmt.Errorf("Failed to delete image: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
if s.AlicloudImageForceDeteleSnapshots {
|
|
|
|
for _, diskDevice := range image.DiskDeviceMappings.DiskDeviceMapping {
|
|
|
|
if err := client.DeleteSnapshot(diskDevice.SnapshotId); err != nil {
|
2017-03-04 05:06:32 -05:00
|
|
|
err := fmt.Errorf("Deleting ECS snapshot failed: %s", err)
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepDeleteAlicloudImageSnapshots) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|