-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bib: use the new uploader interface from images
This commit switches bib to the new uploader interface in images. This requires osbuild/images#1185
- Loading branch information
Showing
8 changed files
with
208 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package main | |
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
|
@@ -17,6 +18,7 @@ import ( | |
"golang.org/x/exp/slices" | ||
|
||
"github.com/osbuild/images/pkg/arch" | ||
"github.com/osbuild/images/pkg/cloud" | ||
"github.com/osbuild/images/pkg/cloud/awscloud" | ||
"github.com/osbuild/images/pkg/container" | ||
"github.com/osbuild/images/pkg/dnfjson" | ||
|
@@ -53,6 +55,9 @@ var distroDefPaths = []string{ | |
var ( | ||
osGetuid = os.Getuid | ||
osGetgid = os.Getgid | ||
|
||
osStdout = os.Stdout | ||
osStderr = os.Stderr | ||
) | ||
|
||
// canChownInPath checks if the ownership of files can be set in a given path. | ||
|
@@ -355,53 +360,36 @@ func cmdManifest(cmd *cobra.Command, args []string) error { | |
return nil | ||
} | ||
|
||
func handleAWSFlags(cmd *cobra.Command) (upload bool, err error) { | ||
func handleAWSFlags(cmd *cobra.Command) (cloud.Uploader, error) { | ||
imgTypes, _ := cmd.Flags().GetStringArray("type") | ||
region, _ := cmd.Flags().GetString("aws-region") | ||
if region == "" { | ||
return false, nil | ||
return nil, nil | ||
} | ||
bucketName, _ := cmd.Flags().GetString("aws-bucket") | ||
imageName, _ := cmd.Flags().GetString("aws-ami-name") | ||
targetArch, _ := cmd.Flags().GetString("target-arch") | ||
|
||
if !slices.Contains(imgTypes, "ami") { | ||
Check failure on line 373 in bib/cmd/bootc-image-builder/main.go
|
||
return false, fmt.Errorf("aws flags set for non-ami image type (type is set to %s)", strings.Join(imgTypes, ",")) | ||
return nil, fmt.Errorf("aws flags set for non-ami image type (type is set to %s)", strings.Join(imgTypes, ",")) | ||
} | ||
|
||
// check as many permission prerequisites as possible before starting | ||
client, err := awscloud.NewDefault(region) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
logrus.Info("Checking AWS region access...") | ||
regions, err := client.Regions() | ||
if err != nil { | ||
return false, fmt.Errorf("retrieving AWS regions for '%s' failed: %w", region, err) | ||
} | ||
|
||
if !slices.Contains(regions, region) { | ||
return false, fmt.Errorf("given AWS region '%s' not found", region) | ||
uploaderOpts := &awscloud.UploaderOptions{ | ||
TargetArch: targetArch, | ||
} | ||
|
||
logrus.Info("Checking AWS bucket...") | ||
buckets, err := client.Buckets() | ||
uploader, err := awscloud.NewUploader(region, bucketName, imageName, uploaderOpts) | ||
if err != nil { | ||
return false, fmt.Errorf("retrieving AWS list of buckets failed: %w", err) | ||
} | ||
if !slices.Contains(buckets, bucketName) { | ||
return false, fmt.Errorf("bucket '%s' not found in the given AWS account", bucketName) | ||
return nil, err | ||
} | ||
|
||
logrus.Info("Checking AWS bucket permissions...") | ||
writePermission, err := client.CheckBucketPermission(bucketName, awscloud.S3PermissionWrite) | ||
if err != nil { | ||
return false, err | ||
status := io.Discard | ||
if logrus.GetLevel() >= logrus.InfoLevel { | ||
status = os.Stderr | ||
} | ||
if !writePermission { | ||
return false, fmt.Errorf("you don't have write permissions to bucket '%s' with the given AWS account", bucketName) | ||
if err := uploader.Check(status); err != nil { | ||
return nil, err | ||
} | ||
logrus.Info("Upload conditions met.") | ||
return true, nil | ||
return uploader, nil | ||
} | ||
|
||
func cmdBuild(cmd *cobra.Command, args []string) error { | ||
|
@@ -430,7 +418,7 @@ func cmdBuild(cmd *cobra.Command, args []string) error { | |
return fmt.Errorf("cannot setup build dir: %w", err) | ||
} | ||
|
||
upload, err := handleAWSFlags(cmd) | ||
uploader, err := handleAWSFlags(cmd) | ||
if err != nil { | ||
return fmt.Errorf("cannot handle AWS setup: %w", err) | ||
} | ||
|
@@ -493,7 +481,7 @@ func cmdBuild(cmd *cobra.Command, args []string) error { | |
} | ||
|
||
pbar.SetMessagef("Build complete!") | ||
if upload { | ||
if uploader != nil { | ||
// XXX: pass our own progress.ProgressBar here | ||
// *for now* just stop our own progress and let the uploadAMI | ||
// progress take over - but we really need to fix this in a | ||
|
@@ -503,7 +491,7 @@ func cmdBuild(cmd *cobra.Command, args []string) error { | |
switch imgType { | ||
case "ami": | ||
diskpath := filepath.Join(outputDir, exports[idx], "disk.raw") | ||
if err := uploadAMI(diskpath, targetArch, cmd.Flags()); err != nil { | ||
if err := upload(uploader, diskpath, cmd.Flags()); err != nil { | ||
return fmt.Errorf("cannot upload AMI: %w", err) | ||
} | ||
default: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.