2013-06-13 19:56:34 +02:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2013-06-19 21:00:51 -07:00
|
|
|
"errors"
|
2013-06-17 14:21:15 +02:00
|
|
|
"fmt"
|
2013-06-13 19:56:34 +02:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-18 21:54:15 -07:00
|
|
|
"log"
|
2013-06-13 19:56:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepSnapshot struct{}
|
|
|
|
|
2013-08-31 12:25:08 -07:00
|
|
|
func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*DigitalOceanClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
c := state.Get("config").(config)
|
|
|
|
dropletId := state.Get("droplet_id").(uint)
|
2013-06-13 19:56:34 +02:00
|
|
|
|
2013-06-17 14:21:15 +02:00
|
|
|
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
|
2013-06-13 19:56:34 +02:00
|
|
|
err := client.CreateSnapshot(dropletId, c.SnapshotName)
|
|
|
|
if err != nil {
|
2013-06-19 21:00:51 -07:00
|
|
|
err := fmt.Errorf("Error creating snapshot: %s", err)
|
2013-08-31 12:25:08 -07:00
|
|
|
state.Put("error", err)
|
2013-06-13 19:56:34 +02:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Waiting for snapshot to complete...")
|
2013-09-04 21:59:58 -07:00
|
|
|
err = waitForDropletState("active", dropletId, client, c.stateTimeout)
|
2013-06-18 21:54:15 -07:00
|
|
|
if err != nil {
|
2013-06-19 21:00:51 -07:00
|
|
|
err := fmt.Errorf("Error waiting for snapshot to complete: %s", err)
|
2013-08-31 12:25:08 -07:00
|
|
|
state.Put("error", err)
|
2013-06-18 21:54:15 -07:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-13 19:56:34 +02:00
|
|
|
|
2013-06-18 21:54:15 -07:00
|
|
|
log.Printf("Looking up snapshot ID for snapshot: %s", c.SnapshotName)
|
|
|
|
images, err := client.Images()
|
2013-06-13 19:56:34 +02:00
|
|
|
if err != nil {
|
2013-06-19 21:00:51 -07:00
|
|
|
err := fmt.Errorf("Error looking up snapshot ID: %s", err)
|
2013-08-31 12:25:08 -07:00
|
|
|
state.Put("error", err)
|
2013-06-13 19:56:34 +02:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-18 21:54:15 -07:00
|
|
|
var imageId uint
|
|
|
|
for _, image := range images {
|
|
|
|
if image.Name == c.SnapshotName {
|
|
|
|
imageId = image.Id
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if imageId == 0 {
|
2013-06-19 21:00:51 -07:00
|
|
|
err := errors.New("Couldn't find snapshot to get the image ID. Bug?")
|
2013-08-31 12:25:08 -07:00
|
|
|
state.Put("error", err)
|
2013-06-19 21:00:51 -07:00
|
|
|
ui.Error(err.Error())
|
2013-06-18 21:54:15 -07:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Snapshot image ID: %d", imageId)
|
|
|
|
|
2013-08-31 12:25:08 -07:00
|
|
|
state.Put("snapshot_image_id", imageId)
|
|
|
|
state.Put("snapshot_name", c.SnapshotName)
|
2014-04-29 20:33:31 -07:00
|
|
|
state.Put("region", c.Region)
|
2013-06-18 21:54:15 -07:00
|
|
|
|
2013-06-13 19:56:34 +02:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:25:08 -07:00
|
|
|
func (s *stepSnapshot) Cleanup(state multistep.StateBag) {
|
2013-06-13 19:56:34 +02:00
|
|
|
// no cleanup
|
|
|
|
}
|