distro: make the osbuild package internal to the distros

Rather than Manifest() returning an osbuild.Manifest object, introduce a
new distro.Manifest object which represents it as an opaque, JSON
serializable object. This new type has the following properties:

1) its serialization is compatible with the input to osbuild,
2) any valid osbuild input can be deserialized into it, and
3) marshalling and unmarshaling to and from JSON is lossless.

This means that even as we change the subset of valid osbulid manifests
that we support, we can still load any previous state from disk, and it
will continue to work just as before, even though we can no longer
deserialize it into our internal notion of osbuild.Manifest.

This fixes the underlying problem of which #685 was a symptom.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2020-05-31 20:11:06 +02:00
parent 4aced4e749
commit 0417c6d8bb
18 changed files with 101 additions and 86 deletions

View file

@ -7,7 +7,6 @@ import (
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/osbuild/osbuild-composer/internal/target"
)
@ -23,7 +22,7 @@ func (ste *StateTransitionError) Error() string {
type ImageBuild struct {
ID int
ImageType distro.ImageType
Manifest osbuild.Manifest
Manifest distro.Manifest
Targets []*target.Target
JobCreated time.Time
JobStarted time.Time

View file

@ -70,7 +70,7 @@ func FixtureBase() *Store {
ImageBuild: ImageBuild{
QueueStatus: common.IBWaiting,
ImageType: imgType,
Manifest: *manifest,
Manifest: manifest,
Targets: []*target.Target{localTarget, awsTarget},
JobCreated: date,
},
@ -80,7 +80,7 @@ func FixtureBase() *Store {
ImageBuild: ImageBuild{
QueueStatus: common.IBRunning,
ImageType: imgType,
Manifest: *manifest,
Manifest: manifest,
Targets: []*target.Target{localTarget},
JobCreated: date,
JobStarted: date,
@ -91,7 +91,7 @@ func FixtureBase() *Store {
ImageBuild: ImageBuild{
QueueStatus: common.IBFinished,
ImageType: imgType,
Manifest: *manifest,
Manifest: manifest,
Targets: []*target.Target{localTarget, awsTarget},
JobCreated: date,
JobStarted: date,
@ -103,7 +103,7 @@ func FixtureBase() *Store {
ImageBuild: ImageBuild{
QueueStatus: common.IBFailed,
ImageType: imgType,
Manifest: *manifest,
Manifest: manifest,
Targets: []*target.Target{localTarget, awsTarget},
JobCreated: date,
JobStarted: date,
@ -173,7 +173,7 @@ func FixtureFinished() *Store {
ImageBuild: ImageBuild{
QueueStatus: common.IBFinished,
ImageType: imgType,
Manifest: *manifest,
Manifest: manifest,
Targets: []*target.Target{localTarget, awsTarget},
JobCreated: date,
},
@ -183,7 +183,7 @@ func FixtureFinished() *Store {
ImageBuild: ImageBuild{
QueueStatus: common.IBFinished,
ImageType: imgType,
Manifest: *manifest,
Manifest: manifest,
Targets: []*target.Target{localTarget},
JobCreated: date,
JobStarted: date,
@ -194,7 +194,7 @@ func FixtureFinished() *Store {
ImageBuild: ImageBuild{
QueueStatus: common.IBFailed,
ImageType: imgType,
Manifest: *manifest,
Manifest: manifest,
Targets: []*target.Target{localTarget, awsTarget},
JobCreated: date,
JobStarted: date,

View file

@ -1,7 +1,6 @@
package store
import (
"encoding/json"
"errors"
"log"
"sort"
@ -11,7 +10,6 @@ import (
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/osbuild/osbuild-composer/internal/target"
)
@ -41,7 +39,7 @@ type composesV0 map[uuid.UUID]composeV0
type imageBuildV0 struct {
ID int `json:"id"`
ImageType string `json:"image_type"`
Manifest json.RawMessage `json:"manifest"`
Manifest distro.Manifest `json:"manifest"`
Targets []*target.Target `json:"targets"`
JobCreated time.Time `json:"job_created"`
JobStarted time.Time `json:"job_started"`
@ -118,12 +116,6 @@ func newImageBuildFromV0(imageBuildStruct imageBuildV0, arch distro.Arch) (Image
// on upgrades.
return ImageBuild{}, errors.New("invalid Image Type string")
}
var manifest osbuild.Manifest
err := json.Unmarshal(imageBuildStruct.Manifest, &manifest)
if err != nil {
// The JSON object is not a valid manifest, this may happen on upgrades.
return ImageBuild{}, errors.New("invalid manifest")
}
// Backwards compatibility: fail all builds that are queued or
// running. Jobs status is now handled outside of the store
// (and the compose). The fields are kept so that previously
@ -136,7 +128,7 @@ func newImageBuildFromV0(imageBuildStruct imageBuildV0, arch distro.Arch) (Image
return ImageBuild{
ID: imageBuildStruct.ID,
ImageType: imgType,
Manifest: manifest,
Manifest: imageBuildStruct.Manifest,
Targets: imageBuildStruct.Targets,
JobCreated: imageBuildStruct.JobCreated,
JobStarted: imageBuildStruct.JobStarted,
@ -262,18 +254,13 @@ func newWorkspaceV0(workspace map[string]blueprint.Blueprint) workspaceV0 {
func newComposeV0(compose Compose) composeV0 {
bp := compose.Blueprint.DeepCopy()
manifest, err := json.Marshal(compose.ImageBuild.Manifest)
if err != nil {
panic(err)
}
rawManifest := json.RawMessage(manifest)
return composeV0{
Blueprint: &bp,
ImageBuilds: []imageBuildV0{
{
ID: compose.ImageBuild.ID,
ImageType: imageTypeToCompatString(compose.ImageBuild.ImageType),
Manifest: rawManifest,
Manifest: compose.ImageBuild.Manifest,
Targets: compose.ImageBuild.Targets,
JobCreated: compose.ImageBuild.JobCreated,
JobStarted: compose.ImageBuild.JobStarted,

View file

@ -15,7 +15,6 @@ import (
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/jsondb"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common"
@ -332,7 +331,7 @@ func (s *Store) GetAllComposes() map[uuid.UUID]Compose {
return composes
}
func (s *Store) PushCompose(composeID uuid.UUID, manifest osbuild.Manifest, imageType distro.ImageType, bp *blueprint.Blueprint, size uint64, targets []*target.Target, jobId uuid.UUID) error {
func (s *Store) PushCompose(composeID uuid.UUID, manifest distro.Manifest, imageType distro.ImageType, bp *blueprint.Blueprint, size uint64, targets []*target.Target, jobId uuid.UUID) error {
if _, exists := s.GetCompose(composeID); exists {
panic("a compose with this id already exists")
}
@ -362,7 +361,7 @@ func (s *Store) PushCompose(composeID uuid.UUID, manifest osbuild.Manifest, imag
// PushTestCompose is used for testing
// Set testSuccess to create a fake successful compose, otherwise it will create a failed compose
// It does not actually run a compose job
func (s *Store) PushTestCompose(composeID uuid.UUID, manifest osbuild.Manifest, imageType distro.ImageType, bp *blueprint.Blueprint, size uint64, targets []*target.Target, testSuccess bool) error {
func (s *Store) PushTestCompose(composeID uuid.UUID, manifest distro.Manifest, imageType distro.ImageType, bp *blueprint.Blueprint, size uint64, targets []*target.Target, testSuccess bool) error {
if targets == nil {
targets = []*target.Target{}
}