2013-07-30 19:58:58 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-07-30 19:58:58 -04:00
|
|
|
"fmt"
|
2014-09-05 19:10:33 -04:00
|
|
|
"time"
|
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2017-04-04 16:39:01 -04:00
|
|
|
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
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-07-30 19:58:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepSnapshot creates a snapshot of the created volume.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// snapshot_id string - ID of the created snapshot
|
|
|
|
type StepSnapshot struct {
|
|
|
|
snapshotId string
|
|
|
|
}
|
|
|
|
|
2018-06-01 19:17:30 -04:00
|
|
|
func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
volumeId := state.Get("volume_id").(string)
|
2013-07-30 19:58:58 -04:00
|
|
|
|
|
|
|
ui.Say("Creating snapshot...")
|
2015-04-05 17:58:48 -04:00
|
|
|
description := fmt.Sprintf("Packer: %s", time.Now().String())
|
|
|
|
|
|
|
|
createSnapResp, err := ec2conn.CreateSnapshot(&ec2.CreateSnapshotInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
VolumeId: &volumeId,
|
2015-04-05 17:58:48 -04:00
|
|
|
Description: &description,
|
|
|
|
})
|
2013-07-30 19:58:58 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating snapshot: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-30 19:58:58 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the snapshot ID so we can delete it later
|
2015-08-17 20:44:01 -04:00
|
|
|
s.snapshotId = *createSnapResp.SnapshotId
|
2013-07-30 19:58:58 -04:00
|
|
|
ui.Message(fmt.Sprintf("Snapshot ID: %s", s.snapshotId))
|
|
|
|
|
|
|
|
// Wait for the snapshot to be ready
|
2018-06-01 19:17:30 -04:00
|
|
|
err = awscommon.WaitUntilSnapshotDone(ctx, ec2conn, s.snapshotId)
|
2013-07-30 19:58:58 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for snapshot: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-30 19:58:58 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("snapshot_id", s.snapshotId)
|
2016-12-29 11:16:52 -05:00
|
|
|
|
|
|
|
snapshots := map[string][]string{
|
|
|
|
*ec2conn.Config.Region: {s.snapshotId},
|
|
|
|
}
|
|
|
|
state.Put("snapshots", snapshots)
|
|
|
|
|
2013-07-30 19:58:58 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepSnapshot) Cleanup(state multistep.StateBag) {
|
2013-07-30 19:58:58 -04:00
|
|
|
if s.snapshotId == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2013-07-30 19:58:58 -04:00
|
|
|
|
|
|
|
if cancelled || halted {
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-30 19:58:58 -04:00
|
|
|
ui.Say("Removing snapshot since we cancelled or halted...")
|
2015-08-17 20:44:01 -04:00
|
|
|
_, err := ec2conn.DeleteSnapshot(&ec2.DeleteSnapshotInput{SnapshotId: &s.snapshotId})
|
2013-07-30 19:58:58 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|