Skip to content

Commit

Permalink
Add ability to EBS Surrogate builder to persist properties from Marke…
Browse files Browse the repository at this point in the history
…tplace source image, such as product codes (#455)

* Add swap volume ebssurrogate option when using create image method, otherwise snapshot and register

* Detach omitted and root devices, pass in all block devices and combine

* Normalize case of error messages to be consistent with rest of project

* Fix the comment, remove copy pasta and add what the swap volumes step does.

* Add more in-depth comment about what actually happens when using Create or Register

* Refactor to prevent the duplicate check of the empty string

* Based on feedback from @lorengordon, Update comment to be more explicit and reference the APIs it utilizes.

---------

Co-authored-by: Dennis Carey <[email protected]>
Co-authored-by: Wilken Rivera <[email protected]>
  • Loading branch information
3 people authored Mar 4, 2024
1 parent 0348575 commit 2aebcef
Show file tree
Hide file tree
Showing 7 changed files with 599 additions and 21 deletions.
9 changes: 9 additions & 0 deletions .web-docs/components/builder/ebssurrogate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ necessary for this build to succeed and can be found further down the page.
[NitroTPM Support](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enable-nitrotpm-support-on-ami.html) for
more information. Only enabled if a valid option is provided, otherwise ignored.

- `use_create_image` (bool) - Whether to use the CreateImage or RegisterImage API when creating the AMI.
When set to `true`, the CreateImage API is used and will create the image
from the instance itself, and inherit properties from the instance.
When set to `false`, the RegisterImage API is used and the image is created using
a snapshot of the specified EBS volume, and no properties are inherited from the instance.
Defaults to `false`.
Ref: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateImage.html
https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_RegisterImage.html

<!-- End of code generated from the comments of the Config struct in builder/ebssurrogate/builder.go; -->


Expand Down
78 changes: 57 additions & 21 deletions builder/ebssurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ type Config struct {
// [NitroTPM Support](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/enable-nitrotpm-support-on-ami.html) for
// more information. Only enabled if a valid option is provided, otherwise ignored.
TpmSupport string `mapstructure:"tpm_support" required:"false"`
// Whether to use the CreateImage or RegisterImage API when creating the AMI.
// When set to `true`, the CreateImage API is used and will create the image
// from the instance itself, and inherit properties from the instance.
// When set to `false`, the RegisterImage API is used and the image is created using
// a snapshot of the specified EBS volume, and no properties are inherited from the instance.
// Defaults to `false`.
//Ref: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateImage.html
// https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_RegisterImage.html
UseCreateImage bool `mapstructure:"use_create_image" required:"false"`

ctx interpolate.Context
}
Expand Down Expand Up @@ -318,6 +327,52 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
amiDevices := b.config.AMIMappings.BuildEC2BlockDeviceMappings()
launchDevices := b.config.LaunchMappings.BuildEC2BlockDeviceMappings()

var buildAmiStep multistep.Step
var volumeStep multistep.Step

if b.config.UseCreateImage {
volumeStep = &StepSwapVolumes{
PollingConfig: b.config.PollingConfig,
RootDevice: b.config.RootDevice,
LaunchDevices: launchDevices,
LaunchOmitMap: b.config.LaunchMappings.GetOmissions(),
Ctx: b.config.ctx,
}

buildAmiStep = &StepCreateAMI{
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
RootDevice: b.config.RootDevice,
AMIDevices: amiDevices,
LaunchDevices: launchDevices,
PollingConfig: b.config.PollingConfig,
IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(),
Tags: b.config.RunTags,
Ctx: b.config.ctx,
}
} else {
volumeStep = &StepSnapshotVolumes{
PollingConfig: b.config.PollingConfig,
LaunchDevices: launchDevices,
SnapshotOmitMap: b.config.LaunchMappings.GetOmissions(),
SnapshotTags: b.config.SnapshotTags,
Ctx: b.config.ctx,
}
buildAmiStep = &StepRegisterAMI{
RootDevice: b.config.RootDevice,
AMIDevices: amiDevices,
LaunchDevices: launchDevices,
EnableAMISriovNetSupport: b.config.AMISriovNetSupport,
EnableAMIENASupport: b.config.AMIENASupport,
Architecture: b.config.Architecture,
LaunchOmitMap: b.config.LaunchMappings.GetOmissions(),
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
PollingConfig: b.config.PollingConfig,
BootMode: b.config.BootMode,
UefiData: b.config.UefiData,
TpmSupport: b.config.TpmSupport,
}
}

// Build the steps
steps := []multistep.Step{
&awscommon.StepPreValidate{
Expand Down Expand Up @@ -420,34 +475,15 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
EnableAMISriovNetSupport: b.config.AMISriovNetSupport,
EnableAMIENASupport: b.config.AMIENASupport,
},
&StepSnapshotVolumes{
PollingConfig: b.config.PollingConfig,
LaunchDevices: launchDevices,
SnapshotOmitMap: b.config.LaunchMappings.GetOmissions(),
SnapshotTags: b.config.SnapshotTags,
Ctx: b.config.ctx,
},
volumeStep,
&awscommon.StepDeregisterAMI{
AccessConfig: &b.config.AccessConfig,
ForceDeregister: b.config.AMIForceDeregister,
ForceDeleteSnapshot: b.config.AMIForceDeleteSnapshot,
AMIName: b.config.AMIName,
Regions: b.config.AMIRegions,
},
&StepRegisterAMI{
RootDevice: b.config.RootDevice,
AMIDevices: amiDevices,
LaunchDevices: launchDevices,
EnableAMISriovNetSupport: b.config.AMISriovNetSupport,
EnableAMIENASupport: b.config.AMIENASupport,
Architecture: b.config.Architecture,
LaunchOmitMap: b.config.LaunchMappings.GetOmissions(),
AMISkipBuildRegion: b.config.AMISkipBuildRegion,
PollingConfig: b.config.PollingConfig,
BootMode: b.config.BootMode,
UefiData: b.config.UefiData,
TpmSupport: b.config.TpmSupport,
},
buildAmiStep,
&awscommon.StepAMIRegionCopy{
AccessConfig: &b.config.AccessConfig,
Regions: b.config.AMIRegions,
Expand Down
2 changes: 2 additions & 0 deletions builder/ebssurrogate/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

155 changes: 155 additions & 0 deletions builder/ebssurrogate/builder_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,75 @@ func TestAccBuilder_Ebssurrogate_SSHPrivateKeyFile_SSM(t *testing.T) {
acctest.TestPlugin(t, testcase)
}

func TestAccBuilder_EbssurrogateUseCreateImageTrue(t *testing.T) {
ami := amazon_acc.AMIHelper{
Region: "us-east-1",
Name: "ebs-image-method-create-acc-test",
}
testCase := &acctest.PluginTestCase{
Name: "amazon-ebssurrogate_image_method_create_test",
Template: fmt.Sprintf(testBuilderAccUseCreateImageTrue, ami.Name),
Teardown: func() error {
return ami.CleanUpAmi()
},
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
return nil
},
}
acctest.TestPlugin(t, testCase)
}

func TestAccBuilder_EbssurrogateUseCreateImageFalse(t *testing.T) {
ami := amazon_acc.AMIHelper{
Region: "us-east-1",
Name: "ebs-image-method-register-acc-test",
}
testCase := &acctest.PluginTestCase{
Name: "amazon-ebssurrogate_image_method_register_test",
Template: fmt.Sprintf(testBuilderAccUseCreateImageFalse, ami.Name),
Teardown: func() error {
return ami.CleanUpAmi()
},
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
return nil
},
}
acctest.TestPlugin(t, testCase)
}

func TestAccBuilder_EbssurrogateUseCreateImageOptional(t *testing.T) {
ami := amazon_acc.AMIHelper{
Region: "us-east-1",
Name: "ebs-image-method-empty-acc-test",
}
testCase := &acctest.PluginTestCase{
Name: "amazon-ebssurrogate_image_method_empty_test",
Template: fmt.Sprintf(testBuilderAccUseCreateImageOptional, ami.Name),
Teardown: func() error {
return ami.CleanUpAmi()
},
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
return nil
},
}
acctest.TestPlugin(t, testCase)
}

const testBuilderAccBasic = `
source "amazon-ebssurrogate" "test" {
ami_name = "%s"
Expand Down Expand Up @@ -197,3 +266,89 @@ build {
sources = ["amazon-ebssurrogate.test"]
}
`

const testBuilderAccUseCreateImageTrue = `
source "amazon-ebssurrogate" "test" {
ami_name = "%s"
region = "us-east-1"
instance_type = "m3.medium"
source_ami = "ami-76b2a71e"
ssh_username = "ubuntu"
use_create_image = true
launch_block_device_mappings {
device_name = "/dev/xvda"
delete_on_termination = true
volume_size = 8
volume_type = "gp2"
}
ami_virtualization_type = "hvm"
ami_root_device {
source_device_name = "/dev/xvda"
device_name = "/dev/sda1"
delete_on_termination = true
volume_size = 8
volume_type = "gp2"
}
}
build {
sources = ["amazon-ebssurrogate.test"]
}
`

const testBuilderAccUseCreateImageFalse = `
source "amazon-ebssurrogate" "test" {
ami_name = "%s"
region = "us-east-1"
instance_type = "m3.medium"
source_ami = "ami-76b2a71e"
ssh_username = "ubuntu"
use_create_image = false
launch_block_device_mappings {
device_name = "/dev/xvda"
delete_on_termination = true
volume_size = 8
volume_type = "gp2"
}
ami_virtualization_type = "hvm"
ami_root_device {
source_device_name = "/dev/xvda"
device_name = "/dev/sda1"
delete_on_termination = true
volume_size = 8
volume_type = "gp2"
}
}
build {
sources = ["amazon-ebssurrogate.test"]
}
`

const testBuilderAccUseCreateImageOptional = `
source "amazon-ebssurrogate" "test" {
ami_name = "%s"
region = "us-east-1"
instance_type = "m3.medium"
source_ami = "ami-76b2a71e"
ssh_username = "ubuntu"
launch_block_device_mappings {
device_name = "/dev/xvda"
delete_on_termination = true
volume_size = 8
volume_type = "gp2"
}
ami_virtualization_type = "hvm"
ami_root_device {
source_device_name = "/dev/xvda"
device_name = "/dev/sda1"
delete_on_termination = true
volume_size = 8
volume_type = "gp2"
}
}
build {
sources = ["amazon-ebssurrogate.test"]
}
`
Loading

0 comments on commit 2aebcef

Please sign in to comment.