From dec91691986f903c0716ec0344727481b53c80b8 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 7 Jun 2021 11:21:57 +0200 Subject: [PATCH] move progress to backend on "down" Signed-off-by: Nicolas De Loof --- aci/compose.go | 36 ++++++++++++++++--------- cli/cmd/compose/compose.go | 2 +- cli/cmd/compose/down.go | 54 ++++++++++++++++---------------------- ecs/down.go | 16 ++++++++++- kube/compose.go | 14 +++++++++- local/compose/down.go | 6 +++++ 6 files changed, 81 insertions(+), 47 deletions(-) diff --git a/aci/compose.go b/aci/compose.go index 7712c818e..83c74c5f5 100644 --- a/aci/compose.go +++ b/aci/compose.go @@ -22,6 +22,7 @@ import ( "net/http" "github.com/compose-spec/compose-go/types" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/docker/compose-cli/aci/convert" @@ -29,6 +30,7 @@ import ( "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/api/errdefs" + "github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/utils/formatter" ) @@ -123,21 +125,29 @@ func (cs aciComposeService) warnKeepVolumeOnDown(ctx context.Context, projectNam } func (cs *aciComposeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error { - logrus.Debugf("Down on projectName with name %q", projectName) + if options.Volumes { + return errors.Wrap(errdefs.ErrNotImplemented, "--volumes option is not supported on ACI") + } + if options.Images != "" { + return errors.Wrap(errdefs.ErrNotImplemented, "--rmi option is not supported on ACI") + } + return progress.Run(ctx, func(ctx context.Context) error { + logrus.Debugf("Down on project with name %q", projectName) + + if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil { + return err + } + + cg, err := deleteACIContainerGroup(ctx, cs.ctx, projectName) + if err != nil { + return err + } + if cg.IsHTTPStatus(http.StatusNoContent) { + return errdefs.ErrNotFound + } - if err := cs.warnKeepVolumeOnDown(ctx, projectName); err != nil { return err - } - - cg, err := deleteACIContainerGroup(ctx, cs.ctx, projectName) - if err != nil { - return err - } - if cg.IsHTTPStatus(http.StatusNoContent) { - return errdefs.ErrNotFound - } - - return err + }) } func (cs *aciComposeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) { diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index 4aecd3b05..c3bb0559d 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -218,7 +218,7 @@ func RootCommand(contextType string, backend compose.Service) *cobra.Command { command.AddCommand( upCommand(&opts, contextType, backend), - downCommand(&opts, contextType, backend), + downCommand(&opts, backend), startCommand(&opts, backend), restartCommand(&opts, backend), stopCommand(&opts, backend), diff --git a/cli/cmd/compose/down.go b/cli/cmd/compose/down.go index af42cfbdd..c2191c2c1 100644 --- a/cli/cmd/compose/down.go +++ b/cli/cmd/compose/down.go @@ -25,8 +25,6 @@ import ( "github.com/spf13/cobra" "github.com/docker/compose-cli/api/compose" - "github.com/docker/compose-cli/api/context/store" - "github.com/docker/compose-cli/api/progress" ) type downOptions struct { @@ -38,7 +36,7 @@ type downOptions struct { images string } -func downCommand(p *projectOptions, contextType string, backend compose.Service) *cobra.Command { +func downCommand(p *projectOptions, backend compose.Service) *cobra.Command { opts := downOptions{ projectOptions: p, } @@ -63,39 +61,33 @@ func downCommand(p *projectOptions, contextType string, backend compose.Service) flags := downCmd.Flags() flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.") flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds") - - switch contextType { - case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType: - flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.") - flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`) - } + flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.") + flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`) return downCmd } func runDown(ctx context.Context, backend compose.Service, opts downOptions) error { - return progress.Run(ctx, func(ctx context.Context) error { - name := opts.ProjectName - var project *types.Project - if opts.ProjectName == "" { - p, err := opts.toProject(nil) - if err != nil { - return err - } - project = p - name = p.Name + name := opts.ProjectName + var project *types.Project + if opts.ProjectName == "" { + p, err := opts.toProject(nil) + if err != nil { + return err } + project = p + name = p.Name + } - var timeout *time.Duration - if opts.timeChanged { - timeoutValue := time.Duration(opts.timeout) * time.Second - timeout = &timeoutValue - } - return backend.Down(ctx, name, compose.DownOptions{ - RemoveOrphans: opts.removeOrphans, - Project: project, - Timeout: timeout, - Images: opts.images, - Volumes: opts.volumes, - }) + var timeout *time.Duration + if opts.timeChanged { + timeoutValue := time.Duration(opts.timeout) * time.Second + timeout = &timeoutValue + } + return backend.Down(ctx, name, compose.DownOptions{ + RemoveOrphans: opts.removeOrphans, + Project: project, + Timeout: timeout, + Images: opts.images, + Volumes: opts.volumes, }) } diff --git a/ecs/down.go b/ecs/down.go index 959829897..d7acecdd3 100644 --- a/ecs/down.go +++ b/ecs/down.go @@ -19,12 +19,26 @@ package ecs import ( "context" - "github.com/docker/compose-cli/api/compose" + "github.com/pkg/errors" + "github.com/docker/compose-cli/api/compose" + "github.com/docker/compose-cli/api/errdefs" "github.com/docker/compose-cli/api/progress" ) func (b *ecsAPIService) Down(ctx context.Context, projectName string, options compose.DownOptions) error { + if options.Volumes { + return errors.Wrap(errdefs.ErrNotImplemented, "--volumes option is not supported on ECS") + } + if options.Images != "" { + return errors.Wrap(errdefs.ErrNotImplemented, "--rmi option is not supported on ECS") + } + return progress.Run(ctx, func(ctx context.Context) error { + return b.down(ctx, projectName) + }) +} + +func (b *ecsAPIService) down(ctx context.Context, projectName string) error { resources, err := b.aws.ListStackResources(ctx, projectName) if err != nil { return err diff --git a/kube/compose.go b/kube/compose.go index ac9a46b5f..389c7eae8 100644 --- a/kube/compose.go +++ b/kube/compose.go @@ -24,6 +24,7 @@ import ( "strings" "github.com/compose-spec/compose-go/types" + "github.com/pkg/errors" "github.com/docker/compose-cli/api/compose" apicontext "github.com/docker/compose-cli/api/context" @@ -110,8 +111,19 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options // Down executes the equivalent to a `compose down` func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error { - w := progress.ContextWriter(ctx) + if options.Volumes { + return errors.Wrap(errdefs.ErrNotImplemented, "--volumes option is not supported on Kubernetes") + } + if options.Images != "" { + return errors.Wrap(errdefs.ErrNotImplemented, "--rmi option is not supported on Kubernetes") + } + return progress.Run(ctx, func(ctx context.Context) error { + return s.down(ctx, projectName, options) + }) +} +func (s *composeService) down(ctx context.Context, projectName string, options compose.DownOptions) error { + w := progress.ContextWriter(ctx) eventName := fmt.Sprintf("Remove %s", projectName) w.Event(progress.CreatingEvent(eventName)) diff --git a/local/compose/down.go b/local/compose/down.go index 0d4a92201..1464ec35c 100644 --- a/local/compose/down.go +++ b/local/compose/down.go @@ -36,6 +36,12 @@ import ( type downOp func() error func (s *composeService) Down(ctx context.Context, projectName string, options compose.DownOptions) error { + return progress.Run(ctx, func(ctx context.Context) error { + return s.down(ctx, projectName, options) + }) +} + +func (s *composeService) down(ctx context.Context, projectName string, options compose.DownOptions) error { w := progress.ContextWriter(ctx) resourceToRemove := false