Commit 4af230a4 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/digitalocean: actually, we want to try hard on shutdown

parent 39f7a5a6
...@@ -15,31 +15,31 @@ func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction { ...@@ -15,31 +15,31 @@ func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
dropletId := state.Get("droplet_id").(uint) dropletId := state.Get("droplet_id").(uint)
// Gracefully power off the droplet. We have to retry this a number _, status, err := client.DropletStatus(dropletId)
// of times because sometimes it says it completed when it actually if err != nil {
// did absolutely nothing (*ALAKAZAM!* magic!). We give up after err := fmt.Errorf("Error checking droplet state: %s", err)
// a pretty arbitrary amount of time. state.Put("error", err)
var err error ui.Error(err.Error())
ui.Say("Gracefully shutting down droplet...") return multistep.ActionHalt
for attempts := 1; attempts <= 10; attempts++ { }
log.Printf("PowerOffDroplet attempt #%d...", attempts)
err := client.PowerOffDroplet(dropletId) if status == "off" {
if err != nil { // Droplet is already off, don't do anything
err := fmt.Errorf("Error powering off droplet: %s", err) return multistep.ActionContinue
state.Put("error", err) }
ui.Error(err.Error())
return multistep.ActionHalt // Pull the plug on the Droplet
} ui.Say("Forcefully shutting down Droplet...")
err = client.PowerOffDroplet(dropletId)
err = waitForDropletState("off", dropletId, client, 20*time.Second) if err != nil {
if err == nil { err := fmt.Errorf("Error powering off droplet: %s", err)
// We reached the state! state.Put("error", err)
break ui.Error(err.Error())
} return multistep.ActionHalt
} }
err = waitForDropletState("off", dropletId, client, 20*time.Second)
if err != nil { if err != nil {
err := fmt.Errorf("Error waiting for droplet to become 'off': %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log"
) )
type stepShutdown struct{} type stepShutdown struct{}
...@@ -14,17 +15,29 @@ func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction { ...@@ -14,17 +15,29 @@ func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
dropletId := state.Get("droplet_id").(uint) dropletId := state.Get("droplet_id").(uint)
err := client.ShutdownDroplet(dropletId) // Gracefully power off the droplet. We have to retry this a number
if err != nil { // of times because sometimes it says it completed when it actually
err := fmt.Errorf("Error shutting down droplet: %s", err) // did absolutely nothing (*ALAKAZAM!* magic!). We give up after
state.Put("error", err) // a pretty arbitrary amount of time.
ui.Error(err.Error()) var err error
return multistep.ActionHalt 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 { if err != nil {
err := fmt.Errorf("Error waiting for droplet to become 'off': %s", err)
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
return multistep.ActionHalt return multistep.ActionHalt
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment