Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to EBS Surrogate builder to persist properties from Marketplace source image, such as product codes #455

Merged
merged 20 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
dwc0011 marked this conversation as resolved.
Show resolved Hide resolved
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{
dwc0011 marked this conversation as resolved.
Show resolved Hide resolved
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" {
dwc0011 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading