osbuild: move result serialization from common

In the same way `osbuild.Manifest` is the input to the osbuild API,
`osbuild.Result` is the output. Move it to the `osbuild` package where
it belongs.

This is not a functional change.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2020-08-25 21:59:39 +01:00 committed by Ondřej Budai
parent 360bf2aa04
commit ac5f69e757
7 changed files with 23 additions and 18 deletions

View file

@ -17,6 +17,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/osbuild/osbuild-composer/internal/target" "github.com/osbuild/osbuild-composer/internal/target"
"github.com/osbuild/osbuild-composer/internal/upload/awsupload" "github.com/osbuild/osbuild-composer/internal/upload/awsupload"
"github.com/osbuild/osbuild-composer/internal/upload/azure" "github.com/osbuild/osbuild-composer/internal/upload/azure"
@ -86,7 +87,7 @@ func openAsStreamOptimizedVmdk(imagePath string) (*os.File, error) {
return f, err return f, err
} }
func RunJob(job *worker.Job, store string, uploadFunc func(uuid.UUID, string, io.Reader) error) (*common.ComposeResult, error) { func RunJob(job *worker.Job, store string, uploadFunc func(uuid.UUID, string, io.Reader) error) (*osbuild.Result, error) {
outputDirectory, err := ioutil.TempDir("/var/tmp", "osbuild-worker-*") outputDirectory, err := ioutil.TempDir("/var/tmp", "osbuild-worker-*")
if err != nil { if err != nil {
return nil, fmt.Errorf("error creating temporary output directory: %v", err) return nil, fmt.Errorf("error creating temporary output directory: %v", err)
@ -279,7 +280,7 @@ func main() {
// This can happen in cases when OSBuild crashes and doesn't produce // This can happen in cases when OSBuild crashes and doesn't produce
// a meaningful output. E.g. when the machine runs of disk space. // a meaningful output. E.g. when the machine runs of disk space.
if result == nil { if result == nil {
result = &common.ComposeResult{ result = &osbuild.Result{
Success: false, Success: false,
} }
} }

View file

@ -6,20 +6,20 @@ import (
"io" "io"
"os/exec" "os/exec"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/osbuild"
) )
type OSBuildError struct { type OSBuildError struct {
Message string Message string
Result *common.ComposeResult Result *osbuild.Result
} }
func (e *OSBuildError) Error() string { func (e *OSBuildError) Error() string {
return e.Message return e.Message
} }
func RunOSBuild(manifest distro.Manifest, store, outputDirectory string, errorWriter io.Writer) (*common.ComposeResult, error) { func RunOSBuild(manifest distro.Manifest, store, outputDirectory string, errorWriter io.Writer) (*osbuild.Result, error) {
cmd := exec.Command( cmd := exec.Command(
"osbuild", "osbuild",
"--store", store, "--store", store,
@ -50,7 +50,7 @@ func RunOSBuild(manifest distro.Manifest, store, outputDirectory string, errorWr
// FIXME: handle or comment this possible error // FIXME: handle or comment this possible error
_ = stdin.Close() _ = stdin.Close()
var result common.ComposeResult var result osbuild.Result
err = json.NewDecoder(stdout).Decode(&result) err = json.NewDecoder(stdout).Decode(&result)
if err != nil { if err != nil {
return nil, fmt.Errorf("error decoding osbuild output: %#v", err) return nil, fmt.Errorf("error decoding osbuild output: %#v", err)

View file

@ -1,4 +1,4 @@
package common package osbuild
import ( import (
"encoding/json" "encoding/json"
@ -26,7 +26,7 @@ type build struct {
Success bool `json:"success"` Success bool `json:"success"`
} }
type ComposeResult struct { type Result struct {
TreeID string `json:"tree_id"` TreeID string `json:"tree_id"`
OutputID string `json:"output_id"` OutputID string `json:"output_id"`
Build *build `json:"build"` Build *build `json:"build"`
@ -35,7 +35,7 @@ type ComposeResult struct {
Success bool `json:"success"` Success bool `json:"success"`
} }
func (cr *ComposeResult) Write(writer io.Writer) error { func (cr *Result) Write(writer io.Writer) error {
if cr.Build == nil && len(cr.Stages) == 0 && cr.Assembler == nil { if cr.Build == nil && len(cr.Stages) == 0 && cr.Assembler == nil {
fmt.Fprintf(writer, "The compose result is empty.\n") fmt.Fprintf(writer, "The compose result is empty.\n")
} }

View file

@ -1,9 +1,10 @@
package common package osbuild
import ( import (
"bytes" "bytes"
"github.com/stretchr/testify/assert"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestWriteFull(t *testing.T) { func TestWriteFull(t *testing.T) {
@ -30,7 +31,7 @@ func TestWriteFull(t *testing.T) {
Output: "Done", Output: "Done",
} }
testComposeResult := ComposeResult{ testComposeResult := Result{
TreeID: "TreeID", TreeID: "TreeID",
OutputID: "OutputID", OutputID: "OutputID",
Build: &testBuild, Build: &testBuild,
@ -71,7 +72,7 @@ Done
func TestWriteEmpty(t *testing.T) { func TestWriteEmpty(t *testing.T) {
testComposeResult := ComposeResult{} testComposeResult := Result{}
var b bytes.Buffer var b bytes.Buffer
assert.NoError(t, testComposeResult.Write(&b)) assert.NoError(t, testComposeResult.Write(&b))

View file

@ -27,6 +27,7 @@ import (
"github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/jobqueue" "github.com/osbuild/osbuild-composer/internal/jobqueue"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/osbuild/osbuild-composer/internal/rpmmd" "github.com/osbuild/osbuild-composer/internal/rpmmd"
"github.com/osbuild/osbuild-composer/internal/store" "github.com/osbuild/osbuild-composer/internal/store"
"github.com/osbuild/osbuild-composer/internal/target" "github.com/osbuild/osbuild-composer/internal/target"
@ -165,7 +166,7 @@ type composeStatus struct {
Queued time.Time Queued time.Time
Started time.Time Started time.Time
Finished time.Time Finished time.Time
Result *common.ComposeResult Result *osbuild.Result
} }
// Returns the state of the image in `compose` and the times the job was // Returns the state of the image in `compose` and the times the job was
@ -194,7 +195,7 @@ func (api *API) getComposeStatus(compose store.Compose) *composeStatus {
Queued: compose.ImageBuild.JobCreated, Queued: compose.ImageBuild.JobCreated,
Started: compose.ImageBuild.JobStarted, Started: compose.ImageBuild.JobStarted,
Finished: compose.ImageBuild.JobFinished, Finished: compose.ImageBuild.JobFinished,
Result: &common.ComposeResult{}, Result: &osbuild.Result{},
} }
} }

View file

@ -15,6 +15,7 @@ import (
"github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/osbuild/osbuild-composer/internal/target" "github.com/osbuild/osbuild-composer/internal/target"
) )
@ -111,7 +112,7 @@ func (c *Client) JobCanceled(job *Job) bool {
return jr.Canceled return jr.Canceled
} }
func (c *Client) UpdateJob(job *Job, status common.ImageBuildState, result *common.ComposeResult) error { func (c *Client) UpdateJob(job *Job, status common.ImageBuildState, result *osbuild.Result) error {
var b bytes.Buffer var b bytes.Buffer
err := json.NewEncoder(&b).Encode(&updateJobRequest{status, result}) err := json.NewEncoder(&b).Encode(&updateJobRequest{status, result})
if err != nil { if err != nil {

View file

@ -5,6 +5,7 @@ import (
"github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/osbuild/osbuild-composer/internal/target" "github.com/osbuild/osbuild-composer/internal/target"
) )
@ -18,7 +19,7 @@ type OSBuildJob struct {
} }
type OSBuildJobResult struct { type OSBuildJobResult struct {
OSBuildOutput *common.ComposeResult `json:"osbuild_output,omitempty"` OSBuildOutput *osbuild.Result `json:"osbuild_output,omitempty"`
} }
// //
@ -49,7 +50,7 @@ type jobResponse struct {
type updateJobRequest struct { type updateJobRequest struct {
Status common.ImageBuildState `json:"status"` Status common.ImageBuildState `json:"status"`
Result *common.ComposeResult `json:"result"` Result *osbuild.Result `json:"result"`
} }
type updateJobResponse struct { type updateJobResponse struct {