Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main,osbuildprogress: add --progress=text,debug support (COMPOSER-2394) #743

Merged
merged 8 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bib/cmd/bootc-image-builder/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func uploadAMI(path, targetArch string, flags *pflag.FlagSet) error {
// similar. Eventually we may provide json progress here too.
var pbar *pb.ProgressBar
switch progress {
case "text":
case "", "plain", "term":
pbar = pb.New(0)
}

Expand Down
57 changes: 45 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,17 @@ func saveManifest(ms manifest.OSBuildManifest, fpath string) error {
return nil
}

func manifestFromCobra(cmd *cobra.Command, args []string) ([]byte, *mTLSConfig, error) {
// manifestFromCobra generate an osbuild manifest from a cobra commandline.
//
// It takes an unstarted progres bar and will start it at the right
// point (it cannot be started yet to avoid the "podman pull" progress
// and our progress fighting). The caller is responsible for stopping
// the progress bar (this function cannot know what else needs to happen
// after manifest generation).
//
// TODO: provide a podman progress reader to integrate the podman progress
// into our progress.
func manifestFromCobra(cmd *cobra.Command, args []string, pbar progress.ProgressBar) ([]byte, *mTLSConfig, error) {
cntArch := arch.Current()

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

pbar.SetPulseMsgf("Manifest generation step")
pbar.Start()
ondrejbudai marked this conversation as resolved.
Show resolved Hide resolved

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

func cmdManifest(cmd *cobra.Command, args []string) error {
mf, _, err := manifestFromCobra(cmd, args)
pbar, err := progress.New("")
if err != nil {
// this should never happen
return fmt.Errorf("cannot create progress bar: %w", err)
}
defer pbar.Stop()

mf, _, err := manifestFromCobra(cmd, args, pbar)
if err != nil {
return fmt.Errorf("cannot generate manifest: %w", err)
}
Expand Down Expand Up @@ -383,6 +403,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 +436,19 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
return fmt.Errorf("chowning is not allowed in output directory")
}

pbar, err := progress.New(progressType)
if err != nil {
return fmt.Errorf("cannto create progress bar: %w", err)
}
defer pbar.Stop()

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.SetMessagef("Generating manifest %s", manifest_fname)
mf, mTLS, err := manifestFromCobra(cmd, args, pbar)
if err != nil {
return fmt.Errorf("cannot build manifest: %w", err)
}
fmt.Print("DONE\n")
pbar.SetMessagef("Done generating manifest")

// collect pipeline exports for each image type
imageTypes, err := imagetypes.New(imgTypes...)
Expand All @@ -434,7 +461,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.SetPulseMsgf("Image building step")
pbar.SetMessagef("Building %s", manifest_fname)

var osbuildEnv []string
if !canChown {
Expand All @@ -453,13 +481,17 @@ 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.SetMessagef("Build complete!")
if upload {
// XXX: pass our own progress.ProgressBar here
// *for now* just stop our own progress and let the uploadAMI
// progress take over - but we really need to fix this in a
// followup
pbar.Stop()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is addressed in #763

for idx, imgType := range imgTypes {
switch imgType {
case "ami":
Expand All @@ -472,7 +504,7 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
}
}
} else {
fmt.Printf("Results saved in\n%s\n", outputDir)
pbar.SetMessagef("Results saved in %s", outputDir)
}

if err := chownR(outputDir, chown); err != nil {
Expand Down Expand Up @@ -607,8 +639,9 @@ 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")
buildCmd.Flags().String("store", "/store", "osbuild store for intermediate pipeline trees")
//TODO: add json progress for higher level tools like "podman bootc"
buildCmd.Flags().String("progress", "", "type of progress bar to use")
// flag rules
for _, dname := range []string{"output", "store", "rpmmd"} {
if err := buildCmd.MarkFlagDirname(dname); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion bib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ require (
github.com/letsencrypt/boulder v0.0.0-20240418210053-89b07f4543e0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions bib/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
Expand Down
19 changes: 19 additions & 0 deletions bib/internal/progress/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package progress

import (
"io"
)

type (
TerminalProgressBar = terminalProgressBar
DebugProgressBar = debugProgressBar
PlainProgressBar = plainProgressBar
)

func MockOsStderr(w io.Writer) (restore func()) {
saved := osStderr
osStderr = w
return func() {
osStderr = saved
}
}
Loading
Loading