The store is responsible for two things: user state and the compose queue. This is problematic, because the rcm API has slightly different semantics from weldr and only used the queue part of the store. Also, the store is simply too complex. This commit splits the queue part out, using the new jobqueue package in both the weldr and the rcm package. The queue is saved to a new directory `queue/`. The weldr package now also has access to a worker server to enqueue and list jobs. Its store continues to track composes, but the `QueueStatus` for each compose (and image build) is deprecated. The field in `ImageBuild` is kept for backwards compatibility for composes which finished before this change, but a lot of code dealing with it in package compose is dropped. store.PushCompose() is degraded to storing a new compose. It should probably be renamed in the future. store.PopJob() is removed. Job ids are now independent of compose ids. Because of that, the local target gains ComposeId and ImageBuildId fields, because a worker cannot infer those from a job anymore. This also necessitates a change in the worker API: the job routes are changed to expect that instead of a (compose id, image build id) pair. The route that accepts built images keeps that pair, because it reports the image back to weldr. worker.Server() now interacts with a job queue instead of the store. It gains public functions that allow enqueuing an osbuild job and getting its status, because only it knows about the specific argument and result types in the job queue (OSBuildJob and OSBuildJobResult). One oddity remains: it needs to report an uploaded image to weldr. Do this with a function that's passed in for now, so that the dependency to the store can be dropped completely. The rcm API drops its dependencies to package blueprint and store, because it too interacts only with the worker server now. Fixes #342
128 lines
4.1 KiB
Go
128 lines
4.1 KiB
Go
// Package compose encapsulates the concept of a compose. It is a separate module from common, because it includes
|
|
// target which in turn includes common and thus it would create a cyclic dependency, which is forbidden in golang.
|
|
package compose
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/osbuild/osbuild-composer/internal/blueprint"
|
|
"github.com/osbuild/osbuild-composer/internal/common"
|
|
"github.com/osbuild/osbuild-composer/internal/osbuild"
|
|
"github.com/osbuild/osbuild-composer/internal/target"
|
|
)
|
|
|
|
type StateTransitionError struct {
|
|
message string
|
|
}
|
|
|
|
func (ste *StateTransitionError) Error() string {
|
|
return ste.message
|
|
}
|
|
|
|
// ImageBuild represents a single image build inside a compose
|
|
type ImageBuild struct {
|
|
Id int `json:"id"`
|
|
ImageType common.ImageType `json:"image_type"`
|
|
Manifest *osbuild.Manifest `json:"manifest"`
|
|
Targets []*target.Target `json:"targets"`
|
|
JobCreated time.Time `json:"job_created"`
|
|
JobStarted time.Time `json:"job_started"`
|
|
JobFinished time.Time `json:"job_finished"`
|
|
Size uint64 `json:"size"`
|
|
JobId uuid.UUID `json:"jobid,omitempty"`
|
|
|
|
// Kept for backwards compatibility. Image builds which were done
|
|
// before the move to the job queue use this to store whether they
|
|
// finished successfully.
|
|
QueueStatus common.ImageBuildState `json:"queue_status,omitempty"`
|
|
}
|
|
|
|
// DeepCopy creates a copy of the ImageBuild structure
|
|
func (ib *ImageBuild) DeepCopy() ImageBuild {
|
|
var newManifestPtr *osbuild.Manifest = nil
|
|
if ib.Manifest != nil {
|
|
manifestCopy := *ib.Manifest
|
|
newManifestPtr = &manifestCopy
|
|
}
|
|
var newTargets []*target.Target
|
|
for _, t := range ib.Targets {
|
|
newTarget := *t
|
|
newTargets = append(newTargets, &newTarget)
|
|
}
|
|
// Create new image build struct
|
|
return ImageBuild{
|
|
Id: ib.Id,
|
|
QueueStatus: ib.QueueStatus,
|
|
ImageType: ib.ImageType,
|
|
Manifest: newManifestPtr,
|
|
Targets: newTargets,
|
|
JobCreated: ib.JobCreated,
|
|
JobStarted: ib.JobStarted,
|
|
JobFinished: ib.JobFinished,
|
|
Size: ib.Size,
|
|
JobId: ib.JobId,
|
|
}
|
|
}
|
|
|
|
func (ib *ImageBuild) GetLocalTargetOptions() *target.LocalTargetOptions {
|
|
for _, t := range ib.Targets {
|
|
switch options := t.Options.(type) {
|
|
case *target.LocalTargetOptions:
|
|
return options
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// A Compose represent the task of building a set of images from a single blueprint.
|
|
// It contains all the information necessary to generate the inputs for the job, as
|
|
// well as the job's state.
|
|
type Compose struct {
|
|
Blueprint *blueprint.Blueprint `json:"blueprint"`
|
|
ImageBuilds []ImageBuild `json:"image_builds"`
|
|
}
|
|
|
|
// DeepCopy creates a copy of the Compose structure
|
|
func (c *Compose) DeepCopy() Compose {
|
|
var newBpPtr *blueprint.Blueprint = nil
|
|
if c.Blueprint != nil {
|
|
bpCopy := *c.Blueprint
|
|
newBpPtr = &bpCopy
|
|
}
|
|
newImageBuilds := []ImageBuild{}
|
|
for _, ib := range c.ImageBuilds {
|
|
newImageBuilds = append(newImageBuilds, ib.DeepCopy())
|
|
}
|
|
return Compose{
|
|
Blueprint: newBpPtr,
|
|
ImageBuilds: newImageBuilds,
|
|
}
|
|
}
|
|
|
|
// UpdateState changes a state of a single image build inside the Compose
|
|
func (c *Compose) UpdateState(imageBuildId int, newState common.ImageBuildState) error {
|
|
switch newState {
|
|
case common.IBWaiting:
|
|
return &StateTransitionError{"image build cannot be moved into waiting state"}
|
|
case common.IBRunning:
|
|
if c.ImageBuilds[imageBuildId].QueueStatus == common.IBWaiting || c.ImageBuilds[imageBuildId].QueueStatus == common.IBRunning {
|
|
c.ImageBuilds[imageBuildId].QueueStatus = newState
|
|
} else {
|
|
return &StateTransitionError{"only waiting image build can be transitioned into running state"}
|
|
}
|
|
case common.IBFinished, common.IBFailed:
|
|
if c.ImageBuilds[imageBuildId].QueueStatus == common.IBRunning {
|
|
c.ImageBuilds[imageBuildId].QueueStatus = newState
|
|
for _, t := range c.ImageBuilds[imageBuildId].Targets {
|
|
t.Status = newState
|
|
}
|
|
} else {
|
|
return &StateTransitionError{"only running image build can be transitioned into finished or failed state"}
|
|
}
|
|
default:
|
|
return &StateTransitionError{"invalid state"}
|
|
}
|
|
return nil
|
|
}
|