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
|
|
|
"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"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2013-06-13 13:56:34 -04:00
|
|
|
)
|
|
|
|
|
2019-07-04 10:25:42 -04:00
|
|
|
type stepSnapshot struct {
|
|
|
|
snapshotTimeout time.Duration
|
|
|
|
}
|
2013-06-13 13:56:34 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-09-18 05:36:21 -04:00
|
|
|
c := state.Get("config").(*Config)
|
2015-06-10 17:02:06 -04:00
|
|
|
dropletId := state.Get("droplet_id").(int)
|
2017-05-16 17:05:40 -04:00
|
|
|
var snapshotRegions []string
|
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))
|
2017-04-09 14:33:05 -04:00
|
|
|
action, _, err := client.DropletActions.Snapshot(context.TODO(), 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
|
|
|
|
}
|
|
|
|
|
2016-09-08 04:32:43 -04:00
|
|
|
// With the pending state over, verify that we're in the active state
|
2019-07-04 10:25:42 -04:00
|
|
|
// because action can take a long time and may depend on the size of the final snapshot,
|
|
|
|
// the timeout is parameterized
|
2016-09-08 04:32:43 -04:00
|
|
|
ui.Say("Waiting for snapshot to complete...")
|
|
|
|
if err := waitForActionState(godo.ActionCompleted, dropletId, action.ID,
|
2019-07-04 10:25:42 -04:00
|
|
|
client, s.snapshotTimeout); err != nil {
|
2015-06-10 22:53:07 -04:00
|
|
|
// If we get an error the first time, actually report it
|
2016-09-08 04:32:43 -04:00
|
|
|
err := fmt.Errorf("Error waiting for snapshot: %s", err)
|
2015-06-10 22:53:07 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-09-08 04:32:43 -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
|
|
|
|
// 20 minutes.
|
|
|
|
if err := waitForDropletUnlocked(client, dropletId, 20*time.Minute); err != nil {
|
|
|
|
// 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-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)
|
2017-04-09 14:33:05 -04:00
|
|
|
images, _, err := client.Droplets.Snapshots(context.TODO(), dropletId, nil)
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-15 22:19:02 -04:00
|
|
|
if len(c.SnapshotRegions) > 0 {
|
|
|
|
regionSet := make(map[string]struct{})
|
|
|
|
regions := make([]string, 0, len(c.SnapshotRegions))
|
2017-05-15 22:51:48 -04:00
|
|
|
regionSet[c.Region] = struct{}{}
|
2017-05-15 22:19:02 -04:00
|
|
|
for _, region := range c.SnapshotRegions {
|
|
|
|
// If we already saw the region, then don't look again
|
|
|
|
if _, ok := regionSet[region]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark that we saw the region
|
|
|
|
regionSet[region] = struct{}{}
|
|
|
|
|
|
|
|
regions = append(regions, region)
|
|
|
|
}
|
2017-05-16 17:05:40 -04:00
|
|
|
snapshotRegions = regions
|
2017-05-15 22:51:48 -04:00
|
|
|
|
2017-05-16 17:05:40 -04:00
|
|
|
for transfer := range snapshotRegions {
|
2017-05-15 22:19:02 -04:00
|
|
|
transferRequest := &godo.ActionRequest{
|
|
|
|
"type": "transfer",
|
2017-05-16 17:05:40 -04:00
|
|
|
"region": snapshotRegions[transfer],
|
2017-05-15 22:19:02 -04:00
|
|
|
}
|
|
|
|
imageTransfer, _, err := client.ImageActions.Transfer(context.TODO(), images[0].ID, transferRequest)
|
|
|
|
if err != nil {
|
2017-08-07 13:45:50 -04:00
|
|
|
err := fmt.Errorf("Error transferring snapshot: %s", err)
|
2017-05-15 22:19:02 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-08-07 13:45:50 -04:00
|
|
|
ui.Say(fmt.Sprintf("transferring Snapshot ID: %d", imageTransfer.ID))
|
2018-11-22 21:33:34 -05:00
|
|
|
if err := WaitForImageState(godo.ActionCompleted, imageTransfer.ID, action.ID,
|
2017-05-16 10:38:53 -04:00
|
|
|
client, 20*time.Minute); err != nil {
|
|
|
|
// If we get an error the first time, actually report it
|
|
|
|
err := fmt.Errorf("Error waiting for snapshot transfer: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-05-15 22:19:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
var imageId int
|
2016-02-09 11:29:06 -05:00
|
|
|
if len(images) == 1 {
|
|
|
|
imageId = images[0].ID
|
|
|
|
} else {
|
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
|
|
|
|
}
|
2018-06-03 10:37:38 -04:00
|
|
|
snapshotRegions = append(snapshotRegions, c.Region)
|
2013-06-19 00:54:15 -04:00
|
|
|
|
|
|
|
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)
|
2017-05-16 17:05:40 -04:00
|
|
|
state.Put("regions", snapshotRegions)
|
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
|
|
|
|
}
|