upload: warn the user if no --arch flag is passed on upload
This mitigates the issue that a `image-builder upload` command currently does not know the target architecture of the image it uploads.
This commit is contained in:
parent
2c017fc630
commit
03613a3fb3
3 changed files with 34 additions and 13 deletions
|
|
@ -335,7 +335,7 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
|
||||
bootMode := res.ImgType.BootMode()
|
||||
uploader, err := uploaderFor(cmd, res.ImgType.Name(), &bootMode)
|
||||
uploader, err := uploaderFor(cmd, res.ImgType.Name(), res.ImgType.Arch().Name(), &bootMode)
|
||||
if errors.Is(err, ErrUploadTypeUnsupported) || errors.Is(err, ErrUploadConfigNotProvided) {
|
||||
err = nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/osbuild/image-builder-cli/pkg/progress"
|
||||
"github.com/osbuild/images/pkg/arch"
|
||||
"github.com/osbuild/images/pkg/cloud"
|
||||
"github.com/osbuild/images/pkg/cloud/awscloud"
|
||||
"github.com/osbuild/images/pkg/platform"
|
||||
|
|
@ -62,17 +63,17 @@ func uploaderCheckWithProgress(pbar progress.ProgressBar, uploader cloud.Uploade
|
|||
return uploader.Check(pw)
|
||||
}
|
||||
|
||||
func uploaderFor(cmd *cobra.Command, typeOrCloud string, bootMode *platform.BootMode) (cloud.Uploader, error) {
|
||||
func uploaderFor(cmd *cobra.Command, typeOrCloud string, targetArch string, bootMode *platform.BootMode) (cloud.Uploader, error) {
|
||||
switch typeOrCloud {
|
||||
case "ami", "aws":
|
||||
return uploaderForCmdAWS(cmd, bootMode)
|
||||
return uploaderForCmdAWS(cmd, targetArch, bootMode)
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: %q", ErrUploadTypeUnsupported, typeOrCloud)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func uploaderForCmdAWS(cmd *cobra.Command, bootMode *platform.BootMode) (cloud.Uploader, error) {
|
||||
func uploaderForCmdAWS(cmd *cobra.Command, targetArch string, bootMode *platform.BootMode) (cloud.Uploader, error) {
|
||||
amiName, err := cmd.Flags().GetString("aws-ami-name")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -85,10 +86,6 @@ func uploaderForCmdAWS(cmd *cobra.Command, bootMode *platform.BootMode) (cloud.U
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
targetArch, err := cmd.Flags().GetString("arch")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if bootMode == nil {
|
||||
// If unset, default to BOOT_HYBIRD which translated
|
||||
// to "uefi-prefered" when registering the image.
|
||||
|
|
@ -127,6 +124,8 @@ func uploaderForCmdAWS(cmd *cobra.Command, bootMode *platform.BootMode) (cloud.U
|
|||
}
|
||||
|
||||
func cmdUpload(cmd *cobra.Command, args []string) error {
|
||||
imagePath := args[0]
|
||||
|
||||
uploadTo, err := cmd.Flags().GetString("to")
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -135,8 +134,18 @@ func cmdUpload(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("missing --to parameter, try --to=aws")
|
||||
}
|
||||
|
||||
imagePath := args[0]
|
||||
uploader, err := uploaderFor(cmd, uploadTo, nil)
|
||||
targetArch, err := cmd.Flags().GetString("arch")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if targetArch == "" {
|
||||
// we could try to inspect the image here and get a
|
||||
// better guess
|
||||
targetArch = arch.Current().String()
|
||||
fmt.Fprintf(osStderr, "WARNING: no upload architecture specified, using %q (use --arch to override)\n", targetArch)
|
||||
}
|
||||
|
||||
uploader, err := uploaderFor(cmd, uploadTo, targetArch, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/osbuild/images/pkg/arch"
|
||||
"github.com/osbuild/images/pkg/cloud"
|
||||
"github.com/osbuild/images/pkg/cloud/awscloud"
|
||||
"github.com/osbuild/images/pkg/platform"
|
||||
|
|
@ -58,7 +59,7 @@ func TestUploadWithAWSMock(t *testing.T) {
|
|||
targetArchArg string
|
||||
expectedUploadArch string
|
||||
}{
|
||||
{"", ""},
|
||||
{"", "x86_64"},
|
||||
{"aarch64", "aarch64"},
|
||||
} {
|
||||
var fa fakeAwsUploader
|
||||
|
|
@ -71,9 +72,11 @@ func TestUploadWithAWSMock(t *testing.T) {
|
|||
})
|
||||
defer restore()
|
||||
|
||||
var fakeStdout bytes.Buffer
|
||||
var fakeStdout, fakeStderr bytes.Buffer
|
||||
restore = main.MockOsStdout(&fakeStdout)
|
||||
defer restore()
|
||||
restore = main.MockOsStderr(&fakeStderr)
|
||||
defer restore()
|
||||
|
||||
restore = main.MockOsArgs([]string{
|
||||
"upload",
|
||||
|
|
@ -100,10 +103,19 @@ func TestUploadWithAWSMock(t *testing.T) {
|
|||
assert.Equal(t, fakeDiskContent, fa.uploadAndRegisterRead.String())
|
||||
// progress was rendered
|
||||
assert.Contains(t, fakeStdout.String(), "--] 100.00%")
|
||||
|
||||
// warning was passed
|
||||
if tc.targetArchArg == "" {
|
||||
assert.Equal(t, fakeStderr.String(), `WARNING: no upload architecture specified, using "x86_64" (use --arch to override)`+"\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadCmdlineErrors(t *testing.T) {
|
||||
var fakeStderr bytes.Buffer
|
||||
restore := main.MockOsStderr(&fakeStderr)
|
||||
defer restore()
|
||||
|
||||
for _, tc := range []struct {
|
||||
cmdline []string
|
||||
expectedErr string
|
||||
|
|
@ -183,7 +195,7 @@ func TestBuildAndUploadWithAWSMock(t *testing.T) {
|
|||
assert.Equal(t, bucketName, "aws-bucket-2")
|
||||
assert.Equal(t, amiName, "aws-ami-3")
|
||||
expectedBootMode := platform.BOOT_HYBRID
|
||||
assert.Equal(t, &awscloud.UploaderOptions{BootMode: &expectedBootMode}, uploadOpts)
|
||||
assert.Equal(t, &awscloud.UploaderOptions{BootMode: &expectedBootMode, TargetArch: arch.Current().String()}, uploadOpts)
|
||||
assert.Equal(t, 1, fa.checkCalls)
|
||||
assert.Equal(t, 1, fa.uploadAndRegisterCalls)
|
||||
assert.Equal(t, "fake-img-raw\n", fa.uploadAndRegisterRead.String())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue