@ -21,61 +21,19 @@ type StepRegisterAMI struct {
func ( s * StepRegisterAMI ) Run ( ctx context . Context , state multistep . StateBag ) multistep . StepAction {
config := state . Get ( "config" ) . ( * Config )
ec2conn := state . Get ( "ec2" ) . ( * ec2 . EC2 )
snapshotI d := state . Get ( "snapshot_id" ) . ( string )
snapshotI D := state . Get ( "snapshot_id" ) . ( string )
ui := state . Get ( "ui" ) . ( packer . Ui )
ui . Say ( "Registering the AMI..." )
var (
registerOpts * ec2 . RegisterImageInput
mappings [ ] * ec2 . BlockDeviceMapping
image * ec2 . Image
rootDeviceName string
)
var registerOpts * ec2 . RegisterImageInput
// Source Image is only required to be passed if the image is not from scratch
if config . FromScratch {
mappings = config . AMIBlockDevices . BuildAMIDevices ( )
rootDeviceName = config . RootDeviceName
registerOpts = buildBaseRegisterOpts ( config , nil , s . RootVolumeSize , snapshotID )
} else {
image = state . Get ( "source_image" ) . ( * ec2 . Image )
mappings = image . BlockDeviceMappings
rootDeviceName = * image . RootDeviceName
}
newMappings := make ( [ ] * ec2 . BlockDeviceMapping , len ( mappings ) )
for i , device := range mappings {
newDevice := device
if * newDevice . DeviceName == rootDeviceName {
if newDevice . Ebs != nil {
newDevice . Ebs . SnapshotId = aws . String ( snapshotId )
} else {
newDevice . Ebs = & ec2 . EbsBlockDevice { SnapshotId : aws . String ( snapshotId ) }
}
if config . FromScratch || s . RootVolumeSize > * newDevice . Ebs . VolumeSize {
newDevice . Ebs . VolumeSize = aws . Int64 ( s . RootVolumeSize )
}
}
// assume working from a snapshot, so we unset the Encrypted field if set,
// otherwise AWS API will return InvalidParameter
if newDevice . Ebs != nil && newDevice . Ebs . Encrypted != nil {
newDevice . Ebs . Encrypted = nil
}
newMappings [ i ] = newDevice
}
if config . FromScratch {
registerOpts = & ec2 . RegisterImageInput {
Name : & config . AMIName ,
Architecture : aws . String ( ec2 . ArchitectureValuesX8664 ) ,
RootDeviceName : aws . String ( rootDeviceName ) ,
VirtualizationType : aws . String ( config . AMIVirtType ) ,
BlockDeviceMappings : newMappings ,
}
} else {
registerOpts = buildRegisterOpts ( config , image , newMappings )
image := state . Get ( "source_image" ) . ( * ec2 . Image )
registerOpts = buildBaseRegisterOpts ( config , image , s . RootVolumeSize , snapshotID )
}
if s . EnableAMISriovNetSupport {
@ -115,6 +73,59 @@ func (s *StepRegisterAMI) Run(ctx context.Context, state multistep.StateBag) mul
func ( s * StepRegisterAMI ) Cleanup ( state multistep . StateBag ) { }
// Builds the base register opts with architecture, name, root block device, mappings, virtualizationtype
func buildBaseRegisterOpts ( config * Config , sourceImage * ec2 . Image , rootVolumeSize int64 , snapshotID string ) * ec2 . RegisterImageInput {
var (
mappings [ ] * ec2 . BlockDeviceMapping
rootDeviceName string
)
if config . FromScratch {
mappings = config . AMIBlockDevices . BuildAMIDevices ( )
rootDeviceName = config . RootDeviceName
} else {
// If config.FromScratch is false, source image must be set
mappings = sourceImage . BlockDeviceMappings
rootDeviceName = * sourceImage . RootDeviceName
}
newMappings := make ( [ ] * ec2 . BlockDeviceMapping , len ( mappings ) )
for i , device := range mappings {
newDevice := device
if * newDevice . DeviceName == rootDeviceName {
if newDevice . Ebs != nil {
newDevice . Ebs . SnapshotId = aws . String ( snapshotID )
} else {
newDevice . Ebs = & ec2 . EbsBlockDevice { SnapshotId : aws . String ( snapshotID ) }
}
if config . FromScratch || rootVolumeSize > * newDevice . Ebs . VolumeSize {
newDevice . Ebs . VolumeSize = aws . Int64 ( rootVolumeSize )
}
}
// assume working from a snapshot, so we unset the Encrypted field if set,
// otherwise AWS API will return InvalidParameter
if newDevice . Ebs != nil && newDevice . Ebs . Encrypted != nil {
newDevice . Ebs . Encrypted = nil
}
newMappings [ i ] = newDevice
}
if config . FromScratch {
return & ec2 . RegisterImageInput {
Name : & config . AMIName ,
Architecture : aws . String ( ec2 . ArchitectureValuesX8664 ) ,
RootDeviceName : aws . String ( rootDeviceName ) ,
VirtualizationType : aws . String ( config . AMIVirtType ) ,
BlockDeviceMappings : newMappings ,
}
}
return buildRegisterOpts ( config , sourceImage , newMappings )
}
func buildRegisterOpts ( config * Config , image * ec2 . Image , mappings [ ] * ec2 . BlockDeviceMapping ) * ec2 . RegisterImageInput {
registerOpts := & ec2 . RegisterImageInput {
Name : & config . AMIName ,