From 12e582ee1a075ae040d6d9ce8fcdfc70c2f88977 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Tue, 9 May 2017 11:56:44 -0400 Subject: [PATCH] Report pull/commit progress by default Have 'from', 'commit', and 'build-using-dockerfile' report progress via stderr (so that capturing output from 'from' and 'commit' still works as expected) unless --quiet is used to suppress the reporting. Closes #94. Signed-off-by: Nalin Dahyabhai Closes: #98 Approved by: rhatdan --- buildah.go | 4 ++++ cmd/buildah/bud.go | 5 ++++- cmd/buildah/commit.go | 12 ++++++++++++ cmd/buildah/from.go | 13 +++++++++++++ commit.go | 6 +++++- common.go | 8 ++++++-- contrib/completions/bash/buildah | 6 ++++++ docs/buildah-bud.md | 6 ++++++ docs/buildah-commit.md | 4 ++++ docs/buildah-from.md | 4 ++++ imagebuildah/build.go | 8 ++++++++ pull.go | 2 +- 12 files changed, 73 insertions(+), 5 deletions(-) diff --git a/buildah.go b/buildah.go index 3e82da93b..38a4eda63 100644 --- a/buildah.go +++ b/buildah.go @@ -3,6 +3,7 @@ package buildah import ( "encoding/json" "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -134,6 +135,9 @@ type BuilderOptions struct { // specified, indicating that the shared, system-wide default policy // should be used. SignaturePolicyPath string + // ReportWriter is an io.Writer which will be used to log the reading + // of the source image from a registry, if we end up pulling the image. + ReportWriter io.Writer } // ImportOptions are used to initialize a Builder. diff --git a/cmd/buildah/bud.go b/cmd/buildah/bud.go index b603bf266..5db1ce5b0 100644 --- a/cmd/buildah/bud.go +++ b/cmd/buildah/bud.go @@ -15,7 +15,7 @@ var ( budFlags = []cli.Flag{ cli.BoolFlag{ Name: "quiet, q", - Usage: "refrain from announcing build instructions", + Usage: "refrain from announcing build instructions and image read/write progress", }, cli.StringFlag{ Name: "registry", @@ -203,6 +203,9 @@ func budCmd(c *cli.Context) error { Runtime: runtime, RuntimeArgs: runtimeFlags, } + if !quiet { + options.ReportWriter = os.Stderr + } return imagebuildah.BuildDockerfiles(store, options, dockerfiles...) } diff --git a/cmd/buildah/commit.go b/cmd/buildah/commit.go index f673f6562..dcd8b024a 100644 --- a/cmd/buildah/commit.go +++ b/cmd/buildah/commit.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "github.com/containers/image/storage" "github.com/containers/image/transports/alltransports" @@ -20,6 +21,10 @@ var ( Name: "signature-policy", Usage: "`pathname` of signature policy file (not usually used)", }, + cli.BoolFlag{ + Name: "quiet, q", + Usage: "don't output progress information when writing images", + }, } commitDescription = "Writes a new image using the container's read-write layer and, if it is based\n on an image, the layers of that image" commitCommand = cli.Command{ @@ -55,6 +60,10 @@ func commitCmd(c *cli.Context) error { if !c.IsSet("disable-compression") || !c.Bool("disable-compression") { compress = archive.Gzip } + quiet := false + if c.IsSet("quiet") { + quiet = c.Bool("quiet") + } store, err := getStore(c) if err != nil { return err @@ -78,6 +87,9 @@ func commitCmd(c *cli.Context) error { Compression: compress, SignaturePolicyPath: signaturePolicy, } + if !quiet { + options.ReportWriter = os.Stderr + } err = builder.Commit(dest, options) if err != nil { return fmt.Errorf("error committing container %q to %q: %v", builder.Container, image, err) diff --git a/cmd/buildah/from.go b/cmd/buildah/from.go index 98bb37728..c7c0e0d0e 100644 --- a/cmd/buildah/from.go +++ b/cmd/buildah/from.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "github.com/projectatomic/buildah" "github.com/urfave/cli" @@ -37,6 +38,10 @@ var ( Name: "signature-policy", Usage: "`pathname` of signature policy file (not usually used)", }, + cli.BoolFlag{ + Name: "quiet, q", + Usage: "don't output progress information when pulling images", + }, } fromDescription = "Creates a new working container, either from scratch or using a specified\n image as a starting point" @@ -91,6 +96,11 @@ func fromCmd(c *cli.Context) error { signaturePolicy = c.String("signature-policy") } + quiet := false + if c.IsSet("quiet") { + quiet = c.Bool("quiet") + } + store, err := getStore(c) if err != nil { return err @@ -103,6 +113,9 @@ func fromCmd(c *cli.Context) error { Registry: registry, SignaturePolicyPath: signaturePolicy, } + if !quiet { + options.ReportWriter = os.Stderr + } builder, err := buildah.NewBuilder(store, options) if err != nil { diff --git a/commit.go b/commit.go index a073add41..21f5bd9e8 100644 --- a/commit.go +++ b/commit.go @@ -2,6 +2,7 @@ package buildah import ( "fmt" + "io" "github.com/containers/image/copy" "github.com/containers/image/docker/reference" @@ -27,6 +28,9 @@ type CommitOptions struct { // the transport to which we're writing the image gives us a way to add // them. AdditionalTags []string + // ReportWriter is an io.Writer which will be used to log the writing + // of the new image. + ReportWriter io.Writer } func expandTags(tags []string) ([]string, error) { @@ -62,7 +66,7 @@ func (b *Builder) Commit(dest types.ImageReference, options CommitOptions) error if err != nil { return err } - err = copy.Image(policyContext, dest, src, getCopyOptions()) + err = copy.Image(policyContext, dest, src, getCopyOptions(options.ReportWriter)) switch dest.Transport().Name() { case storage.Transport.Name(): tags, err := expandTags(options.AdditionalTags) diff --git a/common.go b/common.go index a742dfd73..b138dbc08 100644 --- a/common.go +++ b/common.go @@ -1,12 +1,16 @@ package buildah import ( + "io" + "github.com/containers/image/copy" "github.com/containers/image/types" ) -func getCopyOptions() *copy.Options { - return ©.Options{} +func getCopyOptions(reportWriter io.Writer) *copy.Options { + return ©.Options{ + ReportWriter: reportWriter, + } } func getSystemContext(signaturePolicyPath string) *types.SystemContext { diff --git a/contrib/completions/bash/buildah b/contrib/completions/bash/buildah index 8bbc126a5..96e058dff 100644 --- a/contrib/completions/bash/buildah +++ b/contrib/completions/bash/buildah @@ -287,6 +287,8 @@ return 1 --help -h --disable-compression + --quiet + -q " local options_with_args=" @@ -329,6 +331,8 @@ return 1 -h --pull --pull-always + --quiet + -q " local options_with_args=" @@ -522,6 +526,8 @@ return 1 -h --pull --pull-always + --quiet + -q " local options_with_args=" diff --git a/docs/buildah-bud.md b/docs/buildah-bud.md index 12901c2cf..2024d1fb6 100644 --- a/docs/buildah-bud.md +++ b/docs/buildah-bud.md @@ -67,6 +67,12 @@ Adds global flags for the container rutime. Specifies the name which will be assigned to the resulting image if the build process completes successfully. +**--quiet** + +Suppress output messages which indicate which instruction is being processed, +and of progress when pulling images from a registry, and when writing the +output image. + ## EXAMPLE buildah bud . diff --git a/docs/buildah-commit.md b/docs/buildah-commit.md index d78cbc893..de9621b2d 100644 --- a/docs/buildah-commit.md +++ b/docs/buildah-commit.md @@ -23,6 +23,10 @@ Pathname of a signature policy file to use. It is not recommended that this option be used, as the default behavior of using the system-wide default policy (frequently */etc/containers/policy.json*) is most often preferred. +**--quiet** + +When writing the output image, suppress progress output. + ## EXAMPLE buildah commit containerID diff --git a/docs/buildah-from.md b/docs/buildah-from.md index b2af756ba..ba8c66be8 100644 --- a/docs/buildah-from.md +++ b/docs/buildah-from.md @@ -40,6 +40,10 @@ Pathname of a signature policy file to use. It is not recommended that this option be used, as the default behavior of using the system-wide default policy (frequently */etc/containers/policy.json*) is most often preferred. +**--quiet** + +If an image needs to be pulled from the registry, suppress progress output. + ## EXAMPLE buildah from imagename --pull --registry "myregistry://" diff --git a/imagebuildah/build.go b/imagebuildah/build.go index d5d96fac5..045341623 100644 --- a/imagebuildah/build.go +++ b/imagebuildah/build.go @@ -87,6 +87,10 @@ type BuildOptions struct { // specified, indicating that the shared, system-wide default policy // should be used. SignaturePolicyPath string + // ReportWriter is an io.Writer which will be used to report the + // progress of the (possible) pulling of the source image and the + // writing of the new image. + ReportWriter io.Writer } // Executor is a buildah-based implementation of the imagebuilder.Executor @@ -115,6 +119,7 @@ type Executor struct { volumes imagebuilder.VolumeSet volumeCache map[string]string volumeCacheInfo map[string]os.FileInfo + reportWriter io.Writer } func makeSystemContext(signaturePolicyPath string) *types.SystemContext { @@ -388,6 +393,7 @@ func NewExecutor(store storage.Store, options BuildOptions) (*Executor, error) { log: options.Log, out: options.Out, err: options.Err, + reportWriter: options.ReportWriter, } if exec.err == nil { exec.err = os.Stderr @@ -427,6 +433,7 @@ func (b *Executor) Prepare(ib *imagebuilder.Builder, node *parser.Node, from str PullPolicy: b.pullPolicy, Registry: b.registry, SignaturePolicyPath: b.signaturePolicyPath, + ReportWriter: b.reportWriter, } builder, err := buildah.NewBuilder(b.store, builderOptions) if err != nil { @@ -526,6 +533,7 @@ func (b *Executor) Commit(ib *imagebuilder.Builder) (err error) { Compression: b.compression, SignaturePolicyPath: b.signaturePolicyPath, AdditionalTags: b.additionalTags, + ReportWriter: b.reportWriter, } return b.builder.Commit(imageRef, options) } diff --git a/pull.go b/pull.go index 71ba74aff..4b23da270 100644 --- a/pull.go +++ b/pull.go @@ -54,6 +54,6 @@ func pullImage(store storage.Store, options BuilderOptions, sc *types.SystemCont logrus.Debugf("copying %q to %q", spec, name) - err = copy.Image(policyContext, destRef, srcRef, getCopyOptions()) + err = copy.Image(policyContext, destRef, srcRef, getCopyOptions(options.ReportWriter)) return err }