Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Add --dry-run option to pull cmd
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <noreply@github.com>
  • Loading branch information
satotake authored Jun 25, 2021
1 parent 796d08d commit 91b3e96
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 28 deletions.
6 changes: 6 additions & 0 deletions cmd/compose/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type pullOptions struct {
noParallel bool
includeDeps bool
ignorePullFailures bool
dryRun bool
format string
}

func pullCommand(p *projectOptions, backend api.Service) *cobra.Command {
Expand Down Expand Up @@ -63,6 +65,8 @@ func pullCommand(p *projectOptions, backend api.Service) *cobra.Command {
cmd.Flags().BoolVar(&opts.parallel, "no-parallel", true, "DEPRECATED disable parallel pulling.")
flags.MarkHidden("no-parallel") //nolint:errcheck
cmd.Flags().BoolVar(&opts.ignorePullFailures, "ignore-pull-failures", false, "Pull what it can and ignores images with pull failures")
cmd.Flags().BoolVar(&opts.dryRun, "dry-run", false, "Calc and print effects of execution of pull command without any actual effects")
cmd.Flags().StringVar(&opts.format, "format", "", `Format the output. Only compatible with "--dry-run". Values: [pretty | json]. (Default: pretty)`)
return cmd
}

Expand All @@ -88,5 +92,7 @@ func runPull(ctx context.Context, backend api.Service, opts pullOptions, service
return backend.Pull(ctx, project, api.PullOptions{
Quiet: opts.quiet,
IgnoreFailures: opts.ignorePullFailures,
DryRun: opts.dryRun,
Format: opts.format,
})
}
51 changes: 51 additions & 0 deletions local/e2e/compose/compose_dry_run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"strings"
"testing"

"gotest.tools/v3/assert"

. "github.com/docker/compose-cli/utils/e2e"
)

func TestComposePullDryRun(t *testing.T) {
c := NewParallelE2eCLI(t, binDir)

t.Run("compose pull dry run", func(t *testing.T) {
// ensure storing alpine image and deleting hello-world
c.RunDockerCmd("pull", "alpine")
c.RunDockerCmd("rmi", "hello-world", "-f")

res := c.RunDockerCmd("compose", "-f", "./fixtures/dry-run-test/pull/compose.yaml", "pull", "--dry-run")
lines := Lines(res.Stdout())
for _, line := range lines {
if strings.Contains(line, "expected-skip") {
assert.Assert(t, strings.Contains(line, " skip "))
}
if strings.Contains(line, "expected-fail") {
assert.Assert(t, strings.Contains(line, " fail "))
}
if strings.Contains(line, "expected-fetch") {
assert.Assert(t, strings.Contains(line, " fetch "))
}
}
})

}
14 changes: 14 additions & 0 deletions local/e2e/compose/fixtures/dry-run-test/pull/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
expected-fetch:
image: hello-world
command: top
expected-skip-already-exist:
image: alpine
command: top
expected-fail:
# this image is not expected to be registered on any registries
image: expected-not-to-be-registered
command: top
expected-skip-build:
build: .
command: top
3 changes: 3 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ type PushOptions struct {
type PullOptions struct {
Quiet bool
IgnoreFailures bool
DryRun bool
Format string
}

// ImagesOptions group options of the Images API
Expand Down Expand Up @@ -308,6 +310,7 @@ type ImageSummary struct {
Repository string
Tag string
Size int64
Digests []string
}

// ServiceStatus hold status about a service
Expand Down
77 changes: 72 additions & 5 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"fmt"
"os"
"strings"
"sync"

"github.com/compose-spec/compose-go/types"
"github.com/containerd/containerd/platforms"
Expand All @@ -29,10 +31,12 @@ import (
"github.com/docker/buildx/util/buildflags"
xprogress "github.com/docker/buildx/util/progress"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/registry"
bclient "github.com/moby/buildkit/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/sync/errgroup"

"github.com/docker/compose-cli/pkg/api"
"github.com/docker/compose-cli/pkg/progress"
Expand Down Expand Up @@ -97,7 +101,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types.
}
}

images, err := s.getLocalImagesDigests(ctx, project)
images, err := s.getLocalImagesIDs(ctx, project)
if err != nil {
return err
}
Expand Down Expand Up @@ -163,7 +167,19 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri

}

func (s *composeService) getLocalImagesDigests(ctx context.Context, project *types.Project) (map[string]string, error) {
func (s *composeService) getLocalImagesIDs(ctx context.Context, project *types.Project) (map[string]string, error) {
imgs, err := s.getLocalImageSummaries(ctx, project)
if err != nil {
return nil, err
}
images := map[string]string{}
for name, info := range imgs {
images[name] = info.ID
}
return images, nil
}

func (s *composeService) getLocalImageSummaries(ctx context.Context, project *types.Project) (map[string]api.ImageSummary, error) {
imageNames := []string{}
for _, s := range project.Services {
imgName := getImageName(s, project.Name)
Expand All @@ -175,13 +191,64 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
if err != nil {
return nil, err
}
images := map[string]string{}
for name, info := range imgs {
images[name] = info.ID
return imgs, nil
}

func (s *composeService) getLocalImagesDigests(ctx context.Context, project *types.Project) (map[string][]string, error) {
imgs, err := s.getLocalImageSummaries(ctx, project)
if err != nil {
return nil, err
}
images := map[string][]string{}
for name, summary := range imgs {
for _, d := range summary.Digests {
s := strings.Split(d, "@")
images[name] = append(images[name], s[len(s)-1])
}
}
return images, nil
}

func (s *composeService) getDistributionImagesDigests(ctx context.Context, project *types.Project) (map[string]string, error) {
images := map[string]string{}

for _, service := range project.Services {
if service.Image == "" {
continue
}
images[service.Image] = ""
}

info, err := s.apiClient.Info(ctx)
if err != nil {
return nil, err
}

if info.IndexServerAddress == "" {
info.IndexServerAddress = registry.IndexServer
}

l := sync.Mutex{}
eg, ctx := errgroup.WithContext(ctx)
for img := range images {
img := img
eg.Go(func() error {
registryAuth, err := getEncodedRegistryAuth(img, info, s.configFile)
if err != nil {
return err
}
inspect, _ := s.apiClient.DistributionInspect(ctx, img, registryAuth)
// Ignore error here.
// If you catch error here, all inspect requests will fail.
l.Lock()
images[img] = inspect.Descriptor.Digest.String()
l.Unlock()
return nil
})
}
return images, eg.Wait()
}

func (s *composeService) doBuild(ctx context.Context, project *types.Project, opts map[string]build.Options, observedState Containers, mode string) (map[string]string, error) {
info, err := s.apiClient.Info(ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/compose/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (s *composeService) getImages(ctx context.Context, images []string) (map[st
Repository: repository,
Tag: tag,
Size: inspect.Size,
Digests: inspect.RepoDigests,
}
l.Unlock()
return nil
Expand Down
Loading

0 comments on commit 91b3e96

Please sign in to comment.