prevent panics when cleaning up resources that haven't been created.

This commit is contained in:
Matthew Hooker 2018-04-02 11:56:11 -07:00
parent 3a058b8b67
commit 7e13b5c62a
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
4 changed files with 37 additions and 18 deletions

View File

@ -57,18 +57,22 @@ func (s *stepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu
}
func (s *stepCreateInstance) Cleanup(state multistep.StateBag) {
instanceID, ok := state.GetOk("instance_id")
if !ok {
return
}
// terminate instance
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(*compute.ComputeClient)
config := state.Get("config").(*Config)
imID := state.Get("instance_id").(string)
ui.Say("Terminating source instance...")
instanceClient := client.Instances()
input := &compute.DeleteInstanceInput{
Name: config.ImageName,
ID: imID,
ID: instanceID.(string),
}
err := instanceClient.DeleteInstance(input)

View File

@ -42,12 +42,16 @@ func (s *stepCreateIPReservation) Run(_ context.Context, state multistep.StateBa
}
func (s *stepCreateIPReservation) Cleanup(state multistep.StateBag) {
ipResName, ok := state.GetOk("ipres_name")
if !ok {
return
}
ui := state.Get("ui").(packer.Ui)
ui.Say("Cleaning up IP reservations...")
client := state.Get("client").(*compute.ComputeClient)
ipResName := state.Get("ipres_name").(string)
input := compute.DeleteIPReservationInput{Name: ipResName}
input := compute.DeleteIPReservationInput{Name: ipResName.(string)}
ipClient := client.IPReservations()
err := ipClient.DeleteIPReservation(&input)
if err != nil {

View File

@ -97,6 +97,15 @@ func (s *stepSecurity) Run(_ context.Context, state multistep.StateBag) multiste
}
func (s *stepSecurity) Cleanup(state multistep.StateBag) {
secRuleName, ok := state.GetOk("security_rule_name")
if !ok {
return
}
secListName, ok := state.GetOk("security_list")
if !ok {
return
}
client := state.Get("client").(*compute.ComputeClient)
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(*Config)
@ -105,37 +114,38 @@ func (s *stepSecurity) Cleanup(state multistep.StateBag) {
namePrefix := fmt.Sprintf("/Compute-%s/%s/", config.IdentityDomain, config.Username)
// delete security rules that Packer generated
secRuleName := state.Get("security_rule_name").(string)
secRulesClient := client.SecRules()
ruleInput := compute.DeleteSecRuleInput{Name: namePrefix + secRuleName}
ruleInput := compute.DeleteSecRuleInput{Name: namePrefix + secRuleName.(string)}
err := secRulesClient.DeleteSecRule(&ruleInput)
if err != nil {
ui.Say(fmt.Sprintf("Error deleting the packer-generated security rule %s; "+
"please delete manually. (error: %s)", secRuleName, err.Error()))
"please delete manually. (error: %s)", secRuleName.(string), err.Error()))
}
// delete security list that Packer generated
secListName := state.Get("security_list").(string)
secListClient := client.SecurityLists()
input := compute.DeleteSecurityListInput{Name: namePrefix + secListName}
input := compute.DeleteSecurityListInput{Name: namePrefix + secListName.(string)}
err = secListClient.DeleteSecurityList(&input)
if err != nil {
ui.Say(fmt.Sprintf("Error deleting the packer-generated security list %s; "+
"please delete manually. (error : %s)", secListName, err.Error()))
"please delete manually. (error : %s)", secListName.(string), err.Error()))
}
// Some extra cleanup if we used the winRM communicator
if config.Comm.Type == "winrm" {
// Delete the packer-generated application
application := state.Get("winrm_application").(string)
application, ok := state.GetOk("winrm_application")
if !ok {
return
}
applicationClient := client.SecurityApplications()
deleteApplicationInput := compute.DeleteSecurityApplicationInput{
Name: namePrefix + application,
Name: namePrefix + application.(string),
}
err = applicationClient.DeleteSecurityApplication(&deleteApplicationInput)
if err != nil {
ui.Say(fmt.Sprintf("Error deleting the packer-generated winrm security application %s; "+
"please delete manually. (error : %s)", application, err.Error()))
"please delete manually. (error : %s)", application.(string), err.Error()))
}
}

View File

@ -15,7 +15,6 @@ type stepSnapshot struct {
func (s *stepSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
// get variables from state
s.cleanupSnap = false
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating Snapshot...")
config := state.Get("config").(*Config)
@ -39,7 +38,6 @@ func (s *stepSnapshot) Run(_ context.Context, state multistep.StateBag) multiste
state.Put("error", err)
return multistep.ActionHalt
}
s.cleanupSnap = true
state.Put("snapshot", snap)
ui.Message(fmt.Sprintf("Created snapshot: %s.", snap.Name))
return multistep.ActionContinue
@ -47,13 +45,16 @@ func (s *stepSnapshot) Run(_ context.Context, state multistep.StateBag) multiste
func (s *stepSnapshot) Cleanup(state multistep.StateBag) {
// Delete the snapshot
ui := state.Get("ui").(packer.Ui)
if !s.cleanupSnap {
var snap *compute.Snapshot
if snapshot, ok := state.GetOk("snapshot"); ok {
snap = snapshot.(*compute.Snapshot)
} else {
return
}
ui := state.Get("ui").(packer.Ui)
ui.Say("Deleting Snapshot...")
client := state.Get("client").(*compute.ComputeClient)
snap := state.Get("snapshot").(*compute.Snapshot)
snapClient := client.Snapshots()
snapInput := compute.DeleteSnapshotInput{
Snapshot: snap.Name,