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"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-12-30 16:21:23 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepRegisterAMI creates the AMI.
|
|
|
|
type StepRegisterAMI struct {
|
|
|
|
RootDevice RootBlockDevice
|
|
|
|
BlockDevices []*ec2.BlockDeviceMapping
|
2017-02-27 08:51:38 -05:00
|
|
|
image *ec2.Image
|
2016-12-30 16:21:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
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-02-27 01:40:07 -05:00
|
|
|
blockDevicesExcludingRoot := make([]*ec2.BlockDeviceMapping, 0, len(s.BlockDevices)-1)
|
|
|
|
for _, blockDevice := range s.BlockDevices {
|
|
|
|
if *blockDevice.DeviceName == s.RootDevice.SourceDeviceName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
blockDevicesExcludingRoot = append(blockDevicesExcludingRoot, blockDevice)
|
|
|
|
}
|
|
|
|
|
|
|
|
blockDevicesExcludingRoot = append(blockDevicesExcludingRoot, s.RootDevice.createBlockDeviceMapping(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
|
|
|
}
|
|
|
|
|
|
|
|
if config.AMIEnhancedNetworking {
|
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")
|
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)
|
|
|
|
|
|
|
|
ui.Say("Deregistering the AMI because cancelation or error...")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|