From 7d85485242ebd719a0f528febbaca18ac7419a05 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 7 Apr 2021 15:00:42 +0200 Subject: [PATCH] pause/unpause only need project name. use getContainers where possible Signed-off-by: Nicolas De Loof --- aci/compose.go | 4 ++-- api/client/compose.go | 4 ++-- api/compose/api.go | 10 ++++++++-- cli/cmd/compose/pause.go | 13 +++++++++---- ecs/local/compose.go | 8 ++++---- ecs/up.go | 4 ++-- kube/compose.go | 4 ++-- local/compose/attach.go | 2 +- local/compose/containers.go | 12 +++++------- local/compose/create.go | 6 +----- local/compose/down.go | 5 +---- local/compose/kill.go | 6 +----- local/compose/labels.go | 6 ------ local/compose/logs.go | 21 +-------------------- local/compose/pause.go | 10 +++++----- local/compose/ps.go | 7 +------ local/compose/remove.go | 2 +- local/compose/run.go | 5 +---- local/compose/status.go | 8 +------- local/compose/stop.go | 9 +-------- local/compose/top.go | 6 +----- 21 files changed, 50 insertions(+), 102 deletions(-) diff --git a/aci/compose.go b/aci/compose.go index 5551e0a69..7e057269c 100644 --- a/aci/compose.go +++ b/aci/compose.go @@ -72,11 +72,11 @@ func (cs *aciComposeService) Stop(ctx context.Context, project *types.Project, o return errdefs.ErrNotImplemented } -func (cs *aciComposeService) Pause(ctx context.Context, project *types.Project) error { +func (cs *aciComposeService) Pause(ctx context.Context, project string, options compose.PauseOptions) error { return errdefs.ErrNotImplemented } -func (cs *aciComposeService) UnPause(ctx context.Context, project *types.Project) error { +func (cs *aciComposeService) UnPause(ctx context.Context, project string, options compose.PauseOptions) error { return errdefs.ErrNotImplemented } diff --git a/api/client/compose.go b/api/client/compose.go index ac54ac8f4..0b0e49a12 100644 --- a/api/client/compose.go +++ b/api/client/compose.go @@ -96,11 +96,11 @@ func (c *composeService) Exec(ctx context.Context, project *types.Project, opts return errdefs.ErrNotImplemented } -func (c *composeService) Pause(ctx context.Context, project *types.Project) error { +func (c *composeService) Pause(ctx context.Context, project string, options compose.PauseOptions) error { return errdefs.ErrNotImplemented } -func (c *composeService) UnPause(ctx context.Context, project *types.Project) error { +func (c *composeService) UnPause(ctx context.Context, project string, options compose.PauseOptions) error { return errdefs.ErrNotImplemented } diff --git a/api/compose/api.go b/api/compose/api.go index b9915767d..1bf84f74a 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -63,9 +63,9 @@ type Service interface { // Exec executes a command in a running service container Exec(ctx context.Context, project *types.Project, opts RunOptions) error // Pause executes the equivalent to a `compose pause` - Pause(ctx context.Context, project *types.Project) error + Pause(ctx context.Context, project string, options PauseOptions) error // UnPause executes the equivalent to a `compose unpause` - UnPause(ctx context.Context, project *types.Project) error + UnPause(ctx context.Context, project string, options PauseOptions) error // Top executes the equivalent to a `compose top` Top(ctx context.Context, projectName string, services []string) ([]ContainerProcSummary, error) // Events executes the equivalent to a `compose events` @@ -303,6 +303,12 @@ type LogOptions struct { Timestamps bool } +// PauseOptions group options of the Pause API +type PauseOptions struct { + // Services passed in the command line to be started + Services []string +} + const ( // STARTING indicates that stack is being deployed STARTING string = "Starting" diff --git a/cli/cmd/compose/pause.go b/cli/cmd/compose/pause.go index 87f8fc716..b32c7571e 100644 --- a/cli/cmd/compose/pause.go +++ b/cli/cmd/compose/pause.go @@ -22,6 +22,7 @@ import ( "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" ) @@ -49,13 +50,15 @@ func runPause(ctx context.Context, opts pauseOptions, services []string) error { return err } - project, err := opts.toProject(services) + project, err := opts.toProjectName() if err != nil { return err } _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", c.ComposeService().Pause(ctx, project) + return "", c.ComposeService().Pause(ctx, project, compose.PauseOptions{ + Services: services, + }) }) return err } @@ -84,13 +87,15 @@ func runUnPause(ctx context.Context, opts unpauseOptions, services []string) err return err } - project, err := opts.toProject(services) + project, err := opts.toProjectName() if err != nil { return err } _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", c.ComposeService().UnPause(ctx, project) + return "", c.ComposeService().UnPause(ctx, project, compose.PauseOptions{ + Services: services, + }) }) return err } diff --git a/ecs/local/compose.go b/ecs/local/compose.go index 7a15b1d82..f6db2ed64 100644 --- a/ecs/local/compose.go +++ b/ecs/local/compose.go @@ -188,12 +188,12 @@ func (e ecsLocalSimulation) Exec(ctx context.Context, project *types.Project, op return errdefs.ErrNotImplemented } -func (e ecsLocalSimulation) Pause(ctx context.Context, project *types.Project) error { - return e.compose.Pause(ctx, project) +func (e ecsLocalSimulation) Pause(ctx context.Context, project string, options compose.PauseOptions) error { + return e.compose.Pause(ctx, project, options) } -func (e ecsLocalSimulation) UnPause(ctx context.Context, project *types.Project) error { - return e.compose.UnPause(ctx, project) +func (e ecsLocalSimulation) UnPause(ctx context.Context, project string, options compose.PauseOptions) error { + return e.compose.UnPause(ctx, project, options) } func (e ecsLocalSimulation) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) { diff --git a/ecs/up.go b/ecs/up.go index db1ce902f..bba7911d7 100644 --- a/ecs/up.go +++ b/ecs/up.go @@ -59,11 +59,11 @@ func (b *ecsAPIService) Stop(ctx context.Context, project *types.Project, option return errdefs.ErrNotImplemented } -func (b *ecsAPIService) Pause(ctx context.Context, project *types.Project) error { +func (b *ecsAPIService) Pause(ctx context.Context, project string, options compose.PauseOptions) error { return errdefs.ErrNotImplemented } -func (b *ecsAPIService) UnPause(ctx context.Context, project *types.Project) error { +func (b *ecsAPIService) UnPause(ctx context.Context, project string, options compose.PauseOptions) error { return errdefs.ErrNotImplemented } diff --git a/kube/compose.go b/kube/compose.go index e8d40ad3e..9ec914147 100644 --- a/kube/compose.go +++ b/kube/compose.go @@ -252,11 +252,11 @@ func (s *composeService) Exec(ctx context.Context, project *types.Project, opts return errdefs.ErrNotImplemented } -func (s *composeService) Pause(ctx context.Context, project *types.Project) error { +func (s *composeService) Pause(ctx context.Context, project string, options compose.PauseOptions) error { return errdefs.ErrNotImplemented } -func (s *composeService) UnPause(ctx context.Context, project *types.Project) error { +func (s *composeService) UnPause(ctx context.Context, project string, options compose.PauseOptions) error { return errdefs.ErrNotImplemented } diff --git a/local/compose/attach.go b/local/compose/attach.go index 9fd695fdb..5250204e5 100644 --- a/local/compose/attach.go +++ b/local/compose/attach.go @@ -33,7 +33,7 @@ import ( ) func (s *composeService) attach(ctx context.Context, project *types.Project, listener compose.ContainerEventListener, selectedServices []string) (Containers, error) { - containers, err := s.getContainers(ctx, project, oneOffExclude, selectedServices) + containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, selectedServices...) if err != nil { return nil, err } diff --git a/local/compose/containers.go b/local/compose/containers.go index 1e26b6a90..ad1790971 100644 --- a/local/compose/containers.go +++ b/local/compose/containers.go @@ -21,7 +21,6 @@ import ( "fmt" "sort" - "github.com/compose-spec/compose-go/types" moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -39,10 +38,10 @@ const ( oneOffOnly ) -func (s *composeService) getContainers(ctx context.Context, project *types.Project, oneOff oneOff, selectedServices []string) (Containers, error) { +func (s *composeService) getContainers(ctx context.Context, project string, oneOff oneOff, stopped bool, selectedServices ...string) (Containers, error) { var containers Containers f := filters.NewArgs( - projectFilter(project.Name), + projectFilter(project), ) switch oneOff { case oneOffOnly: @@ -53,15 +52,14 @@ func (s *composeService) getContainers(ctx context.Context, project *types.Proje } containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ Filters: f, - All: true, + All: stopped, }) if err != nil { return nil, err } - if len(selectedServices) == 0 { - selectedServices = project.ServiceNames() + if len(selectedServices) > 0 { + containers = containers.filter(isService(selectedServices...)) } - containers = containers.filter(isService(selectedServices...)) return containers, nil } diff --git a/local/compose/create.go b/local/compose/create.go index a97301d0e..30a708fb6 100644 --- a/local/compose/create.go +++ b/local/compose/create.go @@ -26,7 +26,6 @@ import ( "github.com/compose-spec/compose-go/types" moby "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" - "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/strslice" @@ -69,10 +68,7 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt } var observedState Containers - observedState, err = s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ - Filters: filters.NewArgs(projectFilter(project.Name)), - All: true, - }) + observedState, err = s.getContainers(ctx, project.Name, oneOffInclude, true) if err != nil { return err } diff --git a/local/compose/down.go b/local/compose/down.go index 0b33f10ed..380fd7abb 100644 --- a/local/compose/down.go +++ b/local/compose/down.go @@ -39,10 +39,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c resourceToRemove := false var containers Containers - containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ - Filters: filters.NewArgs(projectFilter(projectName)), - All: true, - }) + containers, err := s.getContainers(ctx, projectName, oneOffInclude, true) if err != nil { return err } diff --git a/local/compose/kill.go b/local/compose/kill.go index 1745a1210..0ec788e89 100644 --- a/local/compose/kill.go +++ b/local/compose/kill.go @@ -21,7 +21,6 @@ import ( "github.com/compose-spec/compose-go/types" moby "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/filters" "golang.org/x/sync/errgroup" "github.com/docker/compose-cli/api/compose" @@ -32,10 +31,7 @@ func (s *composeService) Kill(ctx context.Context, project *types.Project, optio w := progress.ContextWriter(ctx) var containers Containers - containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ - Filters: filters.NewArgs(projectFilter(project.Name)), - All: true, - }) + containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true) if err != nil { return err } diff --git a/local/compose/labels.go b/local/compose/labels.go index 2926d30e9..c658c1df7 100644 --- a/local/compose/labels.go +++ b/local/compose/labels.go @@ -18,8 +18,6 @@ package compose import ( "fmt" - "strconv" - "strings" "github.com/docker/docker/api/types/filters" @@ -47,10 +45,6 @@ func projectFilter(projectName string) filters.KeyValuePair { return filters.Arg("label", fmt.Sprintf("%s=%s", projectLabel, projectName)) } -func oneOffFilter(oneOff bool) filters.KeyValuePair { - return filters.Arg("label", fmt.Sprintf("%s=%s", oneoffLabel, strings.Title(strconv.FormatBool(oneOff)))) -} - func serviceFilter(serviceName string) filters.KeyValuePair { return filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, serviceName)) } diff --git a/local/compose/logs.go b/local/compose/logs.go index cd54d2c59..dea11abc3 100644 --- a/local/compose/logs.go +++ b/local/compose/logs.go @@ -24,28 +24,12 @@ import ( "github.com/docker/compose-cli/utils" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/filters" "github.com/docker/docker/pkg/stdcopy" "golang.org/x/sync/errgroup" ) func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error { - list, err := s.apiClient.ContainerList(ctx, types.ContainerListOptions{ - Filters: filters.NewArgs( - projectFilter(projectName), - oneOffFilter(false), - ), - All: true, - }) - - ignore := func(string) bool { - return false - } - if len(options.Services) > 0 { - ignore = func(s string) bool { - return !utils.StringContains(options.Services, s) - } - } + list, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...) if err != nil { return err @@ -54,9 +38,6 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer for _, c := range list { c := c service := c.Labels[serviceLabel] - if ignore(service) { - continue - } container, err := s.apiClient.ContainerInspect(ctx, c.ID) if err != nil { return err diff --git a/local/compose/pause.go b/local/compose/pause.go index b4a0d0bbd..d1e880943 100644 --- a/local/compose/pause.go +++ b/local/compose/pause.go @@ -19,15 +19,15 @@ package compose import ( "context" - "github.com/compose-spec/compose-go/types" moby "github.com/docker/docker/api/types" "golang.org/x/sync/errgroup" + "github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/progress" ) -func (s *composeService) Pause(ctx context.Context, project *types.Project) error { - containers, err := s.getContainers(ctx, project, oneOffExclude, nil) +func (s *composeService) Pause(ctx context.Context, project string, options compose.PauseOptions) error { + containers, err := s.getContainers(ctx, project, oneOffExclude, true, options.Services...) if err != nil { return err } @@ -48,8 +48,8 @@ func (s *composeService) Pause(ctx context.Context, project *types.Project) erro return eg.Wait() } -func (s *composeService) UnPause(ctx context.Context, project *types.Project) error { - containers, err := s.getContainers(ctx, project, oneOffExclude, nil) +func (s *composeService) UnPause(ctx context.Context, project string, options compose.PauseOptions) error { + containers, err := s.getContainers(ctx, project, oneOffExclude, true, options.Services...) if err != nil { return err } diff --git a/local/compose/ps.go b/local/compose/ps.go index 4289ca6ae..9dc034719 100644 --- a/local/compose/ps.go +++ b/local/compose/ps.go @@ -20,18 +20,13 @@ import ( "context" "fmt" - moby "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/filters" "golang.org/x/sync/errgroup" "github.com/docker/compose-cli/api/compose" ) func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) { - containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ - Filters: filters.NewArgs(projectFilter(projectName)), - All: options.All, - }) + containers, err := s.getContainers(ctx, projectName, oneOffInclude, options.All) if err != nil { return nil, err } diff --git a/local/compose/remove.go b/local/compose/remove.go index c69bf0309..1018bc34d 100644 --- a/local/compose/remove.go +++ b/local/compose/remove.go @@ -29,7 +29,7 @@ import ( ) func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) { - containers, err := s.getContainers(ctx, project, oneOffInclude, nil) + containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true) if err != nil { return nil, err } diff --git a/local/compose/run.go b/local/compose/run.go index 71c57672f..5f6b13e5d 100644 --- a/local/compose/run.go +++ b/local/compose/run.go @@ -30,10 +30,7 @@ import ( ) func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) { - observedState, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{ - Filters: filters.NewArgs(projectFilter(project.Name)), - All: true, - }) + observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true) if err != nil { return 0, err } diff --git a/local/compose/status.go b/local/compose/status.go index c75eb3335..db95a0f08 100644 --- a/local/compose/status.go +++ b/local/compose/status.go @@ -20,7 +20,6 @@ import ( "context" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/filters" "github.com/pkg/errors" ) @@ -103,12 +102,7 @@ func GetContextContainerState(ctx context.Context) (ContainersState, error) { } func (s composeService) getUpdatedContainersStateContext(ctx context.Context, projectName string) (context.Context, error) { - observedState, err := s.apiClient.ContainerList(ctx, types.ContainerListOptions{ - Filters: filters.NewArgs( - projectFilter(projectName), - ), - All: true, - }) + observedState, err := s.getContainers(ctx, projectName, oneOffInclude, true) if err != nil { return nil, err } diff --git a/local/compose/stop.go b/local/compose/stop.go index 5ecc50160..b528312db 100644 --- a/local/compose/stop.go +++ b/local/compose/stop.go @@ -23,23 +23,16 @@ import ( "github.com/docker/compose-cli/api/progress" "github.com/compose-spec/compose-go/types" - moby "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/filters" ) func (s *composeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error { w := progress.ContextWriter(ctx) var containers Containers - containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ - Filters: filters.NewArgs(projectFilter(project.Name)), - All: true, - }) + containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true, project.ServiceNames()...) if err != nil { return err } - containers = containers.filter(isService(project.ServiceNames()...)) - return InReverseDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error { return s.stopContainers(ctx, w, containers.filter(isService(service.Name)), options.Timeout) }) diff --git a/local/compose/top.go b/local/compose/top.go index 24549e99f..6e038a1a0 100644 --- a/local/compose/top.go +++ b/local/compose/top.go @@ -20,16 +20,12 @@ import ( "context" "github.com/docker/compose-cli/api/compose" - moby "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/filters" "golang.org/x/sync/errgroup" ) func (s *composeService) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) { var containers Containers - containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ - Filters: filters.NewArgs(projectFilter(projectName)), - }) + containers, err := s.getContainers(ctx, projectName, oneOffInclude, false) if err != nil { return nil, err }