From a587bd47a18789ab57a5200e063c2a025b109ac5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 6 Sep 2014 10:12:33 -0700 Subject: [PATCH] builder/amazon/ebs: clean up AMI in error [GH-1186] --- CHANGELOG.md | 1 + builder/amazon/ebs/step_create_ami.go | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f3d814e4..c5ecbc4d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ BUG FIXES: * builder/amazon-chroot: Fix crash in root device check. [GH-1360] * builder/amazon-chroot: Add description that Packer made the snapshot with a time. [GH-1388] + * builder/amazon-ebs: AMI is deregistered if an error. [GH-1186] * builder/amazon-instance: Fix deprecation warning for `ec2-bundle-vol` [GH-1424] * builder/amazon-instance: Add `--no-filter` to the `ec2-bundle-vol` diff --git a/builder/amazon/ebs/step_create_ami.go b/builder/amazon/ebs/step_create_ami.go index dd14c9621..7dda9e32b 100644 --- a/builder/amazon/ebs/step_create_ami.go +++ b/builder/amazon/ebs/step_create_ami.go @@ -69,14 +69,17 @@ func (s *stepCreateAMI) Run(state multistep.StateBag) multistep.StepAction { } func (s *stepCreateAMI) Cleanup(state multistep.StateBag) { - if s.image == nil || s.image.State == "available" { + _, cancelled := state.GetOk(multistep.StateCancelled) + _, halted := state.GetOk(multistep.StateHalted) + + if !cancelled && !halted { return } ec2conn := state.Get("ec2").(*ec2.EC2) ui := state.Get("ui").(packer.Ui) - ui.Say("Deregistering the AMI ...") + ui.Say("Deregistering the AMI because cancelation or error...") if resp, err := ec2conn.DeregisterImage(s.image.Id); err != nil { ui.Error(fmt.Sprintf("Error deregistering AMI, may still be around: %s", err)) return