From f6c60aac787b54441053fcda79f47ca73cc31c9d Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 19 Jan 2018 16:08:42 -0800 Subject: [PATCH] clean up instance --- .../oracle/classic/step_create_instance.go | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/builder/oracle/classic/step_create_instance.go b/builder/oracle/classic/step_create_instance.go index 1cbfcbcc6..8c3edf3a3 100644 --- a/builder/oracle/classic/step_create_instance.go +++ b/builder/oracle/classic/step_create_instance.go @@ -92,5 +92,30 @@ func (s *stepCreateInstance) Run(state multistep.StateBag) multistep.StepAction } func (s *stepCreateInstance) Cleanup(state multistep.StateBag) { - // Nothing to do + // terminate instance + ui := state.Get("ui").(packer.Ui) + client := state.Get("client").(*compute.ComputeClient) + imID := state.Get("instance_id").(string) + + ui.Say(fmt.Sprintf("Terminating instance (%s)...", id)) + + instanceClient := client.Instances() + // Instances Input + input := &compute.DeleteInstanceInput{ + Name: config.ImageName, + ID: imID, + } + + instanceInfo, 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 instance.") + return }