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
|
|
|
|
}
|
|
|
|
|
2013-08-24 07:04:51 -04:00
|
|
|
log.Println("Waiting for poweroff event to complete...")
|
2013-06-13 13:56:34 -04:00
|
|
|
|
2013-08-24 07:04:51 -04:00
|
|
|
// This arbitrary sleep is because we can't wait for the state
|
|
|
|
// of the droplet to be 'off', as stepShutdown should already
|
|
|
|
// have accomplished that, and the state indicator is the same.
|
|
|
|
// We just have to assume that this event will process quickly.
|
|
|
|
log.Printf("Sleeping for %v, event_delay", c.RawEventDelay)
|
|
|
|
time.Sleep(c.eventDelay)
|
2013-06-13 13:56:34 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepPowerOff) Cleanup(state map[string]interface{}) {
|
|
|
|
// no cleanup
|
|
|
|
}
|