Skip to content

Commit

Permalink
imagetools: give imagetools create a progress bar
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Chadwell <[email protected]>
  • Loading branch information
jedevc committed May 24, 2022
1 parent 4d6a1ca commit 07b2bec
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
46 changes: 38 additions & 8 deletions commands/imagetools/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"encoding/json"
"fmt"
"os"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/docker/buildx/store"
"github.com/docker/buildx/store/storeutil"
"github.com/docker/buildx/util/imagetools"
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/util/appcontext"
Expand All @@ -25,6 +27,7 @@ type createOptions struct {
tags []string
dryrun bool
actionAppend bool
progress string
}

func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
Expand Down Expand Up @@ -177,18 +180,44 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
// new resolver cause need new auth
r = imagetools.New(imageopt)

ctx2, cancel := context.WithCancel(context.TODO())
defer cancel()
printer := progress.NewPrinter(ctx2, os.Stderr, os.Stderr, in.progress)

eg, _ := errgroup.WithContext(ctx)
pw := progress.WithPrefix(printer, "internal", true)

for _, t := range tags {
if err := r.Copy(ctx, srcs, t); err != nil {
return err
}
t := t
eg.Go(func() error {
progress.Wrap(fmt.Sprintf("pushing %s", t.String()), pw.Write, func(sub progress.SubLogger) error {
eg2, _ := errgroup.WithContext(ctx)
for _, s := range srcs {
s := s
eg2.Go(func() error {
sub.Log(1, []byte(fmt.Sprintf("copying %s from %s to %s\n", s.Desc.Digest.String(), s.Ref.String(), t.String())))
return r.Copy(ctx, s, t)
})
}

if err := eg2.Wait(); err != nil {
return err
}
sub.Log(1, []byte(fmt.Sprintf("pushing %s to %s\n", desc.Digest.String(), t.String())))
return r.Push(ctx, t, desc, dt)
})

return nil
})
}

if err := r.Push(ctx, t, desc, dt); err != nil {
return err
}
fmt.Println(t.String())
err = eg.Wait()
err1 := printer.Wait()
if err == nil {
err = err1
}

return nil
return err
}

func parseSources(in []string) ([]*imagetools.Source, error) {
Expand Down Expand Up @@ -261,6 +290,7 @@ func createCmd(dockerCli command.Cli, opts RootOptions) *cobra.Command {
flags.StringArrayVarP(&options.tags, "tag", "t", []string{}, "Set reference for new image")
flags.BoolVar(&options.dryrun, "dry-run", false, "Show final image instead of pushing")
flags.BoolVar(&options.actionAppend, "append", false, "Append to existing manifest")
flags.StringVar(&options.progress, "progress", "auto", `Set type of progress output ("auto", "plain", "tty"). Use plain to show container output`)

return cmd
}
Expand Down
1 change: 1 addition & 0 deletions docs/reference/buildx_imagetools_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Create a new image based on source images
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
| [`--dry-run`](#dry-run) | | | Show final image instead of pushing |
| [`-f`](#file), [`--file`](#file) | `stringArray` | | Read source descriptor from file |
| `--progress` | `string` | `auto` | Set type of progress output (`auto`, `plain`, `tty`). Use plain to show container output |
| [`-t`](#tag), [`--tag`](#tag) | `stringArray` | | Set reference for new image |


Expand Down
21 changes: 9 additions & 12 deletions util/imagetools/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,23 @@ func (r *Resolver) Push(ctx context.Context, ref reference.Named, desc ocispec.D
return err
}

func (r *Resolver) Copy(ctx context.Context, srcs []*Source, dest reference.Named) error {
func (r *Resolver) Copy(ctx context.Context, src *Source, dest reference.Named) error {
dest = reference.TagNameOnly(dest)
p, err := r.resolver().Pusher(ctx, dest.String())
if err != nil {
return err
}

for _, src := range srcs {
srcRef := reference.TagNameOnly(src.Ref)
f, err := r.resolver().Fetcher(ctx, srcRef.String())
if err != nil {
return err
}

err = contentutil.CopyChain(ctx, contentutil.FromPusher(p), contentutil.FromFetcher(f), src.Desc)
if err != nil {
return err
}
srcRef := reference.TagNameOnly(src.Ref)
f, err := r.resolver().Fetcher(ctx, srcRef.String())
if err != nil {
return err
}

err = contentutil.CopyChain(ctx, contentutil.FromPusher(p), contentutil.FromFetcher(f), src.Desc)
if err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit 07b2bec

Please sign in to comment.