From ec33255ad5b6eaeae8a4bbccf0070a1d67982680 Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Tue, 17 Sep 2024 11:57:38 +0000 Subject: [PATCH] Fix bsu builder --- builder/bsu/builder.hcl2spec.go | 2 + builder/bsu/step_create_omi.go | 29 +++++-- builder/bsusurrogate/builder.hcl2spec.go | 2 + builder/bsusurrogate/step_register_omi.go | 3 +- builder/chroot/builder.hcl2spec.go | 4 +- builder/common/block_device.go | 95 ++++++++--------------- builder/common/omi_config.go | 1 + builder/common/step_update_omi.go | 6 +- 8 files changed, 65 insertions(+), 77 deletions(-) diff --git a/builder/bsu/builder.hcl2spec.go b/builder/bsu/builder.hcl2spec.go index 38a76d39..ffc99a26 100644 --- a/builder/bsu/builder.hcl2spec.go +++ b/builder/bsu/builder.hcl2spec.go @@ -44,6 +44,7 @@ type FlatConfig struct { SnapshotAccountIDs []string `mapstructure:"snapshot_account_ids" cty:"snapshot_account_ids" hcl:"snapshot_account_ids"` GlobalPermission *bool `mapstructure:"global_permission" cty:"global_permission" hcl:"global_permission"` ProductCodes []string `mapstructure:"product_codes" cty:"product_codes" hcl:"product_codes"` + RootDeviceName *string `mapstructure:"root_device_name" cty:"root_device_name" hcl:"root_device_name"` OMIMappings []common.FlatBlockDevice `mapstructure:"omi_block_device_mappings" cty:"omi_block_device_mappings" hcl:"omi_block_device_mappings"` LaunchMappings []common.FlatBlockDevice `mapstructure:"launch_block_device_mappings" cty:"launch_block_device_mappings" hcl:"launch_block_device_mappings"` AssociatePublicIpAddress *bool `mapstructure:"associate_public_ip_address" cty:"associate_public_ip_address" hcl:"associate_public_ip_address"` @@ -168,6 +169,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "snapshot_account_ids": &hcldec.AttrSpec{Name: "snapshot_account_ids", Type: cty.List(cty.String), Required: false}, "global_permission": &hcldec.AttrSpec{Name: "global_permission", Type: cty.Bool, Required: false}, "product_codes": &hcldec.AttrSpec{Name: "product_codes", Type: cty.List(cty.String), Required: false}, + "root_device_name": &hcldec.AttrSpec{Name: "root_device_name", Type: cty.String, Required: false}, "omi_block_device_mappings": &hcldec.BlockListSpec{TypeName: "omi_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())}, "associate_public_ip_address": &hcldec.AttrSpec{Name: "associate_public_ip_address", Type: cty.Bool, Required: false}, diff --git a/builder/bsu/step_create_omi.go b/builder/bsu/step_create_omi.go index 2a78dc35..1503aed5 100644 --- a/builder/bsu/step_create_omi.go +++ b/builder/bsu/step_create_omi.go @@ -29,15 +29,30 @@ func (s *stepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi ui.Say(fmt.Sprintf("Creating OMI %s from vm %s", omiName, vm.GetVmId())) blockDeviceMapping := config.BlockDevices.BuildOscOMIDevices() createOpts := oscgo.CreateImageRequest{ - VmId: vm.VmId, - ImageName: &omiName, - BlockDeviceMappings: &blockDeviceMapping, + ImageName: &omiName, } - if config.OMIDescription != "" { - createOpts.Description = &config.OMIDescription + if len(blockDeviceMapping) == 0 { + createOpts.SetVmId(vm.GetVmId()) + } else { + createOpts.SetBlockDeviceMappings(blockDeviceMapping) + if rootDName := config.RootDeviceName; rootDName != "" { + createOpts.SetRootDeviceName(rootDName) + } else { + err := fmt.Errorf("Error: MissingParameter: You must provide 'RootDeviceName' when creating omi with 'omi_block_device_mappings'.") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + } + if prodCode := config.ProductCodes; prodCode != nil { + createOpts.SetProductCodes(prodCode) + } + + if description := config.OMIDescription; description != "" { + createOpts.SetDescription(description) } - if config.ProductCodes != nil { - createOpts.ProductCodes = &config.ProductCodes + if prodCode := config.ProductCodes; prodCode != nil { + createOpts.SetProductCodes(prodCode) } resp, _, err := oscconn.Api.ImageApi.CreateImage(oscconn.Auth).CreateImageRequest(createOpts).Execute() diff --git a/builder/bsusurrogate/builder.hcl2spec.go b/builder/bsusurrogate/builder.hcl2spec.go index e8d36c70..826e0001 100644 --- a/builder/bsusurrogate/builder.hcl2spec.go +++ b/builder/bsusurrogate/builder.hcl2spec.go @@ -119,6 +119,7 @@ type FlatConfig struct { SnapshotAccountIDs []string `mapstructure:"snapshot_account_ids" cty:"snapshot_account_ids" hcl:"snapshot_account_ids"` GlobalPermission *bool `mapstructure:"global_permission" cty:"global_permission" hcl:"global_permission"` ProductCodes []string `mapstructure:"product_codes" cty:"product_codes" hcl:"product_codes"` + RootDeviceName *string `mapstructure:"root_device_name" cty:"root_device_name" hcl:"root_device_name"` RootDevice *FlatRootBlockDevice `mapstructure:"omi_root_device" cty:"omi_root_device" hcl:"omi_root_device"` VolumeRunTags common.TagMap `mapstructure:"run_volume_tags" cty:"run_volume_tags" hcl:"run_volume_tags"` } @@ -243,6 +244,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "snapshot_account_ids": &hcldec.AttrSpec{Name: "snapshot_account_ids", Type: cty.List(cty.String), Required: false}, "global_permission": &hcldec.AttrSpec{Name: "global_permission", Type: cty.Bool, Required: false}, "product_codes": &hcldec.AttrSpec{Name: "product_codes", Type: cty.List(cty.String), Required: false}, + "root_device_name": &hcldec.AttrSpec{Name: "root_device_name", Type: cty.String, Required: false}, "omi_root_device": &hcldec.BlockSpec{TypeName: "omi_root_device", Nested: hcldec.ObjectSpec((*FlatRootBlockDevice)(nil).HCL2Spec())}, "run_volume_tags": &hcldec.AttrSpec{Name: "run_volume_tags", Type: cty.Map(cty.String), Required: false}, } diff --git a/builder/bsusurrogate/step_register_omi.go b/builder/bsusurrogate/step_register_omi.go index 42317d5b..79da21cb 100644 --- a/builder/bsusurrogate/step_register_omi.go +++ b/builder/bsusurrogate/step_register_omi.go @@ -153,8 +153,7 @@ func (s *StepRegisterOMI) combineDevices(snapshotIDs map[string]string) []oscgo. func copyToDeviceMappingImage(device osc.BlockDeviceMappingVmCreation) oscgo.BlockDeviceMappingImage { log.Printf("Copy device mapping image ") deviceImage := oscgo.BlockDeviceMappingImage{ - DeviceName: device.DeviceName, - VirtualDeviceName: device.VirtualDeviceName, + DeviceName: device.DeviceName, Bsu: &oscgo.BsuToCreate{ DeleteOnVmDeletion: device.Bsu.DeleteOnVmDeletion, Iops: device.Bsu.Iops, diff --git a/builder/chroot/builder.hcl2spec.go b/builder/chroot/builder.hcl2spec.go index 8c7a0fb5..c22be80a 100644 --- a/builder/chroot/builder.hcl2spec.go +++ b/builder/chroot/builder.hcl2spec.go @@ -34,6 +34,7 @@ type FlatConfig struct { SnapshotAccountIDs []string `mapstructure:"snapshot_account_ids" cty:"snapshot_account_ids" hcl:"snapshot_account_ids"` GlobalPermission *bool `mapstructure:"global_permission" cty:"global_permission" hcl:"global_permission"` ProductCodes []string `mapstructure:"product_codes" cty:"product_codes" hcl:"product_codes"` + RootDeviceName *string `mapstructure:"root_device_name" cty:"root_device_name" hcl:"root_device_name"` AccessKey *string `mapstructure:"access_key" cty:"access_key" hcl:"access_key"` CustomEndpointOAPI *string `mapstructure:"custom_endpoint_oapi" cty:"custom_endpoint_oapi" hcl:"custom_endpoint_oapi"` InsecureSkipTLSVerify *bool `mapstructure:"insecure_skip_tls_verify" cty:"insecure_skip_tls_verify" hcl:"insecure_skip_tls_verify"` @@ -56,7 +57,6 @@ type FlatConfig struct { MountPath *string `mapstructure:"mount_path" cty:"mount_path" hcl:"mount_path"` PostMountCommands []string `mapstructure:"post_mount_commands" cty:"post_mount_commands" hcl:"post_mount_commands"` PreMountCommands []string `mapstructure:"pre_mount_commands" cty:"pre_mount_commands" hcl:"pre_mount_commands"` - RootDeviceName *string `mapstructure:"root_device_name" cty:"root_device_name" hcl:"root_device_name"` RootVolumeSize *int64 `mapstructure:"root_volume_size" cty:"root_volume_size" hcl:"root_volume_size"` RootVolumeType *string `mapstructure:"root_volume_type" cty:"root_volume_type" hcl:"root_volume_type"` SourceOMI *string `mapstructure:"source_omi" cty:"source_omi" hcl:"source_omi"` @@ -99,6 +99,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "snapshot_account_ids": &hcldec.AttrSpec{Name: "snapshot_account_ids", Type: cty.List(cty.String), Required: false}, "global_permission": &hcldec.AttrSpec{Name: "global_permission", Type: cty.Bool, Required: false}, "product_codes": &hcldec.AttrSpec{Name: "product_codes", Type: cty.List(cty.String), Required: false}, + "root_device_name": &hcldec.AttrSpec{Name: "root_device_name", Type: cty.String, Required: false}, "access_key": &hcldec.AttrSpec{Name: "access_key", Type: cty.String, Required: false}, "custom_endpoint_oapi": &hcldec.AttrSpec{Name: "custom_endpoint_oapi", Type: cty.String, Required: false}, "insecure_skip_tls_verify": &hcldec.AttrSpec{Name: "insecure_skip_tls_verify", Type: cty.Bool, Required: false}, @@ -121,7 +122,6 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "mount_path": &hcldec.AttrSpec{Name: "mount_path", Type: cty.String, Required: false}, "post_mount_commands": &hcldec.AttrSpec{Name: "post_mount_commands", Type: cty.List(cty.String), Required: false}, "pre_mount_commands": &hcldec.AttrSpec{Name: "pre_mount_commands", Type: cty.List(cty.String), Required: false}, - "root_device_name": &hcldec.AttrSpec{Name: "root_device_name", Type: cty.String, Required: false}, "root_volume_size": &hcldec.AttrSpec{Name: "root_volume_size", Type: cty.Number, Required: false}, "root_volume_type": &hcldec.AttrSpec{Name: "root_volume_type", Type: cty.String, Required: false}, "source_omi": &hcldec.AttrSpec{Name: "source_omi", Type: cty.String, Required: false}, diff --git a/builder/common/block_device.go b/builder/common/block_device.go index 4c37de6b..fc3da06a 100644 --- a/builder/common/block_device.go +++ b/builder/common/block_device.go @@ -2,8 +2,6 @@ package common import ( "fmt" - "log" - "strings" "github.com/aws/aws-sdk-go/aws" "github.com/hashicorp/packer-plugin-sdk/template/interpolate" @@ -35,43 +33,39 @@ type LaunchBlockDevices struct { LaunchMappings []BlockDevice `mapstructure:"launch_block_device_mappings"` } -func buildOscBlockDevicesImage(b []BlockDevice) []oscgo.BlockDeviceMappingImage { - var blockDevices []oscgo.BlockDeviceMappingImage - - for _, blockDevice := range b { - mapping := oscgo.BlockDeviceMappingImage{ - DeviceName: &blockDevice.DeviceName, - } - - if blockDevice.VirtualName != "" { - if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") { - mapping.VirtualDeviceName = &blockDevice.VirtualName - } - } else { - bsu := oscgo.BsuToCreate{ - DeleteOnVmDeletion: &blockDevice.DeleteOnVmDeletion, - } +func setBsuToCreate(blockDevice BlockDevice) oscgo.BsuToCreate { + bsu := oscgo.NewBsuToCreate() - if blockDevice.VolumeType != "" { - bsu.VolumeType = &blockDevice.VolumeType - } + if deleteOnVmDeletion := blockDevice.DeleteOnVmDeletion; !deleteOnVmDeletion { + bsu.SetDeleteOnVmDeletion(deleteOnVmDeletion) + } + if volType := blockDevice.VolumeType; volType != "" { + bsu.SetVolumeType(volType) + } + if volSize := blockDevice.VolumeSize; volSize > 0 { + bsu.SetVolumeSize(int32(blockDevice.VolumeSize)) - if blockDevice.VolumeSize > 0 { - bsu.VolumeSize = oscgo.PtrInt32(int32(blockDevice.VolumeSize)) - } + } + // IOPS is only valid for io1 type + if blockDevice.VolumeType == "io1" { + bsu.SetIops(int32(blockDevice.IOPS)) + } + if snapId := blockDevice.SnapshotId; snapId != "" { + bsu.SetSnapshotId(snapId) + } - // IOPS is only valid for io1 type - if blockDevice.VolumeType == "io1" { - bsu.Iops = oscgo.PtrInt32(int32(blockDevice.IOPS)) - } + return *bsu +} - if blockDevice.SnapshotId != "" { - bsu.SnapshotId = &blockDevice.SnapshotId - } +func buildOscBlockDevicesImage(b []BlockDevice) []oscgo.BlockDeviceMappingImage { + var blockDevices []oscgo.BlockDeviceMappingImage + for _, blockDevice := range b { + mapping := oscgo.BlockDeviceMappingImage{} - mapping.Bsu = &bsu + if deviceName := blockDevice.DeviceName; deviceName != "" { + mapping.SetDeviceName(deviceName) } - + mapping.SetBsu(setBsuToCreate(blockDevice)) blockDevices = append(blockDevices, mapping) } return blockDevices @@ -83,41 +77,16 @@ func buildOscBlockDevicesVmCreation(b []BlockDevice) []oscgo.BlockDeviceMappingV var blockDevices []oscgo.BlockDeviceMappingVmCreation for _, blockDevice := range b { - mapping := oscgo.BlockDeviceMappingVmCreation{ - DeviceName: &blockDevice.DeviceName, + mapping := oscgo.BlockDeviceMappingVmCreation{} + + if deviceName := blockDevice.DeviceName; deviceName != "" { + mapping.SetDeviceName(deviceName) } if blockDevice.NoDevice { mapping.NoDevice = aws.String("") - //blockDevices = mapping[0] - } else if blockDevice.VirtualName != "" { - if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") { - mapping.VirtualDeviceName = &blockDevice.VirtualName - } } else { - bsu := oscgo.BsuToCreate{ - DeleteOnVmDeletion: &blockDevice.DeleteOnVmDeletion, - } - - if blockDevice.VolumeType != "" { - bsu.VolumeType = &blockDevice.VolumeType - } - - if blockDevice.VolumeSize > 0 { - bsu.VolumeSize = oscgo.PtrInt32(int32(blockDevice.VolumeSize)) - - } - - // IOPS is only valid for io1 type - if blockDevice.VolumeType == "io1" { - bsu.Iops = oscgo.PtrInt32(int32(blockDevice.IOPS)) - } - - if blockDevice.SnapshotId != "" { - bsu.SnapshotId = &blockDevice.SnapshotId - } - - mapping.Bsu = &bsu + mapping.SetBsu(setBsuToCreate(blockDevice)) } log.Printf("block device mapping") diff --git a/builder/common/omi_config.go b/builder/common/omi_config.go index 596f109f..ec4b3bfe 100644 --- a/builder/common/omi_config.go +++ b/builder/common/omi_config.go @@ -23,6 +23,7 @@ type OMIConfig struct { SnapshotAccountIDs []string `mapstructure:"snapshot_account_ids"` GlobalPermission bool `mapstructure:"global_permission"` ProductCodes []string `mapstructure:"product_codes"` + RootDeviceName string `mapstructure:"root_device_name"` } func (c *OMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context) []error { diff --git a/builder/common/step_update_omi.go b/builder/common/step_update_omi.go index e655e3df..325b82b4 100644 --- a/builder/common/step_update_omi.go +++ b/builder/common/step_update_omi.go @@ -32,8 +32,8 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa // Determine if there is any work to do. valid := false - valid = valid || (s.AccountIds != nil && len(s.AccountIds) > 0) - valid = valid || (s.SnapshotAccountIds != nil && len(s.SnapshotAccountIds) > 0) + valid = valid || (len(s.AccountIds) > 0) + valid = valid || (len(s.SnapshotAccountIds) > 0) valid = valid || s.GlobalPermission == true if !valid { return multistep.ActionContinue @@ -74,7 +74,7 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa } } - if s.AccountIds == nil || len(s.AccountIds) == 0 { + if len(s.AccountIds) == 0 { return multistep.ActionContinue }