target: add explicit target for uploading back to worker server

The uploading of artifacts back to the worker server for the on-premise
(Weldr) use case was signaled to the worker by setting the `ImageName`
in the `OSBuildJob` definition. The code also relies on the osbuild
exports being specified in the `OSBuildJob`, instead of in the target
(this is not implemented yet).

Prepare the ground for moving osbuild export definition from
`OSBuildJob` to `Target` by introducing an explicit `Worker Server"
upload target. This target will signal to the worker that it should
upload the image back to the worker server. The new target is not yet
used by any API implementation.

Extend the worker osbuild job implementation to handle the new upload
target.
This commit is contained in:
Tomas Hozza 2022-06-29 15:44:29 +02:00 committed by Tom Gundersen
parent a61b8af261
commit a12827865d
3 changed files with 39 additions and 0 deletions

View file

@ -362,6 +362,22 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
for _, jobTarget := range jobArgs.Targets {
var targetResult *target.TargetResult
switch targetOptions := jobTarget.Options.(type) {
case *target.WorkerServerTargetOptions:
targetResult = target.NewWorkerServerTargetResult()
var f *os.File
imagePath := path.Join(outputDirectory, exportPath, jobTarget.OsbuildArtifact.ExportFilename)
f, err = os.Open(imagePath)
if err != nil {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidTargetConfig, err.Error())
break
}
defer f.Close()
err = job.UploadArtifact(jobTarget.ImageName, f)
if err != nil {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, err.Error())
break
}
case *target.VMWareTargetOptions:
targetResult = target.NewVMWareTargetResult()
credentials := vmware.Credentials{

View file

@ -87,6 +87,8 @@ func (target *Target) UnmarshalJSON(data []byte) error {
options = new(OCITargetOptions)
case TargetNameContainer:
options = new(ContainerTargetOptions)
case TargetNameWorkerServer:
options = new(WorkerServerTargetOptions)
default:
return fmt.Errorf("unexpected target name: %s", rawTarget.Name)
}
@ -254,6 +256,12 @@ func (target Target) MarshalJSON() ([]byte, error) {
}
rawOptions, err = json.Marshal(compat)
case *WorkerServerTargetOptions:
// WorkerServer target does not handle the backward compatibility
// for the Filename in target options, because it was added after
// the incompatible change.
rawOptions, err = json.Marshal(target.Options)
default:
return nil, fmt.Errorf("unexpected target options type: %t", t)
}

View file

@ -0,0 +1,15 @@
package target
const TargetNameWorkerServer TargetName = "org.osbuild.worker.server"
type WorkerServerTargetOptions struct{}
func (WorkerServerTargetOptions) isTargetOptions() {}
func NewWorkerServerTarget() *Target {
return newTarget(TargetNameWorkerServer, &WorkerServerTargetOptions{})
}
func NewWorkerServerTargetResult() *TargetResult {
return newTargetResult(TargetNameWorkerServer, nil)
}