2015-06-12 14:05:15 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-12 14:05:15 -04:00
|
|
|
"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"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2015-06-12 14:05:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepDeregisterAMI) Run(ctx context.Context, 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)
|
2018-11-26 05:33:44 -05:00
|
|
|
// Add the session region to list of regions will deregister AMIs in
|
2017-10-30 17:17:19 -04:00
|
|
|
regions := append(s.Regions, *ec2conn.Config.Region)
|
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{
|
2019-03-04 19:22:52 -05:00
|
|
|
Region: aws.String(region),
|
2019-01-23 19:58:48 -05:00
|
|
|
}))
|
2015-06-12 14:05:15 -04:00
|
|
|
|
2017-10-27 12:58:13 -04:00
|
|
|
resp, err := regionconn.DescribeImages(&ec2.DescribeImagesInput{
|
2018-04-05 16:29:32 -04:00
|
|
|
Owners: aws.StringSlice([]string{"self"}),
|
2017-10-27 12:58:13 -04:00
|
|
|
Filters: []*ec2.Filter{{
|
|
|
|
Name: aws.String("name"),
|
2018-04-05 19:32:03 -04:00
|
|
|
Values: aws.StringSlice([]string{s.AMIName}),
|
2017-10-27 12:58:13 -04:00
|
|
|
}}})
|
|
|
|
|
|
|
|
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) {
|
|
|
|
}
|