Skip to content

Commit

Permalink
Fix bsu builder
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Sep 17, 2024
1 parent d426cce commit ec33255
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 77 deletions.
2 changes: 2 additions & 0 deletions builder/bsu/builder.hcl2spec.go

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

29 changes: 22 additions & 7 deletions builder/bsu/step_create_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions builder/bsusurrogate/builder.hcl2spec.go

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

3 changes: 1 addition & 2 deletions builder/bsusurrogate/step_register_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions builder/chroot/builder.hcl2spec.go

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

95 changes: 32 additions & 63 deletions builder/common/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions builder/common/omi_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions builder/common/step_update_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit ec33255

Please sign in to comment.