2015-06-12 14:05:15 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-12 14:05:15 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepDeregisterAMI struct {
|
2017-08-14 12:20:08 -04:00
|
|
|
AccessConfig *AccessConfig
|
2016-11-30 16:28:34 -05:00
|
|
|
ForceDeregister bool
|
|
|
|
ForceDeleteSnapshot bool
|
|
|
|
AMIName string
|
2017-08-14 12:20:08 -04:00
|
|
|
Regions []string
|
2015-06-12 14:05:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepDeregisterAMI) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-08-14 12:20:08 -04:00
|
|
|
regions := s.Regions
|
|
|
|
if len(regions) == 0 {
|
|
|
|
regions = append(regions, s.AccessConfig.RawRegion)
|
|
|
|
}
|
2015-06-12 14:05:15 -04:00
|
|
|
|
2016-11-30 16:28:34 -05:00
|
|
|
// Check for force deregister
|
2015-06-12 14:05:15 -04:00
|
|
|
if s.ForceDeregister {
|
2017-08-14 12:20:08 -04:00
|
|
|
for _, region := range regions {
|
|
|
|
// get new connection for each region in which we need to deregister vms
|
|
|
|
session, err := s.AccessConfig.Session()
|
|
|
|
if err != nil {
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-06-12 14:05:15 -04:00
|
|
|
|
2017-08-14 12:20:08 -04:00
|
|
|
regionconn := ec2.New(session.Copy(&aws.Config{
|
|
|
|
Region: aws.String(region)},
|
|
|
|
))
|
2015-06-12 14:05:15 -04:00
|
|
|
|
2017-08-14 12:20:08 -04:00
|
|
|
resp, err := regionconn.DescribeImages(&ec2.DescribeImagesInput{
|
|
|
|
Filters: []*ec2.Filter{{
|
|
|
|
Name: aws.String("name"),
|
|
|
|
Values: []*string{aws.String(s.AMIName)},
|
|
|
|
}}})
|
2015-06-12 14:05:15 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2017-08-14 12:20:08 -04:00
|
|
|
err := fmt.Errorf("Error describing AMI: %s", err)
|
2015-06-12 14:05:15 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2016-11-30 16:28:34 -05:00
|
|
|
|
2017-08-14 12:20:08 -04:00
|
|
|
// Deregister image(s) by name
|
|
|
|
for _, i := range resp.Images {
|
|
|
|
_, err := regionconn.DeregisterImage(&ec2.DeregisterImageInput{
|
|
|
|
ImageId: i.ImageId,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error deregistering existing AMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
ui.Say(fmt.Sprintf("Deregistered AMI %s, id: %s", s.AMIName, *i.ImageId))
|
|
|
|
|
|
|
|
// Delete snapshot(s) by image
|
|
|
|
if s.ForceDeleteSnapshot {
|
|
|
|
for _, b := range i.BlockDeviceMappings {
|
|
|
|
if b.Ebs != nil && aws.StringValue(b.Ebs.SnapshotId) != "" {
|
|
|
|
_, err := regionconn.DeleteSnapshot(&ec2.DeleteSnapshotInput{
|
|
|
|
SnapshotId: b.Ebs.SnapshotId,
|
|
|
|
})
|
2016-11-30 16:28:34 -05:00
|
|
|
|
2017-08-14 12:20:08 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error deleting existing snapshot: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
ui.Say(fmt.Sprintf("Deleted snapshot: %s", *b.Ebs.SnapshotId))
|
2016-11-30 16:28:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-12 14:05:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepDeregisterAMI) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|