2013-06-13 13:56:34 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2013-06-20 00:00:51 -04:00
|
|
|
"fmt"
|
2014-09-03 17:37:33 -04:00
|
|
|
"log"
|
|
|
|
|
2013-06-13 13:56:34 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepPowerOff struct{}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepPowerOff) Run(state multistep.StateBag) multistep.StepAction {
|
2014-09-03 17:37:33 -04:00
|
|
|
client := state.Get("client").(DigitalOceanClient)
|
2013-09-05 01:26:05 -04:00
|
|
|
c := state.Get("config").(config)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
dropletId := state.Get("droplet_id").(uint)
|
2013-06-13 13:56:34 -04:00
|
|
|
|
2013-09-05 01:14:30 -04:00
|
|
|
_, status, err := client.DropletStatus(dropletId)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error checking droplet state: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == "off" {
|
|
|
|
// Droplet is already off, don't do anything
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull the plug on the Droplet
|
|
|
|
ui.Say("Forcefully shutting down Droplet...")
|
|
|
|
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
|
2013-09-05 00:59:58 -04:00
|
|
|
}
|
|
|
|
|
2013-09-05 01:51:28 -04:00
|
|
|
log.Println("Waiting for poweroff event to complete...")
|
2013-09-05 01:26:05 -04:00
|
|
|
err = waitForDropletState("off", dropletId, client, c.stateTimeout)
|
2013-06-13 13:56:34 -04:00
|
|
|
if err != nil {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|