osbuild-worker/osbuild: move out execution helper
Move the execution helper for osbuild into the osbuild2 package so it can be reused. In the process, generalise it slightly.
This commit is contained in:
parent
6ae65f1951
commit
1b924ae30c
3 changed files with 30 additions and 20 deletions
81
internal/osbuild2/osbuild-exec.go
Normal file
81
internal/osbuild2/osbuild-exec.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package osbuild2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// Run an instance of osbuild, returning a parsed osbuild.Result.
|
||||
//
|
||||
// Note that osbuild returns non-zero when the pipeline fails. This function
|
||||
// does not return an error in this case. Instead, the failure is communicated
|
||||
// with its corresponding logs through osbuild.Result.
|
||||
func RunOSBuild(manifest []byte, store, outputDirectory string, exports, checkpoints []string, result bool, errorWriter io.Writer) (*Result, error) {
|
||||
var stdoutBuffer bytes.Buffer
|
||||
var res Result
|
||||
|
||||
cmd := exec.Command(
|
||||
"osbuild",
|
||||
"--store", store,
|
||||
"--output-directory", outputDirectory,
|
||||
"-",
|
||||
)
|
||||
|
||||
for _, export := range exports {
|
||||
cmd.Args = append(cmd.Args, "--export", export)
|
||||
}
|
||||
|
||||
for _, checkpoint := range checkpoints {
|
||||
cmd.Args = append(cmd.Args, "--checkpoint", checkpoint)
|
||||
}
|
||||
|
||||
if result {
|
||||
cmd.Args = append(cmd.Args, "--json")
|
||||
cmd.Stdout = &stdoutBuffer
|
||||
} else {
|
||||
cmd.Stdout = os.Stdout
|
||||
}
|
||||
cmd.Stderr = errorWriter
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error setting up stdin for osbuild: %v", err)
|
||||
}
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error starting osbuild: %v", err)
|
||||
}
|
||||
|
||||
_, err = stdin.Write(manifest)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error writing osbuild manifest: %v", err)
|
||||
}
|
||||
|
||||
err = stdin.Close()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error closing osbuild's stdin: %v", err)
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
|
||||
if result {
|
||||
// try to decode the output even though the job could have failed
|
||||
decodeErr := json.Unmarshal(stdoutBuffer.Bytes(), &res)
|
||||
if decodeErr != nil {
|
||||
return nil, fmt.Errorf("error decoding osbuild output: %v\nthe raw output:\n%s", decodeErr, stdoutBuffer.String())
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// ignore ExitError if output could be decoded correctly
|
||||
if _, isExitError := err.(*exec.ExitError); !isExitError {
|
||||
return nil, fmt.Errorf("running osbuild failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue