2013-07-29 21:07:07 -04:00
|
|
|
package chroot
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
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"
|
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"
|
2018-07-18 06:01:15 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
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 {
|
2015-06-23 11:35:59 -04:00
|
|
|
volumeId string
|
|
|
|
RootVolumeSize int64
|
2018-07-18 06:01:15 -04:00
|
|
|
RootVolumeTags awscommon.TagMap
|
|
|
|
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)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-29 21:07:07 -04:00
|
|
|
|
2018-07-18 06:01:15 -04:00
|
|
|
volTags, err := s.RootVolumeTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
|
|
|
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 {
|
|
|
|
createVolume = &ec2.CreateVolumeInput{
|
|
|
|
AvailabilityZone: instance.Placement.AvailabilityZone,
|
|
|
|
Size: aws.Int64(s.RootVolumeSize),
|
|
|
|
VolumeType: aws.String(ec2.VolumeTypeGp2),
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2016-08-10 20:24:30 -04:00
|
|
|
if rootDevice == nil {
|
|
|
|
err := fmt.Errorf("Couldn't find root device!")
|
|
|
|
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
|
|
|
ui.Say("Creating the root volume...")
|
|
|
|
vs := *rootDevice.Ebs.VolumeSize
|
|
|
|
if s.RootVolumeSize > *rootDevice.Ebs.VolumeSize {
|
|
|
|
vs = s.RootVolumeSize
|
|
|
|
}
|
|
|
|
|
|
|
|
createVolume = &ec2.CreateVolumeInput{
|
|
|
|
AvailabilityZone: instance.Placement.AvailabilityZone,
|
|
|
|
Size: aws.Int64(vs),
|
|
|
|
SnapshotId: rootDevice.Ebs.SnapshotId,
|
|
|
|
VolumeType: rootDevice.Ebs.VolumeType,
|
|
|
|
Iops: rootDevice.Ebs.Iops,
|
|
|
|
}
|
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
|
2018-06-01 19:17:30 -04:00
|
|
|
err = awscommon.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)
|
|
|
|
ui := state.Get("ui").(packer.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))
|
|
|
|
}
|
|
|
|
}
|