Add 'skip_create_ami' option to the amazon-ebs builder (#10531)

This commit is contained in:
Miguel Hernández 2021-01-27 15:00:42 +01:00 committed by GitHub
parent a5a1344948
commit 782cf058b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 11 deletions

View File

@ -24,6 +24,7 @@ type StepAMIRegionCopy struct {
toDelete string
getRegionConn func(*AccessConfig, string) (ec2iface.EC2API, error)
AMISkipCreateImage bool
AMISkipBuildRegion bool
}
@ -63,6 +64,12 @@ func (s *StepAMIRegionCopy) DeduplicateRegions(intermediary bool) {
func (s *StepAMIRegionCopy) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)
if s.AMISkipCreateImage {
ui.Say("Skipping AMI region copy...")
return multistep.ActionContinue
}
amis := state.Get("amis").(map[string]string)
snapshots := state.Get("snapshots").(map[string][]string)
intermediary, _ := state.Get("intermediary_image").(bool)

View File

@ -16,6 +16,8 @@ import (
)
type StepCreateTags struct {
AMISkipCreateImage bool
Tags map[string]string
SnapshotTags map[string]string
Ctx interpolate.Context
@ -25,6 +27,12 @@ func (s *StepCreateTags) Run(ctx context.Context, state multistep.StateBag) mult
ec2conn := state.Get("ec2").(*ec2.EC2)
session := state.Get("awsSession").(*session.Session)
ui := state.Get("ui").(packersdk.Ui)
if s.AMISkipCreateImage {
ui.Say("Skipping AMI create tags...")
return multistep.ActionContinue
}
amis := state.Get("amis").(map[string]string)
if len(s.Tags) == 0 && len(s.SnapshotTags) == 0 {

View File

@ -14,6 +14,8 @@ import (
)
type StepModifyAMIAttributes struct {
AMISkipCreateImage bool
Users []string
Groups []string
SnapshotUsers []string
@ -29,6 +31,12 @@ func (s *StepModifyAMIAttributes) Run(ctx context.Context, state multistep.State
ec2conn := state.Get("ec2").(*ec2.EC2)
session := state.Get("awsSession").(*session.Session)
ui := state.Get("ui").(packersdk.Ui)
if s.AMISkipCreateImage {
ui.Say("Skipping AMI modify attributes...")
return multistep.ActionContinue
}
amis := state.Get("amis").(map[string]string)
snapshots := state.Get("snapshots").(map[string][]string)

View File

@ -34,6 +34,9 @@ type Config struct {
awscommon.AccessConfig `mapstructure:",squash"`
awscommon.AMIConfig `mapstructure:",squash"`
awscommon.RunConfig `mapstructure:",squash"`
// If true, Packer will not create the AMI. Useful for setting to `true`
// during a build test stage. Default `false`.
AMISkipCreateImage bool `mapstructure:"skip_create_ami" required:"false"`
// Add one or more block device mappings to the AMI. These will be attached
// when booting a new instance from your AMI. To add a block device during
// the Packer build see `launch_block_device_mappings` below. Your options
@ -319,6 +322,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
Regions: b.config.AMIRegions,
},
&stepCreateAMI{
AMISkipCreateImage: b.config.AMISkipCreateImage,
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
PollingConfig: b.config.PollingConfig,
},
@ -330,22 +334,25 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
EncryptBootVolume: b.config.AMIEncryptBootVolume,
Name: b.config.AMIName,
OriginalRegion: *ec2conn.Config.Region,
AMISkipCreateImage: b.config.AMISkipCreateImage,
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
},
&awscommon.StepModifyAMIAttributes{
Description: b.config.AMIDescription,
Users: b.config.AMIUsers,
Groups: b.config.AMIGroups,
ProductCodes: b.config.AMIProductCodes,
SnapshotUsers: b.config.SnapshotUsers,
SnapshotGroups: b.config.SnapshotGroups,
Ctx: b.config.ctx,
GeneratedData: generatedData,
AMISkipCreateImage: b.config.AMISkipCreateImage,
Description: b.config.AMIDescription,
Users: b.config.AMIUsers,
Groups: b.config.AMIGroups,
ProductCodes: b.config.AMIProductCodes,
SnapshotUsers: b.config.SnapshotUsers,
SnapshotGroups: b.config.SnapshotGroups,
Ctx: b.config.ctx,
GeneratedData: generatedData,
},
&awscommon.StepCreateTags{
Tags: b.config.AMITags,
SnapshotTags: b.config.SnapshotTags,
Ctx: b.config.ctx,
AMISkipCreateImage: b.config.AMISkipCreateImage,
Tags: b.config.AMITags,
SnapshotTags: b.config.SnapshotTags,
Ctx: b.config.ctx,
},
}

View File

@ -142,6 +142,7 @@ type FlatConfig struct {
SSHInterface *string `mapstructure:"ssh_interface" cty:"ssh_interface" hcl:"ssh_interface"`
PauseBeforeSSM *string `mapstructure:"pause_before_ssm" cty:"pause_before_ssm" hcl:"pause_before_ssm"`
SessionManagerPort *int `mapstructure:"session_manager_port" cty:"session_manager_port" hcl:"session_manager_port"`
AMISkipCreateImage *bool `mapstructure:"skip_create_ami" required:"false" cty:"skip_create_ami" hcl:"skip_create_ami"`
AMIMappings []common.FlatBlockDevice `mapstructure:"ami_block_device_mappings" required:"false" cty:"ami_block_device_mappings" hcl:"ami_block_device_mappings"`
LaunchMappings []common.FlatBlockDevice `mapstructure:"launch_block_device_mappings" required:"false" cty:"launch_block_device_mappings" hcl:"launch_block_device_mappings"`
VolumeRunTags map[string]string `mapstructure:"run_volume_tags" cty:"run_volume_tags" hcl:"run_volume_tags"`
@ -291,6 +292,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"ssh_interface": &hcldec.AttrSpec{Name: "ssh_interface", Type: cty.String, Required: false},
"pause_before_ssm": &hcldec.AttrSpec{Name: "pause_before_ssm", Type: cty.String, Required: false},
"session_manager_port": &hcldec.AttrSpec{Name: "session_manager_port", Type: cty.Number, Required: false},
"skip_create_ami": &hcldec.AttrSpec{Name: "skip_create_ami", Type: cty.Bool, Required: false},
"ami_block_device_mappings": &hcldec.BlockListSpec{TypeName: "ami_block_device_mappings", Nested: hcldec.ObjectSpec((*common.FlatBlockDevice)(nil).HCL2Spec())},
"launch_block_device_mappings": &hcldec.BlockListSpec{TypeName: "launch_block_device_mappings", Nested: hcldec.ObjectSpec((*common.FlatBlockDevice)(nil).HCL2Spec())},
"run_volume_tags": &hcldec.AttrSpec{Name: "run_volume_tags", Type: cty.Map(cty.String), Required: false},

View File

@ -19,6 +19,7 @@ import (
type stepCreateAMI struct {
PollingConfig *awscommon.AWSPollingConfig
image *ec2.Image
AMISkipCreateImage bool
AMISkipBuildRegion bool
}
@ -28,6 +29,11 @@ func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multi
instance := state.Get("instance").(*ec2.Instance)
ui := state.Get("ui").(packersdk.Ui)
if s.AMISkipCreateImage {
ui.Say("Skipping AMI creation...")
return multistep.ActionContinue
}
// Create the image
amiName := config.AMIName
state.Put("intermediary_image", false)

View File

@ -1,5 +1,8 @@
<!-- Code generated from the comments of the Config struct in builder/amazon/ebs/builder.go; DO NOT EDIT MANUALLY -->
- `skip_create_ami` (bool) - If true, Packer will not create the AMI. Useful for setting to `true`
during a build test stage. Default `false`.
- `ami_block_device_mappings` (awscommon.BlockDevices) - Add one or more block device mappings to the AMI. These will be attached
when booting a new instance from your AMI. To add a block device during
the Packer build see `launch_block_device_mappings` below. Your options