From 5d73c22bb5a315661bd2ce2589dbfffc76180f3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Wed, 6 Aug 2025 13:46:59 +0200 Subject: [PATCH] internal/awscloud: use AWS.ShareImage() from osbuild/images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomáš Hozza --- cmd/osbuild-worker/jobimpl-awsec2.go | 2 +- internal/cloud/awscloud/awscloud.go | 79 ---------------------------- 2 files changed, 1 insertion(+), 80 deletions(-) diff --git a/cmd/osbuild-worker/jobimpl-awsec2.go b/cmd/osbuild-worker/jobimpl-awsec2.go index bd96852df..4bd13e957 100644 --- a/cmd/osbuild-worker/jobimpl-awsec2.go +++ b/cmd/osbuild-worker/jobimpl-awsec2.go @@ -127,7 +127,7 @@ func (impl *AWSEC2ShareJobImpl) Run(job worker.Job) error { return err } - err = aws.ShareImage(args.Ami, args.ShareWithAccounts) + err = aws.ShareImage(args.Ami, nil, args.ShareWithAccounts) if err != nil { logWithId.Errorf("Error sharing image: %v", err) result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Error sharing image with target %v", args.ShareWithAccounts), nil) diff --git a/internal/cloud/awscloud/awscloud.go b/internal/cloud/awscloud/awscloud.go index e6980b96c..5b2d4aa32 100644 --- a/internal/cloud/awscloud/awscloud.go +++ b/internal/cloud/awscloud/awscloud.go @@ -18,7 +18,6 @@ import ( ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/aws/aws-sdk-go-v2/service/s3" images_awscloud "github.com/osbuild/images/pkg/cloud/awscloud" - "github.com/sirupsen/logrus" ) type AWS struct { @@ -299,84 +298,6 @@ func (a *AWS) CopyImage(name, ami, sourceRegion string) (string, error) { return *result.ImageId, nil } -func (a *AWS) ShareImage(ami string, userIds []string) error { - imgs, err := a.ec2.DescribeImages( - context.Background(), - &ec2.DescribeImagesInput{ - ImageIds: []string{ami}, - }, - ) - if err != nil { - return err - } - if len(imgs.Images) == 0 { - return fmt.Errorf("Unable to find image with id: %v", ami) - } - - for _, bdm := range imgs.Images[0].BlockDeviceMappings { - err = a.shareSnapshot(*bdm.Ebs.SnapshotId, userIds) - if err != nil { - return err - } - } - - err = a.shareImage(aws.String(ami), userIds) - if err != nil { - return err - } - return nil -} - -func (a *AWS) shareImage(ami *string, userIds []string) error { - logrus.Info("[AWS] 🎥 Sharing ec2 snapshot") - var uIds []*string - for i := range userIds { - uIds = append(uIds, &userIds[i]) - } - - logrus.Info("[AWS] 💿 Sharing ec2 AMI") - var launchPerms []ec2types.LaunchPermission - for _, id := range uIds { - launchPerms = append(launchPerms, ec2types.LaunchPermission{ - UserId: id, - }) - } - _, err := a.ec2.ModifyImageAttribute( - context.Background(), - &ec2.ModifyImageAttributeInput{ - ImageId: ami, - LaunchPermission: &ec2types.LaunchPermissionModifications{ - Add: launchPerms, - }, - }, - ) - if err != nil { - logrus.Warnf("[AWS] 📨 Error sharing AMI: %v", err) - return err - } - logrus.Info("[AWS] 💿 Shared AMI") - return nil -} - -func (a *AWS) shareSnapshot(snapshotId string, userIds []string) error { - logrus.Info("[AWS] 🎥 Sharing ec2 snapshot") - _, err := a.ec2.ModifySnapshotAttribute( - context.Background(), - &ec2.ModifySnapshotAttributeInput{ - Attribute: ec2types.SnapshotAttributeNameCreateVolumePermission, - OperationType: ec2types.OperationTypeAdd, - SnapshotId: aws.String(snapshotId), - UserIds: userIds, - }, - ) - if err != nil { - logrus.Warnf("[AWS] 📨 Error sharing ec2 snapshot: %v", err) - return err - } - logrus.Info("[AWS] 📨 Shared ec2 snapshot") - return nil -} - func (a *AWS) Regions() ([]string, error) { out, err := a.ec2.DescribeRegions(context.Background(), &ec2.DescribeRegionsInput{}) if err != nil {