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 7 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
69 changes: 48 additions & 21 deletions builder/ebssurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,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 imageMethod multistep.Step
var volumeMethod multistep.Step
dwc0011 marked this conversation as resolved.
Show resolved Hide resolved

if b.config.RootDevice.ImageMethod != "create" {
volumeMethod = &StepSnapshotVolumes{
PollingConfig: b.config.PollingConfig,
LaunchDevices: launchDevices,
SnapshotOmitMap: b.config.LaunchMappings.GetOmissions(),
SnapshotTags: b.config.SnapshotTags,
Ctx: b.config.ctx,
}
imageMethod = &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,
}
} else {
volumeMethod = &StepSwapVolumes{
PollingConfig: b.config.PollingConfig,
RootDevice: b.config.RootDevice,
LaunchDevices: launchDevices,
LaunchOmitMap: b.config.LaunchMappings.GetOmissions(),
Ctx: b.config.ctx,
}

imageMethod = &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,
}
}

// Build the steps
steps := []multistep.Step{
&awscommon.StepPreValidate{
Expand Down Expand Up @@ -420,34 +466,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,
},
volumeMethod,
&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,
},
imageMethod,
&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_EbssurrogateImageMethodCreate(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(testBuilderAccImageMethodCreate, 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_EbssurrogateImageMethodRegister(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(testBuilderAccImageMethodRegister, 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_EbssurrogateImageMethodEmpty(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(testBuilderAccImageMethodEmpty, 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 testBuilderAccImageMethodCreate = `
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"
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"
image_method = "create"
dwc0011 marked this conversation as resolved.
Show resolved Hide resolved
}
}

build {
sources = ["amazon-ebssurrogate.test"]
}
`

const testBuilderAccImageMethodRegister = `
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"
image_method = "register"
}
}

build {
sources = ["amazon-ebssurrogate.test"]
}
`

const testBuilderAccImageMethodEmpty = `
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"]
}
`
6 changes: 6 additions & 0 deletions builder/ebssurrogate/root_block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type RootBlockDevice struct {
// The size of the volume, in GiB. Required if
// not specifying a snapshot_id.
VolumeSize int64 `mapstructure:"volume_size" required:"false"`
//Method used for image, create or register
dwc0011 marked this conversation as resolved.
Show resolved Hide resolved
ImageMethod string `mapstructure:"image_method" required:"false"`
}

func (c *RootBlockDevice) Prepare(ctx *interpolate.Context) []error {
Expand All @@ -61,6 +63,10 @@ func (c *RootBlockDevice) Prepare(ctx *interpolate.Context) []error {
errs = append(errs, errors.New("volume_size must be greater than 0"))
}

if c.ImageMethod != "" && c.ImageMethod != "create" && c.ImageMethod != "register" {
errs = append(errs, errors.New("image_method must be empty string or 'create' or 'register'"))
}

dwc0011 marked this conversation as resolved.
Show resolved Hide resolved
if len(errs) > 0 {
return errs
}
Expand Down
Loading
Loading