From 7e13b5c62aad42cb4976bfe1032c78744b0097eb Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Mon, 2 Apr 2018 11:56:11 -0700 Subject: [PATCH] prevent panics when cleaning up resources that haven't been created. --- .../oracle/classic/step_create_instance.go | 8 ++++-- .../classic/step_create_ip_reservation.go | 8 ++++-- builder/oracle/classic/step_security.go | 28 +++++++++++++------ builder/oracle/classic/step_snapshot.go | 11 ++++---- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/builder/oracle/classic/step_create_instance.go b/builder/oracle/classic/step_create_instance.go index d9d26b3e3..d52500e09 100644 --- a/builder/oracle/classic/step_create_instance.go +++ b/builder/oracle/classic/step_create_instance.go @@ -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) diff --git a/builder/oracle/classic/step_create_ip_reservation.go b/builder/oracle/classic/step_create_ip_reservation.go index 74466a39f..5d400e746 100644 --- a/builder/oracle/classic/step_create_ip_reservation.go +++ b/builder/oracle/classic/step_create_ip_reservation.go @@ -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 { diff --git a/builder/oracle/classic/step_security.go b/builder/oracle/classic/step_security.go index 34b0d6544..a5b04418b 100644 --- a/builder/oracle/classic/step_security.go +++ b/builder/oracle/classic/step_security.go @@ -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())) } } diff --git a/builder/oracle/classic/step_snapshot.go b/builder/oracle/classic/step_snapshot.go index e0ebf3091..64058966d 100644 --- a/builder/oracle/classic/step_snapshot.go +++ b/builder/oracle/classic/step_snapshot.go @@ -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,