add --detach to docker scale

Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
Victor Vieux 2017-06-27 09:55:10 -07:00
parent 2eac0bb7b7
commit 6c6b1091d9
2 changed files with 32 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,10 @@ 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
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,9 +69,21 @@ 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))
} }
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 { if len(errs) == 0 {
@ -67,9 +92,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
``` ```