debian-forge-composer/internal/osbuild/osbuild-exec.go
Christian Kellner 2c0594629f osbuild: add extraEnv argument to RunOSBuild
This adds the ability to supply extra environment variables to
the osbuild process.
2022-07-25 21:21:44 +02:00

86 lines
2 KiB
Go

package osbuild
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, extraEnv []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
}
if len(extraEnv) > 0 {
cmd.Env = append(os.Environ(), extraEnv...)
}
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
}