Skip to content

Commit

Permalink
wip: progress reporting via jsonseq-monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Nov 18, 2024
1 parent 2971ef5 commit 089cf7c
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions cmd/image-builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,68 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"

"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/osbuildmonitor"
)

// XXX: merge back into images/pkg/osbuild/osbuild-exec.go or
// into osbuildmonitor
func runOSBuild(manifest []byte, store, outputDirectory string, exports, extraEnv []string) error {
rp, wp, err := os.Pipe()
if err != nil {
return fmt.Errorf("cannot create pipe for osbuild: %w", err)
}
defer rp.Close()
defer wp.Close()

cmd := exec.Command(
"osbuild",
"--store", store,
"--output-directory", outputDirectory,
"--monitor=JSONSeqMonitor",
"--monitor-fd=3",
"-",
)
for _, export := range exports {
cmd.Args = append(cmd.Args, "--export", export)
}

cmd.Env = append(os.Environ(), extraEnv...)
cmd.Stdin = bytes.NewBuffer(manifest)
cmd.Stderr = os.Stderr
// we could use "--json" here and would get the build-result
// exported here
cmd.Stdout = nil
cmd.ExtraFiles = []*os.File{wp}

if err := cmd.Start(); err != nil {
return fmt.Errorf("error starting osbuild: %v", err)
}
wp.Close()

scanner := osbuildmonitor.NewStatusScanner(rp)
for {
status, err := scanner.Status()
if err != nil {
return err
}
if status == nil {
break
}
// XXX: add progress bar
fmt.Printf("[%s] %s\n", status.Timestamp.Format("2006-01-02 15:04:05"), status.Trace)
}

if err := cmd.Wait(); err != nil {
return fmt.Errorf("error running osbuild: %w", err)
}

return nil
}

func buildImage(out io.Writer, distroName, imgTypeStr, outputFilename string) error {
// cross arch building is not possible, we would have to download
// a pre-populated buildroot (tar,container) with rpm for that
Expand All @@ -33,7 +89,5 @@ func buildImage(out io.Writer, distroName, imgTypeStr, outputFilename string) er
outputDir := "."
buildName := fmt.Sprintf("%s-%s-%s", distroName, imgTypeStr, archStr)
jobOutputDir := filepath.Join(outputDir, buildName)
// XXX: support stremaing via statusWriter
_, err = osbuild.RunOSBuild(mf.Bytes(), osbuildStoreDir, jobOutputDir, imgType.Exports(), nil, nil, false, os.Stderr)
return err
return runOSBuild(mf.Bytes(), osbuildStoreDir, jobOutputDir, imgType.Exports(), nil)
}

0 comments on commit 089cf7c

Please sign in to comment.