2013-06-13 13:56:34 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2013-06-20 00:00:51 -04:00
|
|
|
"fmt"
|
2013-06-13 13:56:34 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-17 07:28:21 -04:00
|
|
|
"log"
|
2013-09-05 00:59:58 -04:00
|
|
|
"time"
|
2013-06-13 13:56:34 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepPowerOff struct{}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*DigitalOceanClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
dropletId := state.Get("droplet_id").(uint)
|
2013-06-13 13:56:34 -04:00
|
|
|
|
2013-09-05 00:59:58 -04:00
|
|
|
// 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)
|
2013-09-05 01:07:19 -04:00
|
|
|
if err == nil {
|
|
|
|
// We reached the state!
|
|
|
|
break
|
|
|
|
}
|
2013-09-05 00:59:58 -04:00
|
|
|
}
|
|
|
|
|
2013-06-13 13:56:34 -04:00
|
|
|
if err != nil {
|
2013-09-05 00:59:58 -04:00
|
|
|
err := fmt.Errorf("Error waiting for droplet to become 'off': %s", err)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-06-13 13:56:34 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-24 07:04:51 -04:00
|
|
|
log.Println("Waiting for poweroff event to complete...")
|
2013-06-13 13:56:34 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepPowerOff) Cleanup(state multistep.StateBag) {
|
2013-06-13 13:56:34 -04:00
|
|
|
// no cleanup
|
|
|
|
}
|