add skip_save_build_region flag to fix naming conflicts when building for multiple regions
This commit is contained in:
parent
022b559c2f
commit
0d55bc46ee
|
@ -29,6 +29,7 @@ type AMIConfig struct {
|
||||||
SnapshotTags TagMap `mapstructure:"snapshot_tags"`
|
SnapshotTags TagMap `mapstructure:"snapshot_tags"`
|
||||||
SnapshotUsers []string `mapstructure:"snapshot_users"`
|
SnapshotUsers []string `mapstructure:"snapshot_users"`
|
||||||
SnapshotGroups []string `mapstructure:"snapshot_groups"`
|
SnapshotGroups []string `mapstructure:"snapshot_groups"`
|
||||||
|
AMISkipBuildRegion bool `mapstructure:"skip_save_build_region"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringInSlice(s []string, searchstr string) bool {
|
func stringInSlice(s []string, searchstr string) bool {
|
||||||
|
|
|
@ -21,8 +21,9 @@ type StepAMIRegionCopy struct {
|
||||||
Name string
|
Name string
|
||||||
OriginalRegion string
|
OriginalRegion string
|
||||||
|
|
||||||
toDelete string
|
toDelete string
|
||||||
getRegionConn func(*AccessConfig, string) (ec2iface.EC2API, error)
|
getRegionConn func(*AccessConfig, string) (ec2iface.EC2API, error)
|
||||||
|
AMISkipBuildRegion bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepAMIRegionCopy) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepAMIRegionCopy) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
|
@ -33,7 +34,9 @@ func (s *StepAMIRegionCopy) Run(ctx context.Context, state multistep.StateBag) m
|
||||||
ami := amis[s.OriginalRegion]
|
ami := amis[s.OriginalRegion]
|
||||||
// Always copy back into original region to preserve the ami name
|
// Always copy back into original region to preserve the ami name
|
||||||
s.toDelete = ami
|
s.toDelete = ami
|
||||||
s.Regions = append(s.Regions, s.OriginalRegion)
|
if !s.AMISkipBuildRegion {
|
||||||
|
s.Regions = append(s.Regions, s.OriginalRegion)
|
||||||
|
}
|
||||||
|
|
||||||
if s.EncryptBootVolume != nil && *s.EncryptBootVolume {
|
if s.EncryptBootVolume != nil && *s.EncryptBootVolume {
|
||||||
// encrypt_boot is true, so we have to copy the temporary
|
// encrypt_boot is true, so we have to copy the temporary
|
||||||
|
|
|
@ -18,8 +18,9 @@ import (
|
||||||
// the build before actually doing any time consuming work
|
// the build before actually doing any time consuming work
|
||||||
//
|
//
|
||||||
type StepPreValidate struct {
|
type StepPreValidate struct {
|
||||||
DestAmiName string
|
DestAmiName string
|
||||||
ForceDeregister bool
|
ForceDeregister bool
|
||||||
|
AMISkipBuildRegion bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
|
@ -76,6 +77,10 @@ func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) mul
|
||||||
ui.Say("Force Deregister flag found, skipping prevalidating AMI Name")
|
ui.Say("Force Deregister flag found, skipping prevalidating AMI Name")
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
if s.AMISkipBuildRegion {
|
||||||
|
ui.Say("skip_build_region was set; not prevalidating AMI name")
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
ec2conn := state.Get("ec2").(*ec2.EC2)
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
|
|
||||||
|
|
|
@ -154,8 +154,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&awscommon.StepPreValidate{
|
&awscommon.StepPreValidate{
|
||||||
DestAmiName: b.config.AMIName,
|
DestAmiName: b.config.AMIName,
|
||||||
ForceDeregister: b.config.AMIForceDeregister,
|
ForceDeregister: b.config.AMIForceDeregister,
|
||||||
|
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
|
||||||
},
|
},
|
||||||
&awscommon.StepSourceAMIInfo{
|
&awscommon.StepSourceAMIInfo{
|
||||||
SourceAmi: b.config.SourceAmi,
|
SourceAmi: b.config.SourceAmi,
|
||||||
|
@ -220,15 +221,18 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
AMIName: b.config.AMIName,
|
AMIName: b.config.AMIName,
|
||||||
Regions: b.config.AMIRegions,
|
Regions: b.config.AMIRegions,
|
||||||
},
|
},
|
||||||
&stepCreateAMI{},
|
&stepCreateAMI{
|
||||||
|
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
|
||||||
|
},
|
||||||
&awscommon.StepAMIRegionCopy{
|
&awscommon.StepAMIRegionCopy{
|
||||||
AccessConfig: &b.config.AccessConfig,
|
AccessConfig: &b.config.AccessConfig,
|
||||||
Regions: b.config.AMIRegions,
|
Regions: b.config.AMIRegions,
|
||||||
AMIKmsKeyId: b.config.AMIKmsKeyId,
|
AMIKmsKeyId: b.config.AMIKmsKeyId,
|
||||||
RegionKeyIds: b.config.AMIRegionKMSKeyIDs,
|
RegionKeyIds: b.config.AMIRegionKMSKeyIDs,
|
||||||
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
||||||
Name: b.config.AMIName,
|
Name: b.config.AMIName,
|
||||||
OriginalRegion: *ec2conn.Config.Region,
|
OriginalRegion: *ec2conn.Config.Region,
|
||||||
|
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
|
||||||
},
|
},
|
||||||
&awscommon.StepModifyAMIAttributes{
|
&awscommon.StepModifyAMIAttributes{
|
||||||
Description: b.config.AMIDescription,
|
Description: b.config.AMIDescription,
|
||||||
|
|
|
@ -14,7 +14,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type stepCreateAMI struct {
|
type stepCreateAMI struct {
|
||||||
image *ec2.Image
|
image *ec2.Image
|
||||||
|
AMISkipBuildRegion bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
|
@ -25,9 +26,9 @@ func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multi
|
||||||
|
|
||||||
// Create the image
|
// Create the image
|
||||||
amiName := config.AMIName
|
amiName := config.AMIName
|
||||||
if config.AMIEncryptBootVolume != nil {
|
if config.AMIEncryptBootVolume != nil || s.AMISkipBuildRegion {
|
||||||
// encrypt_boot was set, so we will create a temporary image
|
// Create a temporary image and then create a copy of it with the
|
||||||
// and then create a copy of it with the correct encrypt_boot
|
// correct encrypt_boot
|
||||||
amiName = random.AlphaNum(7)
|
amiName = random.AlphaNum(7)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue