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"
|
2015-06-10 22:53:07 -04:00
|
|
|
"time"
|
2014-09-03 17:37:33 -04:00
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
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 {
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2015-05-27 15:50:43 -04:00
|
|
|
c := state.Get("config").(Config)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2015-06-10 17:02:06 -04:00
|
|
|
dropletId := state.Get("droplet_id").(int)
|
2013-06-13 13:56:34 -04:00
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
droplet, _, err := client.Droplets.Get(dropletId)
|
2013-09-05 01:14:30 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error checking droplet state: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
if droplet.Status == "off" {
|
2013-09-05 01:14:30 -04:00
|
|
|
// Droplet is already off, don't do anything
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull the plug on the Droplet
|
|
|
|
ui.Say("Forcefully shutting down Droplet...")
|
2015-06-10 17:02:06 -04:00
|
|
|
_, _, err = client.DropletActions.PowerOff(dropletId)
|
2013-09-05 01:14:30 -04:00
|
|
|
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...")
|
2015-06-13 18:26:13 -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
|
|
|
|
}
|
|
|
|
|
2015-06-10 22:53:07 -04:00
|
|
|
// Wait for the droplet to become unlocked for future steps
|
2015-11-14 06:11:44 -05:00
|
|
|
if err := waitForDropletUnlocked(client, dropletId, 4*time.Minute); err != nil {
|
2015-06-10 22:53:07 -04:00
|
|
|
// If we get an error the first time, actually report it
|
|
|
|
err := fmt.Errorf("Error powering off droplet: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|