From 6902f730cb8aab42ffd1023ac588b6993fc7b115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Budai?= Date: Mon, 10 Feb 2020 15:22:37 +0100 Subject: [PATCH] worker: upload local target image using jobqueue api Prior this commit local target copied the image from a worker to a composer using cp(1) command. This prevented the local target to work on remote workers. This commit switches the local target implementation to using the jobqueue API introduced in the previous commit. I had some concerns about speed of this solution (imho nothing can beat pure cp(1) implementation) but ad hoc sanity tests showed the copying of the image using the jobqueue API when running the worker on the same machine as the composer is still more or less instant. --- cmd/osbuild-worker/main.go | 11 ++++++++++- internal/jobqueue/job.go | 13 +++++++++---- internal/jobqueue/local_target_uploader.go | 7 +++++++ 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 internal/jobqueue/local_target_uploader.go diff --git a/cmd/osbuild-worker/main.go b/cmd/osbuild-worker/main.go index 03e1de062..ebadd1514 100644 --- a/cmd/osbuild-worker/main.go +++ b/cmd/osbuild-worker/main.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "log" "net" "net/http" @@ -77,6 +78,14 @@ func (c *ComposerClient) UpdateJob(job *jobqueue.Job, status common.ImageBuildSt return nil } +func (c *ComposerClient) UploadImage(job *jobqueue.Job, reader io.Reader) error { + // content type doesn't really matter + url := fmt.Sprintf("http://localhost/job-queue/v1/jobs/%s/builds/%d/image", job.ID.String(), job.ImageBuildID) + _, err := c.client.Post(url, "application/octet-stream", reader) + + return err +} + func handleJob(client *ComposerClient) error { fmt.Println("Waiting for a new job...") job, err := client.AddJob() @@ -90,7 +99,7 @@ func handleJob(client *ComposerClient) error { } fmt.Printf("Running job %s\n", job.ID.String()) - result, err := job.Run() + result, err := job.Run(client) if err != nil { log.Printf(" Job failed: %v", err) return client.UpdateJob(job, common.IBFailed, result) diff --git a/internal/jobqueue/job.go b/internal/jobqueue/job.go index 2aa4d676f..03fe78fe3 100644 --- a/internal/jobqueue/job.go +++ b/internal/jobqueue/job.go @@ -44,7 +44,7 @@ func (e *TargetsError) Error() string { return errString } -func (job *Job) Run() (*common.ComposeResult, error) { +func (job *Job) Run(uploader LocalTargetUploader) (*common.ComposeResult, error) { distros := distro.NewRegistry([]string{"/etc/osbuild-composer", "/usr/share/osbuild-composer"}) d := distros.GetDistro(job.Distro) if d == nil { @@ -120,14 +120,19 @@ func (job *Job) Run() (*common.ComposeResult, error) { for _, t := range job.Targets { switch options := t.Options.(type) { case *target.LocalTargetOptions: - - err = runCommand("cp", "-a", "-L", tmpStore+"/refs/"+result.OutputID+"/.", options.Location) + filename, _, err := d.FilenameFromType(job.OutputType) if err != nil { r = append(r, err) continue } - err = runCommand("chown", "-R", "_osbuild-composer:_osbuild-composer", options.Location) + f, err := os.Open(tmpStore + "/refs/" + result.OutputID + "/" + filename) + if err != nil { + r = append(r, err) + continue + } + + err = uploader.UploadImage(job, f) if err != nil { r = append(r, err) continue diff --git a/internal/jobqueue/local_target_uploader.go b/internal/jobqueue/local_target_uploader.go new file mode 100644 index 000000000..292b34733 --- /dev/null +++ b/internal/jobqueue/local_target_uploader.go @@ -0,0 +1,7 @@ +package jobqueue + +import "io" + +type LocalTargetUploader interface { + UploadImage(job *Job, reader io.Reader) error +}