store/jobqueue: remove distro field from jobs

A job's purpose is to build an osbuild manifest and upload the results
somewhere. It should not know about which distro was used to generate
the pipeline.

Workers depended on the distro package in two ways:

1. To set an osbuild `--build-env`. This is not necessary anymore in new
   versions of osbuild. More importantly, it was wrong: it passed the
   runner from the distro that is being built, instead of one that
   matches the host.

   This patch simply removes that logic.

2. To fetch the output filename with `Distro.FilenameFromType()`. While
   that is useful, I don't think it warrants the dependency.

   This patch uses the fact that all current pipelines output exactly
   one file and uploads that. This should probably be extended in the
   future to upload all output files, or to name them explicitly in the
   upload target.

The worker should now compile to a smaller binary and do less
unnecessary work on startup (like reading repository files).
This commit is contained in:
Lars Karlitski 2020-03-18 09:51:39 +01:00 committed by David Rheinsberg
parent 76b236796c
commit 1b7cb6c11b
4 changed files with 14 additions and 40 deletions

View file

@ -105,7 +105,6 @@ func (api *API) addJobHandler(writer http.ResponseWriter, request *http.Request,
_ = json.NewEncoder(writer).Encode(Job{
ID: nextJob.ComposeID,
ImageBuildID: nextJob.ImageBuildID,
Distro: nextJob.Distro,
Manifest: nextJob.Manifest,
Targets: nextJob.Targets,
OutputType: nextJob.ImageType,

View file

@ -60,7 +60,7 @@ func TestCreate(t *testing.T) {
}
test.TestRoute(t, api, false, "POST", "/job-queue/v1/jobs", `{}`, http.StatusCreated,
`{"id":"ffffffff-ffff-ffff-ffff-ffffffffffff","image_build_id":0,"distro":"fedora-30","manifest":{"sources":{},"pipeline":{}},"targets":[],"output_type":"qcow2"}`, "created", "uuid")
`{"id":"ffffffff-ffff-ffff-ffff-ffffffffffff","image_build_id":0,"manifest":{"sources":{},"pipeline":{}},"targets":[],"output_type":"qcow2"}`, "created", "uuid")
}
func testUpdateTransition(t *testing.T, from, to string, expectedStatus int, expectedResponse string) {

View file

@ -6,13 +6,13 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/target"
"github.com/osbuild/osbuild-composer/internal/upload/awsupload"
)
@ -20,7 +20,6 @@ import (
type Job struct {
ID uuid.UUID `json:"id"`
ImageBuildID int `json:"image_build_id"`
Distro string `json:"distro"`
Manifest *osbuild.Manifest `json:"manifest"`
Targets []*target.Target `json:"targets"`
OutputType string `json:"output_type"`
@ -46,32 +45,6 @@ func (e *TargetsError) Error() string {
}
func (job *Job) Run(uploader LocalTargetUploader) (*common.ComposeResult, error) {
distros, err := distro.NewDefaultRegistry([]string{"/etc/osbuild-composer", "/usr/share/osbuild-composer"})
if err != nil {
return nil, fmt.Errorf("error loading distros: %v", err)
}
d := distros.GetDistro(job.Distro)
if d == nil {
return nil, fmt.Errorf("unknown distro: %s", job.Distro)
}
build := osbuild.Build{
Runner: d.Runner(),
}
buildFile, err := ioutil.TempFile("", "osbuild-worker-build-env-*")
if err != nil {
return nil, err
}
// FIXME: how to handle errors in defer?
defer os.Remove(buildFile.Name())
err = json.NewEncoder(buildFile).Encode(build)
if err != nil {
return nil, fmt.Errorf("error encoding build environment: %v", err)
}
tmpStore, err := ioutil.TempDir("/var/tmp", "osbuild-store")
if err != nil {
return nil, fmt.Errorf("error setting up osbuild store: %v", err)
@ -82,7 +55,6 @@ func (job *Job) Run(uploader LocalTargetUploader) (*common.ComposeResult, error)
cmd := exec.Command(
"osbuild",
"--store", tmpStore,
"--build-env", buildFile.Name(),
"--json", "-",
)
cmd.Stderr = os.Stderr
@ -125,13 +97,23 @@ func (job *Job) Run(uploader LocalTargetUploader) (*common.ComposeResult, error)
for _, t := range job.Targets {
switch options := t.Options.(type) {
case *target.LocalTargetOptions:
filename, _, err := d.FilenameFromType(job.OutputType)
outputDir := path.Join(tmpStore, "refs", result.OutputID)
files, err := ioutil.ReadDir(outputDir)
if err != nil {
r = append(r, err)
continue
}
f, err := os.Open(tmpStore + "/refs/" + result.OutputID + "/" + filename)
// 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 {
r = append(r, err)
continue