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-06-14 09:26:03 -04:00
|
|
|
"time"
|
2013-06-13 13:56:34 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepPowerOff struct{}
|
|
|
|
|
|
|
|
func (s *stepPowerOff) Run(state map[string]interface{}) multistep.StepAction {
|
|
|
|
client := state["client"].(*DigitalOceanClient)
|
2013-06-17 07:28:21 -04:00
|
|
|
c := state["config"].(config)
|
2013-06-13 13:56:34 -04:00
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
dropletId := state["droplet_id"].(uint)
|
|
|
|
|
2013-06-14 09:26:03 -04:00
|
|
|
// Sleep arbitrarily before sending power off request
|
|
|
|
// Otherwise we get "pending event" errors, even though there isn't
|
|
|
|
// one.
|
2013-06-17 07:28:21 -04:00
|
|
|
log.Printf("Sleeping for %v, event_delay", c.RawEventDelay)
|
2013-07-14 08:20:29 -04:00
|
|
|
time.Sleep(c.eventDelay)
|
2013-06-14 09:26:03 -04:00
|
|
|
|
2013-06-13 13:56:34 -04:00
|
|
|
// Poweroff the droplet so it can be snapshot
|
|
|
|
err := client.PowerOffDroplet(dropletId)
|
|
|
|
|
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error powering off droplet: %s", err)
|
|
|
|
state["error"] = err
|
2013-06-13 13:56:34 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Waiting for droplet to power off...")
|
|
|
|
|
2013-06-23 06:51:51 -04:00
|
|
|
err = waitForDropletState("off", dropletId, client, c)
|
2013-06-13 13:56:34 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error waiting for droplet to become 'off': %s", err)
|
|
|
|
state["error"] = err
|
2013-06-13 13:56:34 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepPowerOff) Cleanup(state map[string]interface{}) {
|
|
|
|
// no cleanup
|
|
|
|
}
|