2013-08-24 07:04:51 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-09-05 01:14:30 -04:00
|
|
|
"log"
|
2013-09-05 01:26:05 -04:00
|
|
|
"time"
|
2014-09-03 17:37:33 -04:00
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2014-09-03 17:37:33 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-08-24 07:04:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepShutdown struct{}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepShutdown) Run(state multistep.StateBag) multistep.StepAction {
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
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-08-24 07:04:51 -04:00
|
|
|
|
2013-09-05 01:14:30 -04:00
|
|
|
// Gracefully power off the droplet. We have to retry this a number
|
|
|
|
// of times because sometimes it says it completed when it actually
|
|
|
|
// did absolutely nothing (*ALAKAZAM!* magic!). We give up after
|
|
|
|
// a pretty arbitrary amount of time.
|
|
|
|
ui.Say("Gracefully shutting down droplet...")
|
2015-06-10 17:02:06 -04:00
|
|
|
_, _, err := client.DropletActions.Shutdown(dropletId)
|
2013-08-24 07:04:51 -04:00
|
|
|
if err != nil {
|
2013-09-05 01:51:28 -04:00
|
|
|
// If we get an error the first time, actually report it
|
|
|
|
err := fmt.Errorf("Error shutting down droplet: %s", err)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-08-24 07:04:51 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-09-05 01:51:28 -04:00
|
|
|
// A channel we use as a flag to end our goroutines
|
|
|
|
done := make(chan struct{})
|
|
|
|
shutdownRetryDone := make(chan struct{})
|
|
|
|
|
|
|
|
// Make sure we wait for the shutdown retry goroutine to end
|
|
|
|
// before moving on.
|
|
|
|
defer func() {
|
|
|
|
close(done)
|
|
|
|
<-shutdownRetryDone
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start a goroutine that just keeps trying to shut down the
|
|
|
|
// droplet.
|
|
|
|
go func() {
|
|
|
|
defer close(shutdownRetryDone)
|
|
|
|
|
|
|
|
for attempts := 2; attempts > 0; attempts++ {
|
|
|
|
log.Printf("ShutdownDroplet attempt #%d...", attempts)
|
2015-06-10 17:02:06 -04:00
|
|
|
_, _, err := client.DropletActions.Shutdown(dropletId)
|
2013-09-05 01:51:28 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Shutdown retry error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
// Retry!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-09-05 02:01:37 -04:00
|
|
|
err = waitForDropletState("off", dropletId, client, 2*time.Minute)
|
2013-09-05 01:51:28 -04:00
|
|
|
if 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 shutting down droplet: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := waitForDropletUnlocked(client, dropletId, 2*time.Minute); err != nil {
|
|
|
|
// If we get an error the first time, actually report it
|
|
|
|
err := fmt.Errorf("Error shutting down droplet: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-09-05 01:51:28 -04:00
|
|
|
}
|
|
|
|
|
2013-08-24 07:04:51 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepShutdown) Cleanup(state multistep.StateBag) {
|
2013-08-24 07:04:51 -04:00
|
|
|
// no cleanup
|
|
|
|
}
|