From 6c6b1091d9ff48d22f73448bc4d91b1ce3bc8ad8 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 27 Jun 2017 09:55:10 -0700 Subject: [PATCH 1/2] add --detach to docker scale Signed-off-by: Victor Vieux --- cli/command/service/scale.go | 36 +++++++++++++++++---- docs/reference/commandline/service_scale.md | 3 +- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/cli/command/service/scale.go b/cli/command/service/scale.go index 397fe1800c..7a23a26626 100644 --- a/cli/command/service/scale.go +++ b/cli/command/service/scale.go @@ -12,17 +12,28 @@ import ( "github.com/docker/docker/api/types" "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) +type scaleOptions struct { + detach bool +} + func newScaleCommand(dockerCli *command.DockerCli) *cobra.Command { - return &cobra.Command{ + options := &scaleOptions{} + + cmd := &cobra.Command{ Use: "scale SERVICE=REPLICAS [SERVICE=REPLICAS...]", Short: "Scale one or multiple replicated services", Args: scaleArgs, RunE: func(cmd *cobra.Command, args []string) error { - return runScale(dockerCli, args) + return runScale(dockerCli, cmd.Flags(), options, args) }, } + + flags := cmd.Flags() + addDetachFlag(flags, &options.detach) + return cmd } func scaleArgs(cmd *cobra.Command, args []string) error { @@ -43,8 +54,10 @@ func scaleArgs(cmd *cobra.Command, args []string) error { return nil } -func runScale(dockerCli *command.DockerCli, args []string) error { +func runScale(dockerCli *command.DockerCli, flags *pflag.FlagSet, options *scaleOptions, args []string) error { var errs []string + ctx := context.Background() + for _, arg := range args { parts := strings.SplitN(arg, "=", 2) serviceID, scaleStr := parts[0], parts[1] @@ -56,9 +69,21 @@ func runScale(dockerCli *command.DockerCli, args []string) error { continue } - if err := runServiceScale(dockerCli, serviceID, scale); err != nil { + if err := runServiceScale(ctx, dockerCli, serviceID, scale); err != nil { errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err)) } + + if options.detach { + continue + } + + if err := waitOnService(ctx, dockerCli, serviceID, false); err != nil { + errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err)) + } + } + + if options.detach { + warnDetachDefault(dockerCli.Err(), dockerCli.Client().ClientVersion(), flags, "scaled") } if len(errs) == 0 { @@ -67,9 +92,8 @@ func runScale(dockerCli *command.DockerCli, args []string) error { return errors.Errorf(strings.Join(errs, "\n")) } -func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale uint64) error { +func runServiceScale(ctx context.Context, dockerCli *command.DockerCli, serviceID string, scale uint64) error { client := dockerCli.Client() - ctx := context.Background() service, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{}) if err != nil { diff --git a/docs/reference/commandline/service_scale.md b/docs/reference/commandline/service_scale.md index a3aef5fd34..47d49faa25 100644 --- a/docs/reference/commandline/service_scale.md +++ b/docs/reference/commandline/service_scale.md @@ -16,11 +16,12 @@ keywords: "service, scale" # service scale ```markdown -Usage: docker service scale SERVICE=REPLICAS [SERVICE=REPLICAS...] +Usage: docker service scale [OPTIONS] SERVICE=REPLICAS [SERVICE=REPLICAS...] Scale one or multiple replicated services Options: + -d, --detach Exit immediately instead of waiting for the service to converge (default true) --help Print usage ``` From 58d3fc24b188c7cb5eff8ceee61d340e27b19aae Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 30 Jun 2017 12:04:49 +0200 Subject: [PATCH 2/2] scale all services at once Signed-off-by: Victor Vieux --- cli/command/service/scale.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cli/command/service/scale.go b/cli/command/service/scale.go index 7a23a26626..f58786156a 100644 --- a/cli/command/service/scale.go +++ b/cli/command/service/scale.go @@ -56,6 +56,7 @@ func scaleArgs(cmd *cobra.Command, args []string) error { func runScale(dockerCli *command.DockerCli, flags *pflag.FlagSet, options *scaleOptions, args []string) error { var errs []string + var serviceIDs []string ctx := context.Background() for _, arg := range args { @@ -71,19 +72,22 @@ func runScale(dockerCli *command.DockerCli, flags *pflag.FlagSet, options *scale if err := runServiceScale(ctx, dockerCli, serviceID, scale); err != nil { errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err)) + } else { + serviceIDs = append(serviceIDs, serviceID) } - if options.detach { - continue - } - - if err := waitOnService(ctx, dockerCli, serviceID, false); err != nil { - errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err)) - } } - if options.detach { - warnDetachDefault(dockerCli.Err(), dockerCli.Client().ClientVersion(), flags, "scaled") + if len(serviceIDs) > 0 { + if options.detach { + warnDetachDefault(dockerCli.Err(), dockerCli.Client().ClientVersion(), flags, "scaled") + } else { + for _, serviceID := range serviceIDs { + if err := waitOnService(ctx, dockerCli, serviceID, false); err != nil { + errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err)) + } + } + } } if len(errs) == 0 {