From e13f0a1ae2438c016a2a4e84e0d8b99216b3c7ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Tue, 16 May 2023 11:45:07 +0200 Subject: [PATCH] AWS: allow specifying the AMI boot mode when registering the image MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the AMI is being registered from a snapshot, the caller can optionally specify the boot mode of the AMI. If no boot mode is specified, then the default behavior is to use the boot type of the instance that is launched from the AMI. The default behavior (no boot type specified) is preserved after this change. Signed-off-by: Tomáš Hozza --- cmd/osbuild-upload-aws/main.go | 2 +- cmd/osbuild-worker/jobimpl-osbuild.go | 2 +- internal/boot/aws.go | 2 +- internal/cloud/awscloud/awscloud.go | 13 ++++++++++++- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/osbuild-upload-aws/main.go b/cmd/osbuild-upload-aws/main.go index 9015cdbcd..0164e7459 100644 --- a/cmd/osbuild-upload-aws/main.go +++ b/cmd/osbuild-upload-aws/main.go @@ -50,7 +50,7 @@ func main() { if shareWith != "" { share = append(share, shareWith) } - ami, err := a.Register(imageName, bucketName, keyName, share, arch) + ami, err := a.Register(imageName, bucketName, keyName, share, arch, nil) if err != nil { println(err.Error()) return diff --git a/cmd/osbuild-worker/jobimpl-osbuild.go b/cmd/osbuild-worker/jobimpl-osbuild.go index b5fecf8d1..5b3631c6d 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild.go +++ b/cmd/osbuild-worker/jobimpl-osbuild.go @@ -511,7 +511,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { break } - ami, err := a.Register(jobTarget.ImageName, bucket, targetOptions.Key, targetOptions.ShareWithAccounts, common.CurrentArch()) + ami, err := a.Register(jobTarget.ImageName, bucket, targetOptions.Key, targetOptions.ShareWithAccounts, common.CurrentArch(), nil) if err != nil { targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorImportingImage, err.Error(), nil) break diff --git a/internal/boot/aws.go b/internal/boot/aws.go index a5c19315c..037103f7b 100644 --- a/internal/boot/aws.go +++ b/internal/boot/aws.go @@ -99,7 +99,7 @@ func UploadImageToAWS(c *awsCredentials, imagePath string, imageName string) err if err != nil { return fmt.Errorf("cannot upload the image: %v", err) } - _, err = uploader.Register(imageName, c.Bucket, imageName, nil, common.CurrentArch()) + _, err = uploader.Register(imageName, c.Bucket, imageName, nil, common.CurrentArch(), nil) if err != nil { return fmt.Errorf("cannot register the image: %v", err) } diff --git a/internal/cloud/awscloud/awscloud.go b/internal/cloud/awscloud/awscloud.go index 812756d51..419d0b4dc 100644 --- a/internal/cloud/awscloud/awscloud.go +++ b/internal/cloud/awscloud/awscloud.go @@ -15,6 +15,7 @@ import ( "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/sirupsen/logrus" + "golang.org/x/exp/slices" ) type AWS struct { @@ -208,7 +209,10 @@ func WaitUntilImportSnapshotTaskCompletedWithContext(c *ec2.EC2, ctx aws.Context // Register is a function that imports a snapshot, waits for the snapshot to // fully import, tags the snapshot, cleans up the image in S3, and registers // an AMI in AWS. -func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch string) (*string, error) { +// The caller can optionally specify the boot mode of the AMI. If the boot +// mode is not specified, then the instances launched from this AMI use the +// default boot mode value of the instance type. +func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch string, bootMode *string) (*string, error) { rpmArchToEC2Arch := map[string]string{ "x86_64": "x86_64", "aarch64": "arm64", @@ -219,6 +223,12 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str return nil, fmt.Errorf("ec2 doesn't support the following arch: %s", rpmArch) } + if bootMode != nil { + if !slices.Contains(ec2.BootModeValues_Values(), *bootMode) { + return nil, fmt.Errorf("ec2 doesn't support the following boot mode: %s", *bootMode) + } + } + logrus.Infof("[AWS] 📥 Importing snapshot from image: %s/%s", bucket, key) snapshotDescription := fmt.Sprintf("Image Builder AWS Import of %s", name) importTaskOutput, err := a.ec2.ImportSnapshot( @@ -294,6 +304,7 @@ func (a *AWS) Register(name, bucket, key string, shareWith []string, rpmArch str registerOutput, err := a.ec2.RegisterImage( &ec2.RegisterImageInput{ Architecture: aws.String(ec2Arch), + BootMode: bootMode, VirtualizationType: aws.String("hvm"), Name: aws.String(name), RootDeviceName: aws.String("/dev/sda1"),