target: pass the image filename in each target

Rather than having to assume that we only ever produce one
artifact, have each upload target contain the filename it expects
to upload from the osbuild output.

An image file is always explicitly named in the manifest, and we
leave it up to each distro to decide how this is done, but the
convention is to use the same image filename as used when
downloading the image through weldr.

Now make this policy explicit, by quering the distro for the image
name and inserting it into each upload target.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2020-03-21 23:40:48 +01:00
parent 61836a7079
commit 839b22026e
8 changed files with 24 additions and 21 deletions

View file

@ -97,23 +97,7 @@ func (job *Job) Run(uploader LocalTargetUploader) (*common.ComposeResult, error)
for _, t := range job.Targets { for _, t := range job.Targets {
switch options := t.Options.(type) { switch options := t.Options.(type) {
case *target.LocalTargetOptions: case *target.LocalTargetOptions:
outputDir := path.Join(tmpStore, "refs", result.OutputID) f, err := os.Open(path.Join(tmpStore, "refs", result.OutputID, options.Filename))
files, err := ioutil.ReadDir(outputDir)
if err != nil {
r = append(r, err)
continue
}
// TODO osbuild pipelines can have multiple outputs. All the pipelines we
// are currently generating have exactly one, but we should support
// uploading all results in the future.
if len(files) != 1 {
r = append(r, fmt.Errorf("expected exactly one resulting image file"))
continue
}
f, err := os.Open(path.Join(outputDir, files[0].Name()))
if err != nil { if err != nil {
r = append(r, err) r = append(r, err)
continue continue
@ -137,7 +121,7 @@ func (job *Job) Run(uploader LocalTargetUploader) (*common.ComposeResult, error)
options.Key = job.ID.String() options.Key = job.ID.String()
} }
_, err = a.Upload(tmpStore+"/refs/"+result.OutputID+"/image.raw.xz", options.Bucket, options.Key) _, err = a.Upload(path.Join(tmpStore, "refs", result.OutputID, options.Filename), options.Bucket, options.Key)
if err != nil { if err != nil {
r = append(r, err) r = append(r, err)
continue continue

View file

@ -595,8 +595,15 @@ func (s *Store) PushCompose(distro distro.Distro, composeID uuid.UUID, bp *bluep
return fmt.Errorf("cannot create output directory for job %v: %#v", composeID, err) return fmt.Errorf("cannot create output directory for job %v: %#v", composeID, err)
} }
filename, _, err := distro.FilenameFromType(composeType)
if err != nil {
return fmt.Errorf("cannot query filename from image type %s: %#v", composeType, err)
}
targets = append(targets, target.NewLocalTarget( targets = append(targets, target.NewLocalTarget(
&target.LocalTargetOptions{}, &target.LocalTargetOptions{
Filename: filename,
},
)) ))
} }

View file

@ -1,6 +1,7 @@
package target package target
type AWSTargetOptions struct { type AWSTargetOptions struct {
Filename string `json:"filename"`
Region string `json:"region"` Region string `json:"region"`
AccessKeyID string `json:"accessKeyID"` AccessKeyID string `json:"accessKeyID"`
SecretAccessKey string `json:"secretAccessKey"` SecretAccessKey string `json:"secretAccessKey"`

View file

@ -1,6 +1,7 @@
package target package target
type AzureTargetOptions struct { type AzureTargetOptions struct {
Filename string `json:"filename"`
Account string `json:"account"` Account string `json:"account"`
AccessKey string `json:"accessKey"` AccessKey string `json:"accessKey"`
Container string `json:"container"` Container string `json:"container"`

View file

@ -1,6 +1,7 @@
package target package target
type LocalTargetOptions struct { type LocalTargetOptions struct {
Filename string `json:"filename"`
} }
func (LocalTargetOptions) isTargetOptions() {} func (LocalTargetOptions) isTargetOptions() {}

View file

@ -1396,7 +1396,7 @@ func (api *API) composeHandler(writer http.ResponseWriter, request *http.Request
var uploadTarget *target.Target var uploadTarget *target.Target
if isRequestVersionAtLeast(params, 1) && cr.Upload != nil { if isRequestVersionAtLeast(params, 1) && cr.Upload != nil {
uploadTarget, err = uploadRequestToTarget(*cr.Upload) uploadTarget, err = uploadRequestToTarget(*cr.Upload, api.distro, cr.ComposeType)
if err != nil { if err != nil {
errors := responseError{ errors := responseError{
ID: "UploadError", ID: "UploadError",

View file

@ -495,6 +495,7 @@ func TestCompose(t *testing.T) {
Status: common.IBWaiting, Status: common.IBWaiting,
ImageName: "test_upload", ImageName: "test_upload",
Options: &target.AWSTargetOptions{ Options: &target.AWSTargetOptions{
Filename: "test.img",
Region: "frankfurt", Region: "frankfurt",
AccessKeyID: "accesskey", AccessKeyID: "accesskey",
SecretAccessKey: "secretkey", SecretAccessKey: "secretkey",

View file

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/target" "github.com/osbuild/osbuild-composer/internal/target"
@ -117,9 +118,14 @@ func targetsToUploadResponses(targets []*target.Target) []uploadResponse {
return uploads return uploads
} }
func uploadRequestToTarget(u uploadRequest) (*target.Target, error) { func uploadRequestToTarget(u uploadRequest, d distro.Distro, imageType string) (*target.Target, error) {
var t target.Target var t target.Target
filename, _, err := d.FilenameFromType(imageType)
if err != nil {
return nil, err
}
t.Uuid = uuid.New() t.Uuid = uuid.New()
t.ImageName = u.ImageName t.ImageName = u.ImageName
t.Status = common.IBWaiting t.Status = common.IBWaiting
@ -129,6 +135,7 @@ func uploadRequestToTarget(u uploadRequest) (*target.Target, error) {
case *awsUploadSettings: case *awsUploadSettings:
t.Name = "org.osbuild.aws" t.Name = "org.osbuild.aws"
t.Options = &target.AWSTargetOptions{ t.Options = &target.AWSTargetOptions{
Filename: filename,
Region: options.Region, Region: options.Region,
AccessKeyID: options.AccessKeyID, AccessKeyID: options.AccessKeyID,
SecretAccessKey: options.SecretAccessKey, SecretAccessKey: options.SecretAccessKey,
@ -138,6 +145,7 @@ func uploadRequestToTarget(u uploadRequest) (*target.Target, error) {
case *azureUploadSettings: case *azureUploadSettings:
t.Name = "org.osbuild.azure" t.Name = "org.osbuild.azure"
t.Options = &target.AzureTargetOptions{ t.Options = &target.AzureTargetOptions{
Filename: filename,
Account: options.Account, Account: options.Account,
AccessKey: options.AccessKey, AccessKey: options.AccessKey,
Container: options.Container, Container: options.Container,