2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-06-27 12:55:10 -04:00
|
|
|
"github.com/spf13/pflag"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2017-06-27 12:55:10 -04:00
|
|
|
type scaleOptions struct {
|
|
|
|
detach bool
|
|
|
|
}
|
|
|
|
|
2017-07-20 04:32:51 -04:00
|
|
|
func newScaleCommand(dockerCli command.Cli) *cobra.Command {
|
2017-06-27 12:55:10 -04:00
|
|
|
options := &scaleOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-09-08 13:11:39 -04:00
|
|
|
Use: "scale SERVICE=REPLICAS [SERVICE=REPLICAS...]",
|
2016-11-02 03:53:18 -04:00
|
|
|
Short: "Scale one or multiple replicated services",
|
2016-09-08 13:11:39 -04:00
|
|
|
Args: scaleArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-06-27 12:55:10 -04:00
|
|
|
return runScale(dockerCli, cmd.Flags(), options, args)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
2017-06-27 12:55:10 -04:00
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
addDetachFlag(flags, &options.detach)
|
|
|
|
return cmd
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func scaleArgs(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := cli.RequiresMinArgs(1)(cmd, args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
if parts := strings.SplitN(arg, "=", 2); len(parts) != 2 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf(
|
2016-09-08 13:11:39 -04:00
|
|
|
"Invalid scale specifier '%s'.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
|
|
|
arg,
|
|
|
|
cmd.CommandPath(),
|
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-20 04:32:51 -04:00
|
|
|
func runScale(dockerCli command.Cli, flags *pflag.FlagSet, options *scaleOptions, args []string) error {
|
2017-03-09 13:23:45 -05:00
|
|
|
var errs []string
|
2017-06-30 06:04:49 -04:00
|
|
|
var serviceIDs []string
|
2017-06-27 12:55:10 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
for _, arg := range args {
|
|
|
|
parts := strings.SplitN(arg, "=", 2)
|
2016-09-19 04:40:44 -04:00
|
|
|
serviceID, scaleStr := parts[0], parts[1]
|
|
|
|
|
|
|
|
// validate input arg scale number
|
|
|
|
scale, err := strconv.ParseUint(scaleStr, 10, 64)
|
|
|
|
if err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
errs = append(errs, fmt.Sprintf("%s: invalid replicas value %s: %v", serviceID, scaleStr, err))
|
2016-09-19 04:40:44 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-27 12:55:10 -04:00
|
|
|
if err := runServiceScale(ctx, dockerCli, serviceID, scale); err != nil {
|
|
|
|
errs = append(errs, fmt.Sprintf("%s: %v", serviceID, err))
|
2017-06-30 06:04:49 -04:00
|
|
|
} else {
|
|
|
|
serviceIDs = append(serviceIDs, serviceID)
|
2017-06-27 12:55:10 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-06-30 06:04:49 -04:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 12:55:10 -04:00
|
|
|
}
|
|
|
|
|
2017-03-09 13:23:45 -05:00
|
|
|
if len(errs) == 0 {
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf(strings.Join(errs, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-07-20 04:32:51 -04:00
|
|
|
func runServiceScale(ctx context.Context, dockerCli command.Cli, serviceID string, scale uint64) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
2017-03-30 20:15:54 -04:00
|
|
|
service, _, err := client.ServiceInspectWithRaw(ctx, serviceID, types.ServiceInspectOptions{})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceMode := &service.Spec.Mode
|
|
|
|
if serviceMode.Replicated == nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("scale can only be used with replicated mode")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-19 04:40:44 -04:00
|
|
|
|
|
|
|
serviceMode.Replicated.Replicas = &scale
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-11-14 21:08:24 -05:00
|
|
|
response, err := client.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:08:24 -05:00
|
|
|
for _, warning := range response.Warnings {
|
|
|
|
fmt.Fprintln(dockerCli.Err(), warning)
|
|
|
|
}
|
|
|
|
|
2016-09-19 04:40:44 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s scaled to %d\n", serviceID, scale)
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|