2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newScaleCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
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 {
|
|
|
|
return runScale(dockerCli, args)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Invalid scale specifier '%s'.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
|
|
|
|
arg,
|
|
|
|
cmd.CommandPath(),
|
|
|
|
cmd.UseLine(),
|
|
|
|
cmd.Short,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runScale(dockerCli *command.DockerCli, args []string) error {
|
|
|
|
var errors []string
|
|
|
|
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 {
|
|
|
|
errors = append(errors, fmt.Sprintf("%s: invalid replicas value %s: %v", serviceID, scaleStr, err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
if err := runServiceScale(dockerCli, serviceID, scale); err != nil {
|
2016-09-19 04:40:44 -04:00
|
|
|
errors = append(errors, fmt.Sprintf("%s: %v", serviceID, err))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf(strings.Join(errors, "\n"))
|
|
|
|
}
|
|
|
|
|
2016-09-19 04:40:44 -04:00
|
|
|
func runServiceScale(dockerCli *command.DockerCli, serviceID string, scale uint64) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
service, _, err := client.ServiceInspectWithRaw(ctx, serviceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceMode := &service.Spec.Mode
|
|
|
|
if serviceMode.Replicated == nil {
|
|
|
|
return fmt.Errorf("scale can only be used with replicated mode")
|
|
|
|
}
|
2016-09-19 04:40:44 -04:00
|
|
|
|
|
|
|
serviceMode.Replicated.Replicas = &scale
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
err = client.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|