2013-06-13 13:56:34 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2013-06-20 00:00:51 -04:00
|
|
|
"errors"
|
2013-06-17 08:21:15 -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 stepSnapshot struct{}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepSnapshot) 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-05-27 15:50:43 -04:00
|
|
|
c := state.Get("config").(Config)
|
2015-06-10 17:02:06 -04:00
|
|
|
dropletId := state.Get("droplet_id").(int)
|
2013-06-13 13:56:34 -04:00
|
|
|
|
2013-06-17 08:21:15 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
|
2015-06-10 17:02:06 -04:00
|
|
|
_, _, err := client.DropletActions.Snapshot(dropletId, c.SnapshotName)
|
2013-06-13 13:56:34 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error creating snapshot: %s", err)
|
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 first. For snapshots
|
|
|
|
// this can end up taking quite a long time, so we hardcode this to
|
|
|
|
// 10 minutes.
|
|
|
|
if err := waitForDropletUnlocked(client, dropletId, 10*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
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the pending state over, verify that we're in the active state
|
2013-06-13 13:56:34 -04:00
|
|
|
ui.Say("Waiting for snapshot to complete...")
|
2015-06-13 18:26:13 -04:00
|
|
|
err = waitForDropletState("active", dropletId, client, c.StateTimeout)
|
2013-06-19 00:54:15 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error waiting for snapshot to complete: %s", err)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-06-19 00:54:15 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-13 13:56:34 -04:00
|
|
|
|
2013-06-19 00:54:15 -04:00
|
|
|
log.Printf("Looking up snapshot ID for snapshot: %s", c.SnapshotName)
|
2015-06-10 17:18:38 -04:00
|
|
|
images, _, err := client.Images.ListUser(&godo.ListOptions{PerPage: 200})
|
2013-06-13 13:56:34 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error looking up snapshot ID: %s", err)
|
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 17:02:06 -04:00
|
|
|
var imageId int
|
2013-06-19 00:54:15 -04:00
|
|
|
for _, image := range images {
|
|
|
|
if image.Name == c.SnapshotName {
|
2015-06-10 17:02:06 -04:00
|
|
|
imageId = image.ID
|
2013-06-19 00:54:15 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if imageId == 0 {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := errors.New("Couldn't find snapshot to get the image ID. Bug?")
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:00:51 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-19 00:54:15 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Snapshot image ID: %d", imageId)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("snapshot_image_id", imageId)
|
|
|
|
state.Put("snapshot_name", c.SnapshotName)
|
2014-04-29 23:33:31 -04:00
|
|
|
state.Put("region", c.Region)
|
2013-06-19 00:54:15 -04:00
|
|
|
|
2013-06-13 13:56:34 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepSnapshot) Cleanup(state multistep.StateBag) {
|
2013-06-13 13:56:34 -04:00
|
|
|
// no cleanup
|
|
|
|
}
|