2013-08-24 07:04:51 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-09-05 01:14:30 -04:00
|
|
|
"log"
|
2013-09-05 01:26:05 -04:00
|
|
|
"time"
|
2013-08-24 07:04:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepShutdown struct{}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*DigitalOceanClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
dropletId := state.Get("droplet_id").(uint)
|
2013-08-24 07:04:51 -04:00
|
|
|
|
2013-09-05 01:14:30 -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++ {
|
2013-09-05 01:26:05 -04:00
|
|
|
log.Printf("ShutdownDroplet attempt #%d...", attempts)
|
2013-09-05 01:14:30 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-09-05 01:26:05 -04:00
|
|
|
err = waitForDropletState("off", dropletId, client, 20*time.Second)
|
2013-09-05 01:14:30 -04:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2013-08-24 07:04:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-08-24 07:04:51 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepShutdown) Cleanup(state multistep.StateBag) {
|
2013-08-24 07:04:51 -04:00
|
|
|
// no cleanup
|
|
|
|
}
|