From ca3bffcb0dee33eb2dccb4b03faf70a07b2fa59e Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Thu, 6 Sep 2018 00:05:47 -0700 Subject: [PATCH] bugfix and some debug logging --- .../oracle/classic/step_create_pv_builder.go | 3 +- .../oracle/classic/step_create_pv_master.go | 31 +++++++++++++++++++ builder/oracle/classic/step_security.go | 3 ++ .../classic/step_terminate_pv_master.go | 1 + 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/builder/oracle/classic/step_create_pv_builder.go b/builder/oracle/classic/step_create_pv_builder.go index 27348a077..96e189209 100644 --- a/builder/oracle/classic/step_create_pv_builder.go +++ b/builder/oracle/classic/step_create_pv_builder.go @@ -76,13 +76,12 @@ func (s *stepCreatePVBuilder) Cleanup(state multistep.StateBag) { // terminate instance ui := state.Get("ui").(packer.Ui) client := state.Get("client").(*compute.ComputeClient) - config := state.Get("config").(*Config) ui.Say("Terminating builder instance...") instanceClient := client.Instances() input := &compute.DeleteInstanceInput{ - Name: config.ImageName, + Name: s.name, ID: instanceID.(string), } diff --git a/builder/oracle/classic/step_create_pv_master.go b/builder/oracle/classic/step_create_pv_master.go index 57390253e..638083188 100644 --- a/builder/oracle/classic/step_create_pv_master.go +++ b/builder/oracle/classic/step_create_pv_master.go @@ -63,4 +63,35 @@ func (s *stepCreatePVMaster) Run(_ context.Context, state multistep.StateBag) mu } func (s *stepCreatePVMaster) Cleanup(state multistep.StateBag) { + _, deleted := state.GetOk("master_instance_deleted") + if deleted { + return + } + + instanceID, ok := state.GetOk("master_instance_id") + if !ok { + return + } + + // terminate instance + ui := state.Get("ui").(packer.Ui) + client := state.Get("client").(*compute.ComputeClient) + + ui.Say("Terminating builder instance...") + + instanceClient := client.Instances() + input := &compute.DeleteInstanceInput{ + Name: s.name, + ID: instanceID.(string), + } + + err := instanceClient.DeleteInstance(input) + if err != nil { + err = fmt.Errorf("Problem destroying instance: %s", err) + ui.Error(err.Error()) + state.Put("error", err) + return + } + // TODO wait for instance state to change to deleted? + ui.Say("Terminated master instance.") } diff --git a/builder/oracle/classic/step_security.go b/builder/oracle/classic/step_security.go index f44702992..74e249df6 100644 --- a/builder/oracle/classic/step_security.go +++ b/builder/oracle/classic/step_security.go @@ -3,6 +3,7 @@ package classic import ( "context" "fmt" + "log" "strings" "github.com/hashicorp/go-oracle-terraform/compute" @@ -30,6 +31,7 @@ func (s *stepSecurity) Run(_ context.Context, state multistep.StateBag) multiste runUUID := uuid.TimeOrderedUUID() secListName := fmt.Sprintf("Packer_%s_Allow_%s_%s", commType, config.ImageName, runUUID) + log.Println(secListName) secListClient := client.SecurityLists() secListInput := compute.CreateSecurityListInput{ Description: fmt.Sprintf("Packer-generated security list to give packer %s access", commType), @@ -73,6 +75,7 @@ func (s *stepSecurity) Run(_ context.Context, state multistep.StateBag) multiste secRulesClient := client.SecRules() secRuleName := fmt.Sprintf("Packer-allow-%s-Rule_%s_%s", commType, config.ImageName, runUUID) + log.Println(secRuleName) secRulesInput := compute.CreateSecRuleInput{ Action: "PERMIT", Application: application, diff --git a/builder/oracle/classic/step_terminate_pv_master.go b/builder/oracle/classic/step_terminate_pv_master.go index 59d4cbc99..f4de4282d 100644 --- a/builder/oracle/classic/step_terminate_pv_master.go +++ b/builder/oracle/classic/step_terminate_pv_master.go @@ -38,6 +38,7 @@ func (s *stepTerminatePVMaster) Run(_ context.Context, state multistep.StateBag) } ui.Message(fmt.Sprintf("Deleted master instance: %s.", instanceInfo.ID)) + state.Put("master_instance_deleted", true) return multistep.ActionContinue }