From 6f730072655bd283770688cb3c5edc7c58f91920 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 2 Mar 2021 09:03:04 +0100 Subject: [PATCH 1/2] support compose build --pull Signed-off-by: Nicolas De Loof --- aci/compose.go | 2 +- api/client/compose.go | 2 +- api/compose/api.go | 8 +++++++- cli/cmd/compose/build.go | 7 ++++++- ecs/local/compose.go | 4 ++-- ecs/up.go | 2 +- kube/compose.go | 2 +- local/compose/build.go | 7 +++++-- 8 files changed, 24 insertions(+), 10 deletions(-) diff --git a/aci/compose.go b/aci/compose.go index 8beba0ba2..f004ddbd7 100644 --- a/aci/compose.go +++ b/aci/compose.go @@ -44,7 +44,7 @@ func newComposeService(ctx store.AciContext) aciComposeService { } } -func (cs *aciComposeService) Build(ctx context.Context, project *types.Project) error { +func (cs *aciComposeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error { return errdefs.ErrNotImplemented } diff --git a/api/client/compose.go b/api/client/compose.go index 75e7be163..f68378b8c 100644 --- a/api/client/compose.go +++ b/api/client/compose.go @@ -28,7 +28,7 @@ import ( type composeService struct { } -func (c *composeService) Build(ctx context.Context, project *types.Project) error { +func (c *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error { return errdefs.ErrNotImplemented } diff --git a/api/compose/api.go b/api/compose/api.go index cd9dc4d86..78bd52610 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -28,7 +28,7 @@ import ( // Service manages a compose project type Service interface { // Build executes the equivalent to a `compose build` - Build(ctx context.Context, project *types.Project) error + Build(ctx context.Context, project *types.Project, options BuildOptions) error // Push executes the equivalent ot a `compose push` Push(ctx context.Context, project *types.Project) error // Pull executes the equivalent of a `compose pull` @@ -65,6 +65,12 @@ type Service interface { UnPause(ctx context.Context, project *types.Project) error } +// BuildOptions group options of the Build API +type BuildOptions struct { + // Pull always attempt to pull a newer version of the image + Pull bool +} + // CreateOptions group options of the Create API type CreateOptions struct { // Remove legacy containers for services that are not defined in the project diff --git a/cli/cmd/compose/build.go b/cli/cmd/compose/build.go index 622b753fa..a112f9789 100644 --- a/cli/cmd/compose/build.go +++ b/cli/cmd/compose/build.go @@ -18,6 +18,7 @@ package compose import ( "context" + "github.com/docker/compose-cli/api/compose" "os" "github.com/spf13/cobra" @@ -30,6 +31,7 @@ type buildOptions struct { *projectOptions composeOptions quiet bool + pull bool } func buildCommand(p *projectOptions) *cobra.Command { @@ -51,6 +53,7 @@ func buildCommand(p *projectOptions) *cobra.Command { }, } cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT") + cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.") return cmd } @@ -66,7 +69,9 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error { } _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", c.ComposeService().Build(ctx, project) + return "", c.ComposeService().Build(ctx, project, compose.BuildOptions{ + Pull: opts.pull, + }) }) return err } diff --git a/ecs/local/compose.go b/ecs/local/compose.go index 6ecce3e71..f8a8da8e5 100644 --- a/ecs/local/compose.go +++ b/ecs/local/compose.go @@ -32,8 +32,8 @@ import ( "github.com/docker/compose-cli/api/errdefs" ) -func (e ecsLocalSimulation) Build(ctx context.Context, project *types.Project) error { - return e.compose.Build(ctx, project) +func (e ecsLocalSimulation) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error { + return e.compose.Build(ctx, project, options) } func (e ecsLocalSimulation) Push(ctx context.Context, project *types.Project) error { diff --git a/ecs/up.go b/ecs/up.go index 697cabe04..b899bd0df 100644 --- a/ecs/up.go +++ b/ecs/up.go @@ -31,7 +31,7 @@ import ( "github.com/compose-spec/compose-go/types" ) -func (b *ecsAPIService) Build(ctx context.Context, project *types.Project) error { +func (b *ecsAPIService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error { return errdefs.ErrNotImplemented } diff --git a/kube/compose.go b/kube/compose.go index 230914a6a..9ca2911fb 100644 --- a/kube/compose.go +++ b/kube/compose.go @@ -124,7 +124,7 @@ func (s *composeService) List(ctx context.Context, opts compose.ListOptions) ([] } // Build executes the equivalent to a `compose build` -func (s *composeService) Build(ctx context.Context, project *types.Project) error { +func (s *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error { return errdefs.ErrNotImplemented } diff --git a/local/compose/build.go b/local/compose/build.go index 80efdbe7c..3fd14e5d4 100644 --- a/local/compose/build.go +++ b/local/compose/build.go @@ -19,6 +19,7 @@ package compose import ( "context" "fmt" + "github.com/docker/compose-cli/api/compose" "os" "path" "strings" @@ -32,14 +33,16 @@ import ( bclient "github.com/moby/buildkit/client" ) -func (s *composeService) Build(ctx context.Context, project *types.Project) error { +func (s *composeService) Build(ctx context.Context, project *types.Project, options compose.BuildOptions) error { opts := map[string]build.Options{} imagesToBuild := []string{} for _, service := range project.Services { if service.Build != nil { imageName := getImageName(service, project.Name) imagesToBuild = append(imagesToBuild, imageName) - opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName) + buildOptions := s.toBuildOptions(service, project.WorkingDir, imageName) + buildOptions.Pull = options.Pull + opts[imageName] = buildOptions } } From 6412d887033c0159c7c3eb65ac489ef2bc15cd96 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 2 Mar 2021 09:14:09 +0100 Subject: [PATCH 2/2] introduce build --progress Signed-off-by: Nicolas De Loof --- api/compose/api.go | 2 ++ cli/cmd/compose/build.go | 11 +++++++---- local/compose/build.go | 11 ++++++----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/api/compose/api.go b/api/compose/api.go index 78bd52610..6c29a982e 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -69,6 +69,8 @@ type Service interface { type BuildOptions struct { // Pull always attempt to pull a newer version of the image Pull bool + // Progress set type of progress output ("auto", "plain", "tty") + Progress string } // CreateOptions group options of the Create API diff --git a/cli/cmd/compose/build.go b/cli/cmd/compose/build.go index a112f9789..b81c17100 100644 --- a/cli/cmd/compose/build.go +++ b/cli/cmd/compose/build.go @@ -18,20 +18,21 @@ package compose import ( "context" - "github.com/docker/compose-cli/api/compose" "os" "github.com/spf13/cobra" "github.com/docker/compose-cli/api/client" + "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/progress" ) type buildOptions struct { *projectOptions composeOptions - quiet bool - pull bool + quiet bool + pull bool + progress string } func buildCommand(p *projectOptions) *cobra.Command { @@ -54,6 +55,7 @@ func buildCommand(p *projectOptions) *cobra.Command { } cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Don't print anything to STDOUT") cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.") + cmd.Flags().StringVar(&opts.progress, "progress", "auto", `Set type of progress output ("auto", "plain", "tty")`) return cmd } @@ -70,7 +72,8 @@ func runBuild(ctx context.Context, opts buildOptions, services []string) error { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { return "", c.ComposeService().Build(ctx, project, compose.BuildOptions{ - Pull: opts.pull, + Pull: opts.pull, + Progress: opts.progress, }) }) return err diff --git a/local/compose/build.go b/local/compose/build.go index 3fd14e5d4..80e8ae991 100644 --- a/local/compose/build.go +++ b/local/compose/build.go @@ -19,11 +19,12 @@ package compose import ( "context" "fmt" - "github.com/docker/compose-cli/api/compose" "os" "path" "strings" + "github.com/docker/compose-cli/api/compose" + "github.com/compose-spec/compose-go/types" "github.com/docker/buildx/build" "github.com/docker/buildx/driver" @@ -46,7 +47,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project, opti } } - err := s.build(ctx, project, opts) + err := s.build(ctx, project, opts, options.Progress) if err == nil { displayScanSuggestMsg(ctx, imagesToBuild) } @@ -96,7 +97,7 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types. } - err := s.build(ctx, project, opts) + err := s.build(ctx, project, opts, "auto") if err == nil { displayScanSuggestMsg(ctx, imagesToBuild) } @@ -114,7 +115,7 @@ func (s *composeService) localImagePresent(ctx context.Context, imageName string return true, nil } -func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options) error { +func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) error { if len(opts) == 0 { return nil } @@ -135,7 +136,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opts // build and will lock progressCtx, cancel := context.WithCancel(context.Background()) defer cancel() - w := progress.NewPrinter(progressCtx, os.Stdout, "auto") + w := progress.NewPrinter(progressCtx, os.Stdout, mode) // We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here _, err = build.Build(ctx, driverInfo, opts, nil, nil, w)