Skip to content

Commit

Permalink
add quiet flag for pull image
Browse files Browse the repository at this point in the history
Signed-off-by: ye.sijun <junnplus@gmail.com>
  • Loading branch information
junnplus committed Dec 7, 2021
1 parent a6992ab commit cce7047
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 25 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,9 @@ Flags:
- :nerd_face: Unlike Docker, this flag can be specified multiple times (`--platform=amd64 --platform=arm64`)
- :nerd_face: `--all-platforms`: Pull content for all platforms
- :nerd_face: `--unpack`: Unpack the image for the current single platform (auto/true/false)
- :whale: `-q, --quiet`: Suppress verbose output

Unimplemented `docker pull` flags: `--all-tags`, `--disable-content-trust` (default true), `--quiet`
Unimplemented `docker pull` flags: `--all-tags`, `--disable-content-trust` (default true)

### :whale: nerdctl push
Push an image to a registry.
Expand Down Expand Up @@ -1081,8 +1082,9 @@ Flags:
- :whale: `--no-log-prefix`: Don't print prefix in logs
- :whale: `--build`: Build images before starting containers.
- :nerd_face: `--ipfs`: Build images with pulling base images from IPFS. See [`./docs/ipfs.md`](./docs/ipfs.md) for details.
- :whale: `--quiet-pull`: Pull without printing progress information

Unimplemented `docker-compose up` (V1) flags: `--quiet-pull`, `--no-deps`, `--force-recreate`, `--always-recreate-deps`, `--no-recreate`,
Unimplemented `docker-compose up` (V1) flags: `--no-deps`, `--force-recreate`, `--always-recreate-deps`, `--no-recreate`,
`--no-start`, `--abort-on-container-exit`, `--attach-dependencies`, `--timeout`, `--renew-anon-volumes`, `--remove-orphans`, `--exit-code-from`,
`--scale`

Expand Down Expand Up @@ -1138,7 +1140,10 @@ Pull service images

Usage: `nerdctl compose pull`

Unimplemented `docker-compose pull` (V1) flags: `--ignore-pull-failures`, `--parallel`, `--no-parallel`, `quiet`, `include-deps`
Flags:
- :whale: `-q, --quiet`: Pull without printing progress information

Unimplemented `docker-compose pull` (V1) flags: `--ignore-pull-failures`, `--parallel`, `--no-parallel`, `include-deps`

### :whale: nerdctl compose push
Push service images
Expand Down
6 changes: 3 additions & 3 deletions cmd/nerdctl/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func getComposer(cmd *cobra.Command, client *containerd.Client) (*composer.Compo
return true, nil
}

o.EnsureImage = func(ctx context.Context, imageName, pullMode, platform string) error {
o.EnsureImage = func(ctx context.Context, imageName, pullMode, platform string, quiet bool) error {
ocispecPlatforms := []ocispec.Platform{platforms.DefaultSpec()}
if platform != "" {
parsed, err := platforms.Parse(platform)
Expand All @@ -182,10 +182,10 @@ func getComposer(cmd *cobra.Command, client *containerd.Client) (*composer.Compo
return err
}
_, imgErr = ipfs.EnsureImage(ctx, client, ipfsClient, cmd.OutOrStdout(), cmd.ErrOrStderr(), snapshotter, scheme, ref,
pullMode, ocispecPlatforms, nil)
pullMode, ocispecPlatforms, nil, quiet)
} else {
_, imgErr = imgutil.EnsureImage(ctx, client, cmd.OutOrStdout(), cmd.ErrOrStderr(), snapshotter, imageName,
pullMode, insecure, ocispecPlatforms, nil)
pullMode, insecure, ocispecPlatforms, nil, quiet)
}
return imgErr
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/nerdctl/compose_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func newComposePullCommand() *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
}
composePullCommand.Flags().BoolP("quiet", "q", false, "Pull without printing progress information")
return composePullCommand
}

Expand All @@ -50,6 +51,10 @@ func composePullAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
po := composer.PullOptions{}
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
}
po := composer.PullOptions{quiet}
return c.Pull(ctx, po)
}
6 changes: 6 additions & 0 deletions cmd/nerdctl/compose_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func newComposeUpCommand() *cobra.Command {
composeUpCommand.Flags().Bool("no-log-prefix", false, "Don't print prefix in logs")
composeUpCommand.Flags().Bool("build", false, "Build images before starting containers.")
composeUpCommand.Flags().Bool("ipfs", false, "Allow pulling base images from IPFS during build")
composeUpCommand.Flags().Bool("quiet-pull", false, "Pull without printing progress information")
return composeUpCommand
}

Expand Down Expand Up @@ -68,6 +69,10 @@ func composeUpAction(cmd *cobra.Command, services []string) error {
if err != nil {
return err
}
quietPull, err := cmd.Flags().GetBool("quiet-pull")
if err != nil {
return err
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
Expand All @@ -86,6 +91,7 @@ func composeUpAction(cmd *cobra.Command, services []string) error {
NoLogPrefix: noLogPrefix,
ForceBuild: build,
IPFS: enableIPFS,
QuietPull: quietPull,
}
return c.Up(ctx, uo, services)
}
9 changes: 7 additions & 2 deletions cmd/nerdctl/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func newPullCommand() *cobra.Command {
pullCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
pullCommand.Flags().Bool("all-platforms", false, "Pull content for all platforms")
// #endregion
pullCommand.Flags().BoolP("quiet", "q", false, "Suppress verbose output")

return pullCommand
}
Expand Down Expand Up @@ -90,18 +91,22 @@ func pullAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
}

if scheme, ref, err := referenceutil.ParseIPFSRefWithScheme(args[0]); err == nil {
ipfsClient, err := httpapi.NewLocalApi()
if err != nil {
return err
}
_, err = ipfs.EnsureImage(ctx, client, ipfsClient, cmd.OutOrStdout(), cmd.ErrOrStderr(), snapshotter, scheme, ref,
"always", ocispecPlatforms, unpack)
"always", ocispecPlatforms, unpack, quiet)
return err
}

_, err = imgutil.EnsureImage(ctx, client, cmd.OutOrStdout(), cmd.ErrOrStderr(), snapshotter, args[0],
"always", insecure, ocispecPlatforms, unpack)
"always", insecure, ocispecPlatforms, unpack, quiet)
return err
}
4 changes: 2 additions & 2 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,13 +719,13 @@ func generateRootfsOpts(ctx context.Context, client *containerd.Client, platform
if err != nil {
return nil, nil, nil, err
}
ensured, err = ipfs.EnsureImage(ctx, client, ipfsClient, cmd.OutOrStdout(), cmd.ErrOrStderr(), snapshotter, scheme, ref, pull, ocispecPlatforms, nil)
ensured, err = ipfs.EnsureImage(ctx, client, ipfsClient, cmd.OutOrStdout(), cmd.ErrOrStderr(), snapshotter, scheme, ref, pull, ocispecPlatforms, nil, false)
if err != nil {
return nil, nil, nil, err
}
} else {
ensured, err = imgutil.EnsureImage(ctx, client, cmd.OutOrStdout(), cmd.ErrOrStderr(), snapshotter, args[0],
pull, insecureRegistry, ocispecPlatforms, nil)
pull, insecureRegistry, ocispecPlatforms, nil, false)
if err != nil {
return nil, nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Options struct {
NetworkExists func(string) (bool, error)
VolumeExists func(string) (bool, error)
ImageExists func(ctx context.Context, imageName string) (bool, error)
EnsureImage func(ctx context.Context, imageName, pullMode, platform string) error
EnsureImage func(ctx context.Context, imageName, pullMode, platform string, quiet bool) error
DebugPrintFull bool // full debug print, may leak secret env var to logs
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/composer/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
)

type PullOptions struct {
Quiet bool
}

func (c *Composer) Pull(ctx context.Context, po PullOptions) error {
Expand All @@ -47,6 +48,9 @@ func (c *Composer) pullServiceImage(ctx context.Context, image string, platform
if platform != "" {
args = append(args, "--platform="+platform)
}
if po.Quiet {
args = append(args, "--quiet")
}
args = append(args, image)

cmd := c.createNerdctlCmd(ctx, append([]string{"pull"}, args...)...)
Expand Down
1 change: 1 addition & 0 deletions pkg/composer/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type UpOptions struct {
NoLogPrefix bool
ForceBuild bool
IPFS bool
QuietPull bool
}

func (c *Composer) Up(ctx context.Context, uo UpOptions, services []string) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/composer/up_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *Composer) upServices(ctx context.Context, parsedServices []*servicepars

// TODO: parallelize loop for ensuring images (make sure not to mess up tty)
for _, ps := range parsedServices {
if err := c.ensureServiceImage(ctx, ps, !uo.NoBuild, uo.ForceBuild, BuildOptions{IPFS: uo.IPFS}); err != nil {
if err := c.ensureServiceImage(ctx, ps, !uo.NoBuild, uo.ForceBuild, BuildOptions{IPFS: uo.IPFS}, uo.QuietPull); err != nil {
return err
}
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (c *Composer) upServices(ctx context.Context, parsedServices []*servicepars
return nil
}

func (c *Composer) ensureServiceImage(ctx context.Context, ps *serviceparser.Service, allowBuild, forceBuild bool, bo BuildOptions) error {
func (c *Composer) ensureServiceImage(ctx context.Context, ps *serviceparser.Service, allowBuild, forceBuild bool, bo BuildOptions, quiet bool) error {
if ps.Build != nil && allowBuild {
if ps.Build.Force || forceBuild {
return c.buildServiceImage(ctx, ps.Image, ps.Build, ps.Unparsed.Platform, bo)
Expand All @@ -116,7 +116,7 @@ func (c *Composer) ensureServiceImage(ctx context.Context, ps *serviceparser.Ser
// even when c.ImageExists returns true, we need to call c.EnsureImage
// because ps.PullMode can be "always".
logrus.Infof("Ensuring image %s", ps.Image)
if err := c.EnsureImage(ctx, ps.Image, ps.PullMode, ps.Unparsed.Platform); err != nil {
if err := c.EnsureImage(ctx, ps.Image, ps.PullMode, ps.Unparsed.Platform, quiet); err != nil {
return err
}
return nil
Expand Down
18 changes: 10 additions & 8 deletions pkg/imgutil/imgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func GetExistingImage(ctx context.Context, client *containerd.Client, snapshotte
// EnsureImage ensures the image.
//
// When insecure is set, skips verifying certs, and also falls back to HTTP when the registry does not speak HTTPS
func EnsureImage(ctx context.Context, client *containerd.Client, stdout, stderr io.Writer, snapshotter, rawRef string, mode PullMode, insecure bool, ocispecPlatforms []ocispec.Platform, unpack *bool) (*EnsuredImage, error) {
func EnsureImage(ctx context.Context, client *containerd.Client, stdout, stderr io.Writer, snapshotter, rawRef string, mode PullMode, insecure bool, ocispecPlatforms []ocispec.Platform, unpack *bool, quiet bool) (*EnsuredImage, error) {
switch mode {
case "always", "missing", "never":
// NOP
Expand Down Expand Up @@ -139,7 +139,7 @@ func EnsureImage(ctx context.Context, client *containerd.Client, stdout, stderr
return nil, err
}

img, err := PullImage(ctx, client, stdout, stderr, snapshotter, resolver, ref, ocispecPlatforms, unpack)
img, err := PullImage(ctx, client, stdout, stderr, snapshotter, resolver, ref, ocispecPlatforms, unpack, quiet)
if err != nil {
if !IsErrHTTPResponseToHTTPSClient(err) {
return nil, err
Expand All @@ -151,7 +151,7 @@ func EnsureImage(ctx context.Context, client *containerd.Client, stdout, stderr
if err != nil {
return nil, err
}
return PullImage(ctx, client, stdout, stderr, snapshotter, resolver, ref, ocispecPlatforms, unpack)
return PullImage(ctx, client, stdout, stderr, snapshotter, resolver, ref, ocispecPlatforms, unpack, quiet)
} else {
logrus.WithError(err).Errorf("server %q does not seem to support HTTPS", refDomain)
logrus.Info("Hint: you may want to try --insecure-registry to allow plain HTTP (if you are in a trusted network)")
Expand All @@ -171,7 +171,7 @@ func IsErrHTTPResponseToHTTPSClient(err error) bool {
}

// PullImage pulls an image using the specified resolver.
func PullImage(ctx context.Context, client *containerd.Client, stdout, stderr io.Writer, snapshotter string, resolver remotes.Resolver, ref string, ocispecPlatforms []ocispec.Platform, unpack *bool) (*EnsuredImage, error) {
func PullImage(ctx context.Context, client *containerd.Client, stdout, stderr io.Writer, snapshotter string, resolver remotes.Resolver, ref string, ocispecPlatforms []ocispec.Platform, unpack *bool, quiet bool) (*EnsuredImage, error) {
ctx, done, err := client.WithLease(ctx)
if err != nil {
return nil, err
Expand All @@ -180,10 +180,12 @@ func PullImage(ctx context.Context, client *containerd.Client, stdout, stderr io

var containerdImage containerd.Image
config := &pull.Config{
Resolver: resolver,
ProgressOutput: stderr,
RemoteOpts: []containerd.RemoteOpt{},
Platforms: ocispecPlatforms, // empty for all-platforms
Resolver: resolver,
RemoteOpts: []containerd.RemoteOpt{},
Platforms: ocispecPlatforms, // empty for all-platforms
}
if !quiet {
config.ProgressOutput = stderr
}

var unpackB bool
Expand Down
4 changes: 2 additions & 2 deletions pkg/ipfs/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
)

// EnsureImage pull the specified image from IPFS.
func EnsureImage(ctx context.Context, client *containerd.Client, ipfsClient iface.CoreAPI, stdout, stderr io.Writer, snapshotter string, scheme string, ref string, mode imgutil.PullMode, ocispecPlatforms []ocispec.Platform, unpack *bool) (*imgutil.EnsuredImage, error) {
func EnsureImage(ctx context.Context, client *containerd.Client, ipfsClient iface.CoreAPI, stdout, stderr io.Writer, snapshotter string, scheme string, ref string, mode imgutil.PullMode, ocispecPlatforms []ocispec.Platform, unpack *bool, quiet bool) (*imgutil.EnsuredImage, error) {
switch mode {
case "always", "missing", "never":
// NOP
Expand Down Expand Up @@ -72,7 +72,7 @@ func EnsureImage(ctx context.Context, client *containerd.Client, ipfsClient ifac
if err != nil {
return nil, err
}
return imgutil.PullImage(ctx, client, stdout, stderr, snapshotter, r, ref, ocispecPlatforms, unpack)
return imgutil.PullImage(ctx, client, stdout, stderr, snapshotter, r, ref, ocispecPlatforms, unpack, quiet)
}

// Push pushes the specified image to IPFS.
Expand Down

0 comments on commit cce7047

Please sign in to comment.