Merge pull request #243 from vieux/scale2

add `--detach` to docker scale
This commit is contained in:
Victor Vieux 2017-07-04 16:36:59 +02:00 committed by GitHub
commit 43fb4a4547
2 changed files with 36 additions and 7 deletions

View File

@ -12,17 +12,28 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
) )
type scaleOptions struct {
detach bool
}
func newScaleCommand(dockerCli *command.DockerCli) *cobra.Command { func newScaleCommand(dockerCli *command.DockerCli) *cobra.Command {
return &cobra.Command{ options := &scaleOptions{}
cmd := &cobra.Command{
Use: "scale SERVICE=REPLICAS [SERVICE=REPLICAS...]", Use: "scale SERVICE=REPLICAS [SERVICE=REPLICAS...]",
Short: "Scale one or multiple replicated services", Short: "Scale one or multiple replicated services",
Args: scaleArgs, Args: scaleArgs,
RunE: func(cmd *cobra.Command, args []string) error { 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 { func scaleArgs(cmd *cobra.Command, args []string) error {
@ -43,8 +54,11 @@ func scaleArgs(cmd *cobra.Command, args []string) error {
return nil 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 var errs []string
var serviceIDs []string
ctx := context.Background()
for _, arg := range args { for _, arg := range args {
parts := strings.SplitN(arg, "=", 2) parts := strings.SplitN(arg, "=", 2)
serviceID, scaleStr := parts[0], parts[1] serviceID, scaleStr := parts[0], parts[1]
@ -56,8 +70,23 @@ func runScale(dockerCli *command.DockerCli, args []string) error {
continue 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)) errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err))
} else {
serviceIDs = append(serviceIDs, serviceID)
}
}
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))
}
}
} }
} }
@ -67,9 +96,8 @@ func runScale(dockerCli *command.DockerCli, args []string) error {
return errors.Errorf(strings.Join(errs, "\n")) 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() client := dockerCli.Client()
ctx := context.Background()
service, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{}) service, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
if err != nil { if err != nil {

View File

@ -16,11 +16,12 @@ keywords: "service, scale"
# service scale # service scale
```markdown ```markdown
Usage: docker service scale SERVICE=REPLICAS [SERVICE=REPLICAS...] Usage: docker service scale [OPTIONS] SERVICE=REPLICAS [SERVICE=REPLICAS...]
Scale one or multiple replicated services Scale one or multiple replicated services
Options: Options:
-d, --detach Exit immediately instead of waiting for the service to converge (default true)
--help Print usage --help Print usage
``` ```