Merge pull request #1186 from henrysher/ami-cleanup
Add the cleanup when the process of EBS AMI interrupted
This commit is contained in:
commit
669f26a108
|
@ -8,7 +8,9 @@ import (
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stepCreateAMI struct{}
|
type stepCreateAMI struct {
|
||||||
|
image *ec2.Image
|
||||||
|
}
|
||||||
|
|
||||||
func (s *stepCreateAMI) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *stepCreateAMI) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
config := state.Get("config").(config)
|
config := state.Get("config").(config)
|
||||||
|
@ -54,9 +56,32 @@ func (s *stepCreateAMI) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
imagesResp, err := ec2conn.Images([]string{createResp.ImageId}, nil)
|
||||||
|
if err != nil {
|
||||||
|
err := fmt.Errorf("Error searching for AMI: %s", err)
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
s.image = &imagesResp.Images[0]
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepCreateAMI) Cleanup(multistep.StateBag) {
|
func (s *stepCreateAMI) Cleanup(state multistep.StateBag) {
|
||||||
// No cleanup...
|
if s.image == nil || s.image.State == "available" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
|
ui.Say("Deregistering the AMI ...")
|
||||||
|
if resp, err := ec2conn.DeregisterImage(s.image.Id); err != nil {
|
||||||
|
ui.Error(fmt.Sprintf("Error deregistering AMI, may still be around: %s", err))
|
||||||
|
return
|
||||||
|
} else if resp.Return == false {
|
||||||
|
ui.Error(fmt.Sprintf("Error deregistering AMI, may still be around: %s", resp.Return))
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue