debian-forge-composer/cmd/osbuild-worker/osbuild.go
Tom Gundersen 0417c6d8bb 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>
2020-06-03 00:30:01 +02:00

68 lines
1.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io"
"os/exec"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro"
)
type OSBuildError struct {
Message string
Result *common.ComposeResult
}
func (e *OSBuildError) Error() string {
return e.Message
}
func RunOSBuild(manifest distro.Manifest, store, outputDirectory string, errorWriter io.Writer) (*common.ComposeResult, error) {
cmd := exec.Command(
"osbuild",
"--store", store,
"--output-directory", outputDirectory,
"--json", "-",
)
cmd.Stderr = errorWriter
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("error setting up stdin for osbuild: %v", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("error setting up stdout for osbuild: %v", err)
}
err = cmd.Start()
if err != nil {
return nil, fmt.Errorf("error starting osbuild: %v", err)
}
err = json.NewEncoder(stdin).Encode(manifest)
if err != nil {
return nil, fmt.Errorf("error encoding osbuild pipeline: %v", err)
}
// FIXME: handle or comment this possible error
_ = stdin.Close()
var result common.ComposeResult
err = json.NewDecoder(stdout).Decode(&result)
if err != nil {
return nil, fmt.Errorf("error decoding osbuild output: %#v", err)
}
err = cmd.Wait()
if err != nil {
return nil, &OSBuildError{
Message: fmt.Sprintf("running osbuild failed: %v", err),
Result: &result,
}
}
return &result, nil
}