github.com/portworx/docker@v1.12.1/api/client/service/scale.go (about) 1 package service 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 8 "golang.org/x/net/context" 9 10 "github.com/docker/docker/api/client" 11 "github.com/docker/docker/cli" 12 "github.com/docker/engine-api/types" 13 "github.com/spf13/cobra" 14 ) 15 16 func newScaleCommand(dockerCli *client.DockerCli) *cobra.Command { 17 return &cobra.Command{ 18 Use: "scale SERVICE=REPLICAS [SERVICE=REPLICAS...]", 19 Short: "Scale one or multiple services", 20 Args: scaleArgs, 21 RunE: func(cmd *cobra.Command, args []string) error { 22 return runScale(dockerCli, args) 23 }, 24 } 25 } 26 27 func scaleArgs(cmd *cobra.Command, args []string) error { 28 if err := cli.RequiresMinArgs(1)(cmd, args); err != nil { 29 return err 30 } 31 for _, arg := range args { 32 if parts := strings.SplitN(arg, "=", 2); len(parts) != 2 { 33 return fmt.Errorf( 34 "Invalid scale specifier '%s'.\nSee '%s --help'.\n\nUsage: %s\n\n%s", 35 arg, 36 cmd.CommandPath(), 37 cmd.UseLine(), 38 cmd.Short, 39 ) 40 } 41 } 42 return nil 43 } 44 45 func runScale(dockerCli *client.DockerCli, args []string) error { 46 var errors []string 47 for _, arg := range args { 48 parts := strings.SplitN(arg, "=", 2) 49 serviceID, scale := parts[0], parts[1] 50 if err := runServiceScale(dockerCli, serviceID, scale); err != nil { 51 errors = append(errors, fmt.Sprintf("%s: %s", serviceID, err.Error())) 52 } 53 } 54 55 if len(errors) == 0 { 56 return nil 57 } 58 return fmt.Errorf(strings.Join(errors, "\n")) 59 } 60 61 func runServiceScale(dockerCli *client.DockerCli, serviceID string, scale string) error { 62 client := dockerCli.Client() 63 ctx := context.Background() 64 65 service, _, err := client.ServiceInspectWithRaw(ctx, serviceID) 66 67 if err != nil { 68 return err 69 } 70 71 serviceMode := &service.Spec.Mode 72 if serviceMode.Replicated == nil { 73 return fmt.Errorf("scale can only be used with replicated mode") 74 } 75 uintScale, err := strconv.ParseUint(scale, 10, 64) 76 if err != nil { 77 return fmt.Errorf("invalid replicas value %s: %s", scale, err.Error()) 78 } 79 serviceMode.Replicated.Replicas = &uintScale 80 81 err = client.ServiceUpdate(ctx, service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{}) 82 if err != nil { 83 return err 84 } 85 86 fmt.Fprintf(dockerCli.Out(), "%s scaled to %s\n", serviceID, scale) 87 return nil 88 }