2013-07-29 21:07:07 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2018-09-06 16:20:26 -04:00
|
|
|
"errors"
|
2013-07-29 21:07:07 -04:00
|
|
|
"fmt"
|
2015-04-05 17:58:48 -04:00
|
|
|
"log"
|
|
|
|
|
2015-06-23 11:35:59 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2017-04-04 16:39:01 -04:00
|
|
|
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
2013-07-29 21:07:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepCreateVolume creates a new volume from the snapshot of the root
|
|
|
|
// device of the AMI.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// volume_id string - The ID of the created volume
|
|
|
|
type StepCreateVolume struct {
|
2020-11-10 15:27:29 -05:00
|
|
|
PollingConfig *awscommon.AWSPollingConfig
|
|
|
|
volumeId string
|
|
|
|
RootVolumeSize int64
|
|
|
|
RootVolumeType string
|
|
|
|
RootVolumeTags map[string]string
|
|
|
|
RootVolumeEncryptBoot config.Trilean
|
|
|
|
RootVolumeKmsKeyId string
|
|
|
|
Ctx interpolate.Context
|
2013-07-29 21:07:07 -04:00
|
|
|
}
|
|
|
|
|
2018-06-01 19:17:30 -04:00
|
|
|
func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-08-10 20:24:30 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
instance := state.Get("instance").(*ec2.Instance)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-07-29 21:07:07 -04:00
|
|
|
|
2020-03-16 12:47:44 -04:00
|
|
|
volTags, err := awscommon.TagMap(s.RootVolumeTags).EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
2018-07-18 06:01:15 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging volumes: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect tags for tagging on resource creation
|
|
|
|
var tagSpecs []*ec2.TagSpecification
|
|
|
|
|
|
|
|
if len(volTags) > 0 {
|
|
|
|
runVolTags := &ec2.TagSpecification{
|
|
|
|
ResourceType: aws.String("volume"),
|
|
|
|
Tags: volTags,
|
|
|
|
}
|
|
|
|
|
|
|
|
tagSpecs = append(tagSpecs, runVolTags)
|
|
|
|
}
|
|
|
|
|
2016-08-10 20:24:30 -04:00
|
|
|
var createVolume *ec2.CreateVolumeInput
|
|
|
|
if config.FromScratch {
|
2018-09-06 16:20:26 -04:00
|
|
|
rootVolumeType := ec2.VolumeTypeGp2
|
|
|
|
if s.RootVolumeType == "io1" {
|
|
|
|
err := errors.New("Cannot use io1 volume when building from scratch")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
} else if s.RootVolumeType != "" {
|
|
|
|
rootVolumeType = s.RootVolumeType
|
|
|
|
}
|
2016-08-10 20:24:30 -04:00
|
|
|
createVolume = &ec2.CreateVolumeInput{
|
|
|
|
AvailabilityZone: instance.Placement.AvailabilityZone,
|
|
|
|
Size: aws.Int64(s.RootVolumeSize),
|
2018-09-06 16:20:26 -04:00
|
|
|
VolumeType: aws.String(rootVolumeType),
|
2016-08-10 20:24:30 -04:00
|
|
|
}
|
2018-09-06 16:20:26 -04:00
|
|
|
|
2016-08-10 20:24:30 -04:00
|
|
|
} else {
|
|
|
|
// Determine the root device snapshot
|
|
|
|
image := state.Get("source_image").(*ec2.Image)
|
|
|
|
log.Printf("Searching for root device of the image (%s)", *image.RootDeviceName)
|
|
|
|
var rootDevice *ec2.BlockDeviceMapping
|
|
|
|
for _, device := range image.BlockDeviceMappings {
|
|
|
|
if *device.DeviceName == *image.RootDeviceName {
|
|
|
|
rootDevice = device
|
|
|
|
break
|
|
|
|
}
|
2013-07-29 21:07:07 -04:00
|
|
|
}
|
|
|
|
|
2018-09-04 19:56:17 -04:00
|
|
|
ui.Say("Creating the root volume...")
|
|
|
|
createVolume, err = s.buildCreateVolumeInput(*instance.Placement.AvailabilityZone, rootDevice)
|
|
|
|
if err != nil {
|
2016-08-10 20:24:30 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-29 21:07:07 -04:00
|
|
|
}
|
2016-08-10 20:24:30 -04:00
|
|
|
|
2018-07-18 06:01:15 -04:00
|
|
|
if len(tagSpecs) > 0 {
|
|
|
|
createVolume.SetTagSpecifications(tagSpecs)
|
|
|
|
volTags.Report(ui)
|
|
|
|
}
|
2015-10-11 15:18:23 -04:00
|
|
|
log.Printf("Create args: %+v", createVolume)
|
2013-07-29 21:07:07 -04:00
|
|
|
|
|
|
|
createVolumeResp, err := ec2conn.CreateVolume(createVolume)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating root volume: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 21:07:07 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the volume ID so we remember to delete it later
|
2015-08-17 20:44:01 -04:00
|
|
|
s.volumeId = *createVolumeResp.VolumeId
|
2013-07-29 21:55:11 -04:00
|
|
|
log.Printf("Volume ID: %s", s.volumeId)
|
|
|
|
|
|
|
|
// Wait for the volume to become ready
|
2020-08-17 11:09:19 -04:00
|
|
|
err = s.PollingConfig.WaitUntilVolumeAvailable(ctx, ec2conn, s.volumeId)
|
2013-07-29 21:55:11 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for volume: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 21:55:11 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-29 21:07:07 -04:00
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("volume_id", s.volumeId)
|
2013-07-29 21:07:07 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
|
2013-07-29 21:17:27 -04:00
|
|
|
if s.volumeId == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-07-29 21:17:27 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting the created EBS volume...")
|
2015-08-17 20:44:01 -04:00
|
|
|
_, err := ec2conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeId: &s.volumeId})
|
2013-07-29 21:17:27 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting EBS volume: %s", err))
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 19:56:17 -04:00
|
|
|
|
|
|
|
func (s *StepCreateVolume) buildCreateVolumeInput(az string, rootDevice *ec2.BlockDeviceMapping) (*ec2.CreateVolumeInput, error) {
|
|
|
|
if rootDevice == nil {
|
|
|
|
return nil, fmt.Errorf("Couldn't find root device!")
|
|
|
|
}
|
|
|
|
createVolumeInput := &ec2.CreateVolumeInput{
|
|
|
|
AvailabilityZone: aws.String(az),
|
|
|
|
Size: rootDevice.Ebs.VolumeSize,
|
|
|
|
SnapshotId: rootDevice.Ebs.SnapshotId,
|
|
|
|
VolumeType: rootDevice.Ebs.VolumeType,
|
|
|
|
Iops: rootDevice.Ebs.Iops,
|
2020-11-10 15:27:29 -05:00
|
|
|
Encrypted: rootDevice.Ebs.Encrypted,
|
|
|
|
KmsKeyId: rootDevice.Ebs.KmsKeyId,
|
2018-09-04 19:56:17 -04:00
|
|
|
}
|
|
|
|
if s.RootVolumeSize > *rootDevice.Ebs.VolumeSize {
|
|
|
|
createVolumeInput.Size = aws.Int64(s.RootVolumeSize)
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:27:29 -05:00
|
|
|
if s.RootVolumeEncryptBoot.True() {
|
|
|
|
createVolumeInput.Encrypted = aws.Bool(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.RootVolumeKmsKeyId != "" {
|
|
|
|
createVolumeInput.KmsKeyId = aws.String(s.RootVolumeKmsKeyId)
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:56:17 -04:00
|
|
|
if s.RootVolumeType == "" || s.RootVolumeType == *rootDevice.Ebs.VolumeType {
|
|
|
|
return createVolumeInput, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.RootVolumeType == "io1" {
|
|
|
|
return nil, fmt.Errorf("Root volume type cannot be io1, because existing root volume type was %s", *rootDevice.Ebs.VolumeType)
|
|
|
|
}
|
|
|
|
|
|
|
|
createVolumeInput.VolumeType = aws.String(s.RootVolumeType)
|
|
|
|
// non io1 cannot set iops
|
|
|
|
createVolumeInput.Iops = nil
|
|
|
|
|
|
|
|
return createVolumeInput, nil
|
|
|
|
}
|