2016-12-30 16:21:23 -05:00
|
|
|
package ebssurrogate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"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"
|
2016-12-30 16:21:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepRegisterAMI creates the AMI.
|
|
|
|
type StepRegisterAMI struct {
|
2017-08-28 12:18:23 -04:00
|
|
|
RootDevice RootBlockDevice
|
|
|
|
BlockDevices []*ec2.BlockDeviceMapping
|
|
|
|
EnableAMIENASupport bool
|
|
|
|
EnableAMISriovNetSupport bool
|
|
|
|
image *ec2.Image
|
2016-12-30 16:21:23 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepRegisterAMI) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-12-30 16:21:23 -05:00
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
snapshotId := state.Get("snapshot_id").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Registering the AMI...")
|
|
|
|
|
2017-06-27 19:12:22 -04:00
|
|
|
blockDevicesExcludingRoot := DeduplicateRootVolume(s.BlockDevices, s.RootDevice, snapshotId)
|
2016-12-30 16:21:23 -05:00
|
|
|
|
|
|
|
registerOpts := &ec2.RegisterImageInput{
|
|
|
|
Name: &config.AMIName,
|
|
|
|
Architecture: aws.String(ec2.ArchitectureValuesX8664),
|
|
|
|
RootDeviceName: aws.String(s.RootDevice.DeviceName),
|
|
|
|
VirtualizationType: aws.String(config.AMIVirtType),
|
2017-02-27 01:40:07 -05:00
|
|
|
BlockDeviceMappings: blockDevicesExcludingRoot,
|
2016-12-30 16:21:23 -05:00
|
|
|
}
|
|
|
|
|
2017-08-28 12:18:23 -04:00
|
|
|
if s.EnableAMISriovNetSupport {
|
Always set both SRIOV and ENA when Enhanced Networking is enabled
Set SriovNetSupport to "simple". As of February 2017, this applies to C3, C4,
D2, I2, R3, and M4 (excluding m4.16xlarge).
Set EnaSupport to true. As of February 2017, this applies to C5, I3, P2, R4,
X1, and m4.16xlarge.
2017-02-21 20:46:16 -05:00
|
|
|
// Set SriovNetSupport to "simple". See http://goo.gl/icuXh5
|
|
|
|
// As of February 2017, this applies to C3, C4, D2, I2, R3, and M4 (excluding m4.16xlarge)
|
2016-12-30 16:21:23 -05:00
|
|
|
registerOpts.SriovNetSupport = aws.String("simple")
|
2017-08-28 12:18:23 -04:00
|
|
|
}
|
|
|
|
if s.EnableAMIENASupport {
|
Always set both SRIOV and ENA when Enhanced Networking is enabled
Set SriovNetSupport to "simple". As of February 2017, this applies to C3, C4,
D2, I2, R3, and M4 (excluding m4.16xlarge).
Set EnaSupport to true. As of February 2017, this applies to C5, I3, P2, R4,
X1, and m4.16xlarge.
2017-02-21 20:46:16 -05:00
|
|
|
// Set EnaSupport to true
|
|
|
|
// As of February 2017, this applies to C5, I3, P2, R4, X1, and m4.16xlarge
|
|
|
|
registerOpts.EnaSupport = aws.Bool(true)
|
2016-12-30 16:21:23 -05:00
|
|
|
}
|
|
|
|
registerResp, err := ec2conn.RegisterImage(registerOpts)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error registering AMI: %s", err))
|
|
|
|
ui.Error(state.Get("error").(error).Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the AMI ID in the state
|
|
|
|
ui.Say(fmt.Sprintf("AMI: %s", *registerResp.ImageId))
|
|
|
|
amis := make(map[string]string)
|
|
|
|
amis[*ec2conn.Config.Region] = *registerResp.ImageId
|
|
|
|
state.Put("amis", amis)
|
|
|
|
|
|
|
|
// Wait for the image to become ready
|
|
|
|
stateChange := awscommon.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "available",
|
|
|
|
Refresh: awscommon.AMIStateRefreshFunc(ec2conn, *registerResp.ImageId),
|
|
|
|
StepState: state,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Waiting for AMI to become ready...")
|
|
|
|
if _, err := awscommon.WaitForState(&stateChange); err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for AMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-02-27 08:51:38 -05:00
|
|
|
imagesResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ImageIds: []*string{registerResp.ImageId}})
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error searching for AMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
s.image = imagesResp.Images[0]
|
|
|
|
|
|
|
|
snapshots := make(map[string][]string)
|
|
|
|
for _, blockDeviceMapping := range imagesResp.Images[0].BlockDeviceMappings {
|
|
|
|
if blockDeviceMapping.Ebs != nil && blockDeviceMapping.Ebs.SnapshotId != nil {
|
|
|
|
|
|
|
|
snapshots[*ec2conn.Config.Region] = append(snapshots[*ec2conn.Config.Region], *blockDeviceMapping.Ebs.SnapshotId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.Put("snapshots", snapshots)
|
|
|
|
|
2016-12-30 16:21:23 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-02-27 08:51:38 -05:00
|
|
|
func (s *StepRegisterAMI) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.image == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2017-05-25 21:49:29 -04:00
|
|
|
ui.Say("Deregistering the AMI because cancellation or error...")
|
2017-02-27 08:51:38 -05:00
|
|
|
deregisterOpts := &ec2.DeregisterImageInput{ImageId: s.image.ImageId}
|
|
|
|
if _, err := ec2conn.DeregisterImage(deregisterOpts); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deregistering AMI, may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 19:12:22 -04:00
|
|
|
|
|
|
|
func DeduplicateRootVolume(BlockDevices []*ec2.BlockDeviceMapping, RootDevice RootBlockDevice, snapshotId string) []*ec2.BlockDeviceMapping {
|
|
|
|
// Defensive coding to make sure we only add the root volume once
|
|
|
|
blockDevicesExcludingRoot := make([]*ec2.BlockDeviceMapping, 0, len(BlockDevices))
|
|
|
|
for _, blockDevice := range BlockDevices {
|
|
|
|
if *blockDevice.DeviceName == RootDevice.SourceDeviceName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
blockDevicesExcludingRoot = append(blockDevicesExcludingRoot, blockDevice)
|
|
|
|
}
|
|
|
|
|
|
|
|
blockDevicesExcludingRoot = append(blockDevicesExcludingRoot, RootDevice.createBlockDeviceMapping(snapshotId))
|
|
|
|
return blockDevicesExcludingRoot
|
|
|
|
}
|