2018-03-25 19:25:53 -04:00
|
|
|
package ebssurrogate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepSnapshotVolumes creates snapshots of the created volumes.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// snapshot_ids map[string]string - IDs of the created snapshots
|
|
|
|
type StepSnapshotVolumes struct {
|
2019-05-03 12:39:52 -04:00
|
|
|
LaunchDevices []*ec2.BlockDeviceMapping
|
|
|
|
snapshotIds map[string]string
|
2019-12-04 09:13:07 -05:00
|
|
|
snapshotMutex sync.Mutex
|
2019-05-03 12:39:52 -04:00
|
|
|
SnapshotOmitMap map[string]bool
|
2018-03-25 19:25:53 -04:00
|
|
|
}
|
|
|
|
|
2018-06-01 19:17:30 -04:00
|
|
|
func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName string, state multistep.StateBag) error {
|
2018-03-25 19:25:53 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
instance := state.Get("instance").(*ec2.Instance)
|
|
|
|
|
|
|
|
var volumeId string
|
|
|
|
for _, volume := range instance.BlockDeviceMappings {
|
|
|
|
if *volume.DeviceName == deviceName {
|
|
|
|
volumeId = *volume.Ebs.VolumeId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if volumeId == "" {
|
|
|
|
return fmt.Errorf("Volume ID for device %s not found", deviceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Creating snapshot of EBS Volume %s...", volumeId))
|
|
|
|
description := fmt.Sprintf("Packer: %s", time.Now().String())
|
|
|
|
|
|
|
|
createSnapResp, err := ec2conn.CreateSnapshot(&ec2.CreateSnapshotInput{
|
|
|
|
VolumeId: &volumeId,
|
|
|
|
Description: &description,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the snapshot ID so we can delete it later
|
2019-12-04 09:13:07 -05:00
|
|
|
s.snapshotMutex.Lock()
|
2018-03-25 19:25:53 -04:00
|
|
|
s.snapshotIds[deviceName] = *createSnapResp.SnapshotId
|
2019-12-04 09:13:07 -05:00
|
|
|
s.snapshotMutex.Unlock()
|
2018-03-25 19:25:53 -04:00
|
|
|
|
2018-05-31 14:29:04 -04:00
|
|
|
// Wait for snapshot to be created
|
2018-06-01 19:17:30 -04:00
|
|
|
err = awscommon.WaitUntilSnapshotDone(ctx, ec2conn, *createSnapResp.SnapshotId)
|
2018-03-25 19:25:53 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-01 19:17:30 -04:00
|
|
|
func (s *StepSnapshotVolumes) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-03-25 19:25:53 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
s.snapshotIds = map[string]string{}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var errs *multierror.Error
|
|
|
|
for _, device := range s.LaunchDevices {
|
2019-05-03 12:39:52 -04:00
|
|
|
// Skip devices we've flagged for omission
|
|
|
|
omit, ok := s.SnapshotOmitMap[*device.DeviceName]
|
|
|
|
if ok && omit {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-03-25 19:25:53 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(device *ec2.BlockDeviceMapping) {
|
|
|
|
defer wg.Done()
|
2018-06-01 19:17:30 -04:00
|
|
|
if err := s.snapshotVolume(ctx, *device.DeviceName, state); err != nil {
|
2018-03-25 19:25:53 -04:00
|
|
|
errs = multierror.Append(errs, err)
|
|
|
|
}
|
|
|
|
}(device)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if errs != nil {
|
|
|
|
state.Put("error", errs)
|
|
|
|
ui.Error(errs.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("snapshot_ids", s.snapshotIds)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSnapshotVolumes) Cleanup(state multistep.StateBag) {
|
|
|
|
if len(s.snapshotIds) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
|
|
|
|
if cancelled || halted {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Say("Removing snapshots since we cancelled or halted...")
|
2019-12-04 09:13:07 -05:00
|
|
|
s.snapshotMutex.Lock()
|
2018-03-25 19:25:53 -04:00
|
|
|
for _, snapshotId := range s.snapshotIds {
|
|
|
|
_, err := ec2conn.DeleteSnapshot(&ec2.DeleteSnapshotInput{SnapshotId: &snapshotId})
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error: %s", err))
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 09:13:07 -05:00
|
|
|
s.snapshotMutex.Unlock()
|
2018-03-25 19:25:53 -04:00
|
|
|
}
|
|
|
|
}
|