소스 검색

Add --attach-dependencies

This actually makes the service selection
effective and add the flag to disable
the service selection.

Signed-off-by: Ulysses Souza <[email protected]>
Ulysses Souza 4 년 전
부모
커밋
10d024428c
7개의 변경된 파일38개의 추가작업 그리고 26개의 파일을 삭제
  1. 2 0
      api/compose/api.go
  2. 25 18
      cli/cmd/compose/up.go
  3. 2 2
      local/compose/attach.go
  4. 5 2
      local/compose/containers.go
  5. 2 2
      local/compose/pause.go
  6. 1 1
      local/compose/remove.go
  7. 1 1
      local/compose/start.go

+ 2 - 0
api/compose/api.go

@@ -93,6 +93,8 @@ type CreateOptions struct {
 type StartOptions struct {
 	// Attach will attach to service containers and send container logs and events
 	Attach ContainerEventListener
+	// Services passed in the command line to be started
+	Services []string
 }
 
 // StopOptions group options of the Stop API

+ 25 - 18
cli/cmd/compose/up.go

@@ -51,22 +51,23 @@ type composeOptions struct {
 
 type upOptions struct {
 	*composeOptions
-	Detach        bool
-	Environment   []string
-	removeOrphans bool
-	forceRecreate bool
-	noRecreate    bool
-	recreateDeps  bool
-	noStart       bool
-	noDeps        bool
-	cascadeStop   bool
-	exitCodeFrom  string
-	scale         []string
-	noColor       bool
-	noPrefix      bool
-	timeChanged   bool
-	timeout       int
-	noInherit     bool
+	Detach             bool
+	Environment        []string
+	removeOrphans      bool
+	forceRecreate      bool
+	noRecreate         bool
+	recreateDeps       bool
+	noStart            bool
+	noDeps             bool
+	cascadeStop        bool
+	exitCodeFrom       string
+	scale              []string
+	noColor            bool
+	noPrefix           bool
+	timeChanged        bool
+	timeout            int
+	noInherit          bool
+	attachDependencies bool
 }
 
 func (opts upOptions) recreateStrategy() string {
@@ -156,8 +157,8 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
 				if opts.Build && opts.noBuild {
 					return fmt.Errorf("--build and --no-build are incompatible")
 				}
-				if opts.cascadeStop && opts.Detach {
-					return fmt.Errorf("--abort-on-container-exit and --detach are incompatible")
+				if opts.Detach && (opts.attachDependencies || opts.cascadeStop) {
+					return fmt.Errorf("--detach cannot be combined with --abort-on-container-exit or --attach-dependencies")
 				}
 				if opts.forceRecreate && opts.noRecreate {
 					return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
@@ -194,6 +195,7 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
 		flags.BoolVar(&opts.noDeps, "no-deps", false, "Don't start linked services.")
 		flags.BoolVar(&opts.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.")
 		flags.BoolVarP(&opts.noInherit, "renew-anon-volumes", "V", false, "Recreate anonymous volumes instead of retrieving data from the previous containers.")
+		flags.BoolVar(&opts.attachDependencies, "attach-dependencies", false, "Attach to dependent containers.")
 	}
 
 	return upCmd
@@ -254,6 +256,10 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
 		return nil
 	}
 
+	if opts.attachDependencies {
+		services = nil
+	}
+
 	if opts.Detach {
 		return nil
 	}
@@ -295,6 +301,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
 		Attach: func(event compose.ContainerEvent) {
 			queue <- event
 		},
+		Services: services,
 	})
 	if err != nil {
 		return err

+ 2 - 2
local/compose/attach.go

@@ -31,8 +31,8 @@ import (
 	"github.com/docker/docker/pkg/stdcopy"
 )
 
-func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.ContainerEventListener) (Containers, error) {
-	containers, err := s.getContainers(ctx, project, oneOffExclude)
+func (s *composeService) attach(ctx context.Context, project *types.Project, consumer compose.ContainerEventListener, selectedServices []string) (Containers, error) {
+	containers, err := s.getContainers(ctx, project, oneOffExclude, selectedServices)
 	if err != nil {
 		return nil, err
 	}

+ 5 - 2
local/compose/containers.go

@@ -37,7 +37,7 @@ const (
 	oneOffOnly
 )
 
-func (s *composeService) getContainers(ctx context.Context, project *types.Project, oneOff oneOff) (Containers, error) {
+func (s *composeService) getContainers(ctx context.Context, project *types.Project, oneOff oneOff, selectedServices []string) (Containers, error) {
 	var containers Containers
 	f := filters.NewArgs(
 		projectFilter(project.Name),
@@ -56,7 +56,10 @@ func (s *composeService) getContainers(ctx context.Context, project *types.Proje
 	if err != nil {
 		return nil, err
 	}
-	containers = containers.filter(isService(project.ServiceNames()...))
+	if len(selectedServices) == 0 {
+		selectedServices = project.ServiceNames()
+	}
+	containers = containers.filter(isService(selectedServices...))
 	return containers, nil
 }
 

+ 2 - 2
local/compose/pause.go

@@ -27,7 +27,7 @@ import (
 )
 
 func (s *composeService) Pause(ctx context.Context, project *types.Project) error {
-	containers, err := s.getContainers(ctx, project, oneOffExclude)
+	containers, err := s.getContainers(ctx, project, oneOffExclude, nil)
 	if err != nil {
 		return err
 	}
@@ -49,7 +49,7 @@ func (s *composeService) Pause(ctx context.Context, project *types.Project) erro
 }
 
 func (s *composeService) UnPause(ctx context.Context, project *types.Project) error {
-	containers, err := s.getContainers(ctx, project, oneOffExclude)
+	containers, err := s.getContainers(ctx, project, oneOffExclude, nil)
 	if err != nil {
 		return err
 	}

+ 1 - 1
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)
+	containers, err := s.getContainers(ctx, project, oneOffInclude, nil)
 	if err != nil {
 		return nil, err
 	}

+ 1 - 1
local/compose/start.go

@@ -29,7 +29,7 @@ import (
 func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
 	var containers Containers
 	if options.Attach != nil {
-		c, err := s.attach(ctx, project, options.Attach)
+		c, err := s.attach(ctx, project, options.Attach, options.Services)
 		if err != nil {
 			return err
 		}