2013-06-13 13:56:34 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-04-08 15:52:57 -04:00
|
|
|
"context"
|
2013-06-20 00:00:51 -04:00
|
|
|
"fmt"
|
2014-09-03 17:37:33 -04:00
|
|
|
"log"
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-13 13:56:34 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepPowerOff struct{}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepPowerOff) Run(_ context.Context, 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
|
|
|
|
2017-04-09 14:33:05 -04:00
|
|
|
droplet, _, err := client.Droplets.Get(context.TODO(), 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...")
|
2017-04-09 14:33:05 -04:00
|
|
|
_, _, err = client.DropletActions.PowerOff(context.TODO(), 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
|
2016-08-14 15:12:30 -04:00
|
|
|
if err := waitForDropletUnlocked(client, dropletId, c.StateTimeout); 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
|
|
|
|
}
|