Skip to content

Commit

Permalink
main,osbuildprogress: add --progress=text,debug support
Browse files Browse the repository at this point in the history
This adds a new `progress` flag that makes use of the
osbuild jsonseq progress information to show progress and
hide the low-level details from the user.
  • Loading branch information
mvo5 committed Dec 5, 2024
1 parent 565bcfd commit d31e3d4
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions bib/cmd/bootc-image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/dnfjson"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/rpmmd"

"github.com/osbuild/bootc-image-builder/bib/internal/buildconfig"
podman_container "github.com/osbuild/bootc-image-builder/bib/internal/container"
"github.com/osbuild/bootc-image-builder/bib/internal/imagetypes"
"github.com/osbuild/bootc-image-builder/bib/internal/progress"
"github.com/osbuild/bootc-image-builder/bib/internal/setup"
"github.com/osbuild/bootc-image-builder/bib/internal/source"
"github.com/osbuild/bootc-image-builder/bib/internal/util"
Expand Down Expand Up @@ -171,7 +171,7 @@ func saveManifest(ms manifest.OSBuildManifest, fpath string) error {
return nil
}

func manifestFromCobra(cmd *cobra.Command, args []string) ([]byte, *mTLSConfig, error) {
func manifestFromCobra(pbar progress.ProgressBar, cmd *cobra.Command, args []string) ([]byte, *mTLSConfig, error) {
cntArch := arch.Current()

imgref := args[0]
Expand Down Expand Up @@ -234,6 +234,9 @@ func manifestFromCobra(cmd *cobra.Command, args []string) ([]byte, *mTLSConfig,
logrus.Debug("Using local container")
}

pbar.SetPulseMsg("Manifest generation step")
pbar.Start()

Check failure on line 238 in bib/cmd/bootc-image-builder/main.go

View workflow job for this annotation

GitHub Actions / ⌨ Lint & unittests

Error return value of `pbar.Start` is not checked (errcheck)

if err := setup.ValidateHasContainerTags(imgref); err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -320,7 +323,8 @@ func manifestFromCobra(cmd *cobra.Command, args []string) ([]byte, *mTLSConfig,
}

func cmdManifest(cmd *cobra.Command, args []string) error {
mf, _, err := manifestFromCobra(cmd, args)
pbar := progress.NewNullProgressBar()
mf, _, err := manifestFromCobra(pbar, cmd, args)
if err != nil {
return fmt.Errorf("cannot generate manifest: %w", err)
}
Expand Down Expand Up @@ -383,6 +387,7 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
osbuildStore, _ := cmd.Flags().GetString("store")
outputDir, _ := cmd.Flags().GetString("output")
targetArch, _ := cmd.Flags().GetString("target-arch")
progressType, _ := cmd.Flags().GetString("progress")

logrus.Debug("Validating environment")
if err := setup.Validate(targetArch); err != nil {
Expand Down Expand Up @@ -415,13 +420,17 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
return fmt.Errorf("chowning is not allowed in output directory")
}

pbar := progress.New(progressType)
if pbar == nil {
return fmt.Errorf("unsupported progress name %q", progressType)
}
manifest_fname := fmt.Sprintf("manifest-%s.json", strings.Join(imgTypes, "-"))
fmt.Printf("Generating manifest %s\n", manifest_fname)
mf, mTLS, err := manifestFromCobra(cmd, args)
pbar.SetMessage("Generating manifest %s", manifest_fname)
mf, mTLS, err := manifestFromCobra(pbar, cmd, args)
if err != nil {
return fmt.Errorf("cannot build manifest: %w", err)
}
fmt.Print("DONE\n")
pbar.SetMessage("Done generating manifest")

// collect pipeline exports for each image type
imageTypes, err := imagetypes.New(imgTypes...)
Expand All @@ -434,7 +443,8 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
return fmt.Errorf("cannot save manifest: %w", err)
}

fmt.Printf("Building %s\n", manifest_fname)
pbar.SetPulseMsg("Image generation step")
pbar.SetMessage("Building %s", manifest_fname)

var osbuildEnv []string
if !canChown {
Expand All @@ -453,12 +463,11 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
osbuildEnv = append(osbuildEnv, envVars...)
}

_, err = osbuild.RunOSBuild(mf, osbuildStore, outputDir, exports, nil, osbuildEnv, false, os.Stderr)
if err != nil {
if err = progress.RunOSBuild(pbar, mf, osbuildStore, outputDir, exports, osbuildEnv); err != nil {
return fmt.Errorf("cannot run osbuild: %w", err)
}

fmt.Println("Build complete!")
pbar.SetMessage("Build complete!")
if upload {
for idx, imgType := range imgTypes {
switch imgType {
Expand All @@ -472,7 +481,7 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
}
}
} else {
fmt.Printf("Results saved in\n%s\n", outputDir)
pbar.SetMessage("Results saved in %s", outputDir)
}

if err := chownR(outputDir, chown); err != nil {
Expand Down Expand Up @@ -607,7 +616,8 @@ func buildCobraCmdline() (*cobra.Command, error) {
buildCmd.Flags().String("aws-region", "", "target region for AWS uploads (only for type=ami)")
buildCmd.Flags().String("chown", "", "chown the ouput directory to match the specified UID:GID")
buildCmd.Flags().String("output", ".", "artifact output directory")
buildCmd.Flags().String("progress", "text", "type of progress bar to use")
// XXX: make this a proper type
buildCmd.Flags().String("progress", "", "type of progress bar to use")
buildCmd.Flags().String("store", "/store", "osbuild store for intermediate pipeline trees")
// flag rules
for _, dname := range []string{"output", "store", "rpmmd"} {
Expand Down

0 comments on commit d31e3d4

Please sign in to comment.