From 4edeab80e3162707de16a24c135c47ca862471aa Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Thu, 17 Aug 2023 19:33:12 +0200 Subject: [PATCH] osbuild-worker: add the pulp.ostree target to the worker --- cmd/osbuild-worker/jobimpl-osbuild.go | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/cmd/osbuild-worker/jobimpl-osbuild.go b/cmd/osbuild-worker/jobimpl-osbuild.go index 9968eb251..307cdebda 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild.go +++ b/cmd/osbuild-worker/jobimpl-osbuild.go @@ -12,12 +12,14 @@ import ( "os" "os/exec" "path" + "path/filepath" "strings" "github.com/osbuild/images/pkg/container" "github.com/osbuild/images/pkg/osbuild" "github.com/osbuild/osbuild-composer/internal/upload/oci" + "github.com/osbuild/osbuild-composer/internal/upload/pulp" "github.com/google/uuid" "github.com/sirupsen/logrus" @@ -67,6 +69,11 @@ type OCIConfiguration struct { Namespace string } +type PulpConfiguration struct { + CredsFilePath string + ServerAddress string +} + type OSBuildJobImpl struct { Store string Output string @@ -293,6 +300,26 @@ func (impl *OSBuildJobImpl) getContainerClient(destination string, targetOptions return client, nil } +func (impl *OSBuildJobImpl) getPulpClient(targetOptions *target.PulpOSTreeTargetOptions) (*pulp.Client, error) { + creds := &pulp.Credentials{} + if targetOptions.Username != "" && targetOptions.Password != "" { + creds = &pulp.Credentials{ + Username: targetOptions.Username, + Password: targetOptions.Password, + } + } else { + // TODO: read from worker configuration + return nil, fmt.Errorf("no credentials for pulp were set") + } + + if targetOptions.ServerAddress == "" { + // TODO: read from worker configuration + return nil, fmt.Errorf("pulp server address not set") + } + + return pulp.NewClient(targetOptions.ServerAddress, creds), nil +} + func (impl *OSBuildJobImpl) Run(job worker.Job) error { logWithId := logrus.WithField("jobId", job.Id().String()) // Initialize variable needed for reporting back to osbuild-composer. @@ -1101,6 +1128,23 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { logWithId.Printf("[container] 🎉 Image uploaded (%s)!", digest.String()) targetResult.Options = &target.ContainerTargetResultOptions{URL: client.Target.String(), Digest: digest.String()} + case *target.PulpOSTreeTargetOptions: + targetResult = target.NewPulpOSTreeTargetResult(nil, &artifact) + archivePath := filepath.Join(outputDirectory, jobTarget.OsbuildArtifact.ExportName, jobTarget.OsbuildArtifact.ExportFilename) + + client, err := impl.getPulpClient(targetOptions) + if err != nil { + targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidConfig, err.Error(), nil) + break + } + + url, err := client.UploadAndDistributeCommit(archivePath, targetOptions.Repository, targetOptions.BasePath) + if err != nil { + targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, err.Error(), nil) + break + } + targetResult.Options = &target.PulpOSTreeTargetResultOptions{RepoURL: url} + default: // TODO: we may not want to return completely here with multiple targets, because then no TargetErrors will be added to the JobError details // Nevertheless, all target errors will be still in the OSBuildJobResult.