From eece75a7a6e56fe9c9ac6bcd2d1ea17a4808f68b Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Wed, 10 May 2017 09:12:24 -0700 Subject: [PATCH] Wait for a 410 before returning success on a machine delete. --- builder/triton/driver_triton.go | 16 +++++++++------- builder/triton/step_create_source_machine.go | 7 +++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/builder/triton/driver_triton.go b/builder/triton/driver_triton.go index af0fe8333..cfb3b0e72 100644 --- a/builder/triton/driver_triton.go +++ b/builder/triton/driver_triton.go @@ -3,6 +3,7 @@ package triton import ( "context" "errors" + "net/http" "time" "github.com/hashicorp/packer/packer" @@ -129,15 +130,16 @@ func (d *driverTriton) WaitForMachineState(machineId string, state string, timeo func (d *driverTriton) WaitForMachineDeletion(machineId string, timeout time.Duration) error { return waitFor( func() (bool, error) { - machine, err := d.client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{ + _, err := d.client.Machines().GetMachine(context.Background(), &triton.GetMachineInput{ ID: machineId, }) - if err != nil && triton.IsResourceNotFound(err) { - return true, nil - } - - if machine != nil { - return machine.State == "deleted", nil + if err != nil { + // Return true only when we receive a 410 (Gone) response. A 404 + // indicates that the machine is being deleted whereas a 410 indicates + // that this process has completed. + if triErr, ok := err.(*triton.TritonError); ok && triErr.StatusCode == http.StatusGone { + return true, nil + } } return false, err diff --git a/builder/triton/step_create_source_machine.go b/builder/triton/step_create_source_machine.go index b5a8aec15..46b7d789c 100644 --- a/builder/triton/step_create_source_machine.go +++ b/builder/triton/step_create_source_machine.go @@ -64,5 +64,12 @@ func (s *StepCreateSourceMachine) Cleanup(state multistep.StateBag) { state.Put("error", fmt.Errorf("Problem deleting source machine: %s", err)) return } + + ui.Say(fmt.Sprintf("Waiting for source machine to be destroyed (%s)...", machineId)) + err = driver.WaitForMachineDeletion(machineId, 10*time.Minute) + if err != nil { + state.Put("error", fmt.Errorf("Problem waiting for source machine to be deleted: %s", err)) + return + } } }