2018-07-26 02:30:51 -04:00
|
|
|
package common
|
2015-06-18 14:23:48 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-18 14:23:48 -04:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-18 14:23:48 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// stepCleanupVolumes cleans up any orphaned volumes that were not designated to
|
|
|
|
// remain after termination of the instance. These volumes are typically ones
|
|
|
|
// that are marked as "delete on terminate:false" in the source_ami of a build.
|
2018-07-26 02:30:51 -04:00
|
|
|
type StepCleanupVolumes struct {
|
|
|
|
BlockDevices BlockDevices
|
2015-06-18 14:23:48 -04:00
|
|
|
}
|
|
|
|
|
2018-07-26 02:30:51 -04:00
|
|
|
func (s *StepCleanupVolumes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-18 14:23:48 -04:00
|
|
|
// stepCleanupVolumes is for Cleanup only
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-07-26 02:30:51 -04:00
|
|
|
func (s *StepCleanupVolumes) Cleanup(state multistep.StateBag) {
|
2015-06-18 14:23:48 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
instanceRaw := state.Get("instance")
|
|
|
|
var instance *ec2.Instance
|
|
|
|
if instanceRaw != nil {
|
|
|
|
instance = instanceRaw.(*ec2.Instance)
|
|
|
|
}
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
if instance == nil {
|
|
|
|
ui.Say("No volumes to clean up, skipping")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Cleaning up any extra volumes...")
|
|
|
|
|
|
|
|
// Collect Volume information from the cached Instance as a map of volume-id
|
2017-05-25 16:48:32 -04:00
|
|
|
// to device name, to compare with save list below
|
2015-06-18 14:23:48 -04:00
|
|
|
var vl []*string
|
|
|
|
volList := make(map[string]string)
|
|
|
|
for _, bdm := range instance.BlockDeviceMappings {
|
2015-08-17 20:44:01 -04:00
|
|
|
if bdm.Ebs != nil {
|
|
|
|
vl = append(vl, bdm.Ebs.VolumeId)
|
|
|
|
volList[*bdm.Ebs.VolumeId] = *bdm.DeviceName
|
2015-06-18 14:23:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using the volume list from the cached Instance, check with AWS for up to
|
|
|
|
// date information on them
|
|
|
|
resp, err := ec2conn.DescribeVolumes(&ec2.DescribeVolumesInput{
|
|
|
|
Filters: []*ec2.Filter{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2015-06-18 14:23:48 -04:00
|
|
|
Name: aws.String("volume-id"),
|
|
|
|
Values: vl,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Error describing volumes: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any of the returned volumes are in a "deleting" stage or otherwise not
|
|
|
|
// available, remove them from the list of volumes
|
|
|
|
for _, v := range resp.Volumes {
|
|
|
|
if v.State != nil && *v.State != "available" {
|
2015-08-17 20:44:01 -04:00
|
|
|
delete(volList, *v.VolumeId)
|
2015-06-18 14:23:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Volumes) == 0 {
|
|
|
|
ui.Say("No volumes to clean up, skipping")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-25 16:48:32 -04:00
|
|
|
// Filter out any devices created as part of the launch mappings, since
|
|
|
|
// we'll let amazon follow the `delete_on_termination` setting.
|
|
|
|
for _, b := range s.BlockDevices.LaunchMappings {
|
2015-06-18 14:23:48 -04:00
|
|
|
for volKey, volName := range volList {
|
2017-05-25 16:48:32 -04:00
|
|
|
if volName == b.DeviceName {
|
2015-06-18 14:23:48 -04:00
|
|
|
delete(volList, volKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy remaining volumes
|
2016-11-01 17:08:04 -04:00
|
|
|
for k := range volList {
|
2015-06-18 14:23:48 -04:00
|
|
|
ui.Say(fmt.Sprintf("Destroying volume (%s)...", k))
|
2015-08-17 20:44:01 -04:00
|
|
|
_, err := ec2conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeId: aws.String(k)})
|
2015-06-18 14:23:48 -04:00
|
|
|
if err != nil {
|
2017-01-13 14:42:20 -05:00
|
|
|
ui.Say(fmt.Sprintf("Error deleting volume: %s", err))
|
2015-06-18 14:23:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|