Merge pull request #1509 from docker/pause_project_name

pause/unpause only need project name. use getContainers where possible
This commit is contained in:
Nicolas De loof 2021-04-08 10:00:16 +02:00 committed by GitHub
commit d073c93ff7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 50 additions and 102 deletions

View File

@ -72,11 +72,11 @@ func (cs *aciComposeService) Stop(ctx context.Context, project *types.Project, o
return errdefs.ErrNotImplemented 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 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 return errdefs.ErrNotImplemented
} }

View File

@ -96,11 +96,11 @@ func (c *composeService) Exec(ctx context.Context, project *types.Project, opts
return errdefs.ErrNotImplemented 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 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 return errdefs.ErrNotImplemented
} }

View File

@ -63,9 +63,9 @@ type Service interface {
// Exec executes a command in a running service container // Exec executes a command in a running service container
Exec(ctx context.Context, project *types.Project, opts RunOptions) error Exec(ctx context.Context, project *types.Project, opts RunOptions) error
// Pause executes the equivalent to a `compose pause` // 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 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 executes the equivalent to a `compose top`
Top(ctx context.Context, projectName string, services []string) ([]ContainerProcSummary, error) Top(ctx context.Context, projectName string, services []string) ([]ContainerProcSummary, error)
// Events executes the equivalent to a `compose events` // Events executes the equivalent to a `compose events`
@ -303,6 +303,12 @@ type LogOptions struct {
Timestamps bool Timestamps bool
} }
// PauseOptions group options of the Pause API
type PauseOptions struct {
// Services passed in the command line to be started
Services []string
}
const ( const (
// STARTING indicates that stack is being deployed // STARTING indicates that stack is being deployed
STARTING string = "Starting" STARTING string = "Starting"

View File

@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client" "github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/api/progress"
) )
@ -49,13 +50,15 @@ func runPause(ctx context.Context, opts pauseOptions, services []string) error {
return err return err
} }
project, err := opts.toProject(services) project, err := opts.toProjectName()
if err != nil { if err != nil {
return err return err
} }
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, 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 return err
} }
@ -84,13 +87,15 @@ func runUnPause(ctx context.Context, opts unpauseOptions, services []string) err
return err return err
} }
project, err := opts.toProject(services) project, err := opts.toProjectName()
if err != nil { if err != nil {
return err return err
} }
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, 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 return err
} }

View File

@ -188,12 +188,12 @@ func (e ecsLocalSimulation) Exec(ctx context.Context, project *types.Project, op
return errdefs.ErrNotImplemented return errdefs.ErrNotImplemented
} }
func (e ecsLocalSimulation) Pause(ctx context.Context, project *types.Project) error { func (e ecsLocalSimulation) Pause(ctx context.Context, project string, options compose.PauseOptions) error {
return e.compose.Pause(ctx, project) return e.compose.Pause(ctx, project, options)
} }
func (e ecsLocalSimulation) UnPause(ctx context.Context, project *types.Project) error { func (e ecsLocalSimulation) UnPause(ctx context.Context, project string, options compose.PauseOptions) error {
return e.compose.UnPause(ctx, project) return e.compose.UnPause(ctx, project, options)
} }
func (e ecsLocalSimulation) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) { func (e ecsLocalSimulation) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) {

View File

@ -59,11 +59,11 @@ func (b *ecsAPIService) Stop(ctx context.Context, project *types.Project, option
return errdefs.ErrNotImplemented 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 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 return errdefs.ErrNotImplemented
} }

View File

@ -252,11 +252,11 @@ func (s *composeService) Exec(ctx context.Context, project *types.Project, opts
return errdefs.ErrNotImplemented 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 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 return errdefs.ErrNotImplemented
} }

View File

@ -33,7 +33,7 @@ import (
) )
func (s *composeService) attach(ctx context.Context, project *types.Project, listener compose.ContainerEventListener, selectedServices []string) (Containers, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -21,7 +21,6 @@ import (
"fmt" "fmt"
"sort" "sort"
"github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
@ -39,10 +38,10 @@ const (
oneOffOnly 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 var containers Containers
f := filters.NewArgs( f := filters.NewArgs(
projectFilter(project.Name), projectFilter(project),
) )
switch oneOff { switch oneOff {
case oneOffOnly: case oneOffOnly:
@ -53,15 +52,14 @@ func (s *composeService) getContainers(ctx context.Context, project *types.Proje
} }
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
Filters: f, Filters: f,
All: true, All: stopped,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(selectedServices) == 0 { if len(selectedServices) > 0 {
selectedServices = project.ServiceNames()
}
containers = containers.filter(isService(selectedServices...)) containers = containers.filter(isService(selectedServices...))
}
return containers, nil return containers, nil
} }

View File

@ -26,7 +26,6 @@ import (
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "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/mount"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/strslice" "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 var observedState Containers
observedState, err = s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ observedState, err = s.getContainers(ctx, project.Name, oneOffInclude, true)
Filters: filters.NewArgs(projectFilter(project.Name)),
All: true,
})
if err != nil { if err != nil {
return err return err
} }

View File

@ -39,10 +39,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
resourceToRemove := false resourceToRemove := false
var containers Containers var containers Containers
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ containers, err := s.getContainers(ctx, projectName, oneOffInclude, true)
Filters: filters.NewArgs(projectFilter(projectName)),
All: true,
})
if err != nil { if err != nil {
return err return err
} }

View File

@ -21,7 +21,6 @@ import (
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/docker/compose-cli/api/compose" "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) w := progress.ContextWriter(ctx)
var containers Containers var containers Containers
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
Filters: filters.NewArgs(projectFilter(project.Name)),
All: true,
})
if err != nil { if err != nil {
return err return err
} }

View File

@ -18,8 +18,6 @@ package compose
import ( import (
"fmt" "fmt"
"strconv"
"strings"
"github.com/docker/docker/api/types/filters" "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)) 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 { func serviceFilter(serviceName string) filters.KeyValuePair {
return filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, serviceName)) return filters.Arg("label", fmt.Sprintf("%s=%s", serviceLabel, serviceName))
} }

View File

@ -24,28 +24,12 @@ import (
"github.com/docker/compose-cli/utils" "github.com/docker/compose-cli/utils"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/stdcopy"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error { func (s *composeService) Logs(ctx context.Context, projectName string, consumer compose.LogConsumer, options compose.LogOptions) error {
list, err := s.apiClient.ContainerList(ctx, types.ContainerListOptions{ list, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
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)
}
}
if err != nil { if err != nil {
return err return err
@ -54,9 +38,6 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
for _, c := range list { for _, c := range list {
c := c c := c
service := c.Labels[serviceLabel] service := c.Labels[serviceLabel]
if ignore(service) {
continue
}
container, err := s.apiClient.ContainerInspect(ctx, c.ID) container, err := s.apiClient.ContainerInspect(ctx, c.ID)
if err != nil { if err != nil {
return err return err

View File

@ -19,15 +19,15 @@ package compose
import ( import (
"context" "context"
"github.com/compose-spec/compose-go/types"
moby "github.com/docker/docker/api/types" moby "github.com/docker/docker/api/types"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/api/progress"
) )
func (s *composeService) Pause(ctx context.Context, project *types.Project) error { func (s *composeService) Pause(ctx context.Context, project string, options compose.PauseOptions) error {
containers, err := s.getContainers(ctx, project, oneOffExclude, nil) containers, err := s.getContainers(ctx, project, oneOffExclude, true, options.Services...)
if err != nil { if err != nil {
return err return err
} }
@ -48,8 +48,8 @@ func (s *composeService) Pause(ctx context.Context, project *types.Project) erro
return eg.Wait() return eg.Wait()
} }
func (s *composeService) UnPause(ctx context.Context, project *types.Project) error { func (s *composeService) UnPause(ctx context.Context, project string, options compose.PauseOptions) error {
containers, err := s.getContainers(ctx, project, oneOffExclude, nil) containers, err := s.getContainers(ctx, project, oneOffExclude, true, options.Services...)
if err != nil { if err != nil {
return err return err
} }

View File

@ -20,18 +20,13 @@ import (
"context" "context"
"fmt" "fmt"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"github.com/docker/compose-cli/api/compose" "github.com/docker/compose-cli/api/compose"
) )
func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) { func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ containers, err := s.getContainers(ctx, projectName, oneOffInclude, options.All)
Filters: filters.NewArgs(projectFilter(projectName)),
All: options.All,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -29,7 +29,7 @@ import (
) )
func (s *composeService) Remove(ctx context.Context, project *types.Project, options compose.RemoveOptions) ([]string, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -30,10 +30,7 @@ import (
) )
func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) { func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
observedState, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{ observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
Filters: filters.NewArgs(projectFilter(project.Name)),
All: true,
})
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/pkg/errors" "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) { func (s composeService) getUpdatedContainersStateContext(ctx context.Context, projectName string) (context.Context, error) {
observedState, err := s.apiClient.ContainerList(ctx, types.ContainerListOptions{ observedState, err := s.getContainers(ctx, projectName, oneOffInclude, true)
Filters: filters.NewArgs(
projectFilter(projectName),
),
All: true,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -23,23 +23,16 @@ import (
"github.com/docker/compose-cli/api/progress" "github.com/docker/compose-cli/api/progress"
"github.com/compose-spec/compose-go/types" "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 { func (s *composeService) Stop(ctx context.Context, project *types.Project, options compose.StopOptions) error {
w := progress.ContextWriter(ctx) w := progress.ContextWriter(ctx)
var containers Containers var containers Containers
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ containers, err := s.getContainers(ctx, project.Name, oneOffInclude, true, project.ServiceNames()...)
Filters: filters.NewArgs(projectFilter(project.Name)),
All: true,
})
if err != nil { if err != nil {
return err return err
} }
containers = containers.filter(isService(project.ServiceNames()...))
return InReverseDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error { return InReverseDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
return s.stopContainers(ctx, w, containers.filter(isService(service.Name)), options.Timeout) return s.stopContainers(ctx, w, containers.filter(isService(service.Name)), options.Timeout)
}) })

View File

@ -20,16 +20,12 @@ import (
"context" "context"
"github.com/docker/compose-cli/api/compose" "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" "golang.org/x/sync/errgroup"
) )
func (s *composeService) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) { func (s *composeService) Top(ctx context.Context, projectName string, services []string) ([]compose.ContainerProcSummary, error) {
var containers Containers var containers Containers
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{ containers, err := s.getContainers(ctx, projectName, oneOffInclude, false)
Filters: filters.NewArgs(projectFilter(projectName)),
})
if err != nil { if err != nil {
return nil, err return nil, err
} }