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 {
|
2017-10-27 12:58:13 -04:00
|
|
|
// Check for force deregister
|
|
|
|
if !s.ForceDeregister {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2015-06-12 14:05:15 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-10-27 12:58:13 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
2017-08-14 12:20:08 -04:00
|
|
|
regions := s.Regions
|
|
|
|
if len(regions) == 0 {
|
2017-10-27 12:58:13 -04:00
|
|
|
regions = append(regions, *ec2conn.Config.Region)
|
2017-08-14 12:20:08 -04:00
|
|
|
}
|
2015-06-12 14:05:15 -04:00
|
|
|
|
2017-10-27 12:58:13 -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
|
|
|
|
}
|
|
|
|
|
|
|
|
regionconn := ec2.New(session.Copy(&aws.Config{
|
|
|
|
Region: aws.String(region)},
|
|
|
|
))
|
2015-06-12 14:05:15 -04:00
|
|
|
|
2017-10-27 12:58:13 -04:00
|
|
|
resp, err := regionconn.DescribeImages(&ec2.DescribeImagesInput{
|
|
|
|
Filters: []*ec2.Filter{{
|
|
|
|
Name: aws.String("name"),
|
|
|
|
Values: []*string{aws.String(s.AMIName)},
|
|
|
|
}}})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error describing AMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-06-12 14:05:15 -04:00
|
|
|
|
2017-10-27 12:58:13 -04:00
|
|
|
// Deregister image(s) by name
|
|
|
|
for _, i := range resp.Images {
|
|
|
|
_, err := regionconn.DeregisterImage(&ec2.DeregisterImageInput{
|
|
|
|
ImageId: i.ImageId,
|
|
|
|
})
|
2015-06-12 14:05:15 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2017-10-27 12:58:13 -04:00
|
|
|
err := fmt.Errorf("Error deregistering existing AMI: %s", err)
|
2015-06-12 14:05:15 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-10-27 12:58:13 -04:00
|
|
|
ui.Say(fmt.Sprintf("Deregistered AMI %s, id: %s", s.AMIName, *i.ImageId))
|
2016-11-30 16:28:34 -05:00
|
|
|
|
2017-10-27 12:58:13 -04:00
|
|
|
// 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-10-27 12:58:13 -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
|
2016-11-30 16:28:34 -05:00
|
|
|
}
|
2017-10-27 12:58:13 -04:00
|
|
|
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) {
|
|
|
|
}
|