diff --git a/builder/digitalocean/step_power_off.go b/builder/digitalocean/step_power_off.go index a29116cae..9964db802 100644 --- a/builder/digitalocean/step_power_off.go +++ b/builder/digitalocean/step_power_off.go @@ -15,31 +15,31 @@ func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) dropletId := state.Get("droplet_id").(uint) - // Gracefully power off the droplet. We have to retry this a number - // of times because sometimes it says it completed when it actually - // did absolutely nothing (*ALAKAZAM!* magic!). We give up after - // a pretty arbitrary amount of time. - var err error - ui.Say("Gracefully shutting down droplet...") - for attempts := 1; attempts <= 10; attempts++ { - log.Printf("PowerOffDroplet attempt #%d...", attempts) - err := client.PowerOffDroplet(dropletId) - if err != nil { - err := fmt.Errorf("Error powering off droplet: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - - err = waitForDropletState("off", dropletId, client, 20*time.Second) - if err == nil { - // We reached the state! - break - } + _, status, err := client.DropletStatus(dropletId) + if err != nil { + err := fmt.Errorf("Error checking droplet state: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + if status == "off" { + // Droplet is already off, don't do anything + return multistep.ActionContinue + } + + // Pull the plug on the Droplet + ui.Say("Forcefully shutting down Droplet...") + err = client.PowerOffDroplet(dropletId) + if err != nil { + err := fmt.Errorf("Error powering off droplet: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt } + err = waitForDropletState("off", dropletId, client, 20*time.Second) if err != nil { - err := fmt.Errorf("Error waiting for droplet to become 'off': %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt diff --git a/builder/digitalocean/step_shutdown.go b/builder/digitalocean/step_shutdown.go index 3bbbf83f8..16996bb02 100644 --- a/builder/digitalocean/step_shutdown.go +++ b/builder/digitalocean/step_shutdown.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" + "log" ) type stepShutdown struct{} @@ -14,17 +15,29 @@ func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) dropletId := state.Get("droplet_id").(uint) - err := client.ShutdownDroplet(dropletId) - if err != nil { - err := fmt.Errorf("Error shutting down droplet: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + // Gracefully power off the droplet. We have to retry this a number + // of times because sometimes it says it completed when it actually + // did absolutely nothing (*ALAKAZAM!* magic!). We give up after + // a pretty arbitrary amount of time. + var err error + ui.Say("Gracefully shutting down droplet...") + for attempts := 1; attempts <= 10; attempts++ { + log.Printf("ShutdownDropetl attempt #%d...", attempts) + err := client.ShutdownDroplet(dropletId) + if err != nil { + err := fmt.Errorf("Error shutting down droplet: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + err = waitForDropletState("off", dropletId, client, c.stateTimeout) + if err == nil { + break + } } - err = waitForDropletState("off", dropletId, client, c.stateTimeout) if err != nil { - err := fmt.Errorf("Error waiting for droplet to become 'off': %s", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt