Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 9601aa5

Browse files
committed
add support for up --scale
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent f52bdc5 commit 9601aa5

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

cli/cmd/compose/up.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"os"
2323
"os/signal"
2424
"path/filepath"
25+
"strconv"
26+
"strings"
2527
"syscall"
2628

2729
"github.com/docker/compose-cli/api/client"
@@ -54,6 +56,7 @@ type upOptions struct {
5456
noStart bool
5557
cascadeStop bool
5658
exitCodeFrom string
59+
scale []string
5760
}
5861

5962
func (o upOptions) recreateStrategy() string {
@@ -98,6 +101,7 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
98101
flags.BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background")
99102
flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
100103
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
104+
flags.StringArrayVar(&opts.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
101105

102106
switch contextType {
103107
case store.AciContextType:
@@ -119,6 +123,11 @@ func runUp(ctx context.Context, opts upOptions, services []string) error {
119123
return err
120124
}
121125

126+
err = applyScale(opts.scale, project)
127+
if err != nil {
128+
return err
129+
}
130+
122131
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
123132
return "", c.ComposeService().Up(ctx, project, compose.UpOptions{
124133
Detach: opts.Detach,
@@ -133,6 +142,11 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
133142
return err
134143
}
135144

145+
err = applyScale(opts.scale, project)
146+
if err != nil {
147+
return err
148+
}
149+
136150
if opts.exitCodeFrom != "" {
137151
_, err := project.GetService(opts.exitCodeFrom)
138152
if err != nil {
@@ -201,6 +215,38 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
201215
return err
202216
}
203217

218+
func applyScale(opts []string, project *types.Project) error {
219+
SCALE:
220+
for _, scale := range opts {
221+
split := strings.Split(scale, "=")
222+
if len(split) != 2 {
223+
return fmt.Errorf("invalid --scale option %q. Should be SERVICE=NUM", scale)
224+
}
225+
name := split[0]
226+
replicas, err := strconv.Atoi(split[1])
227+
if err != nil {
228+
return err
229+
}
230+
for i, s := range project.Services {
231+
if s.Name == name {
232+
service, err := project.GetService(name)
233+
if err != nil {
234+
return err
235+
}
236+
if service.Deploy == nil {
237+
service.Deploy = &types.DeployConfig{}
238+
}
239+
count := uint64(replicas)
240+
service.Deploy.Replicas = &count
241+
project.Services[i] = service
242+
continue SCALE
243+
}
244+
}
245+
return fmt.Errorf("unknown service %q", name)
246+
}
247+
return nil
248+
}
249+
204250
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
205251
c, err := client.NewWithDefaultLocalBackend(ctx)
206252
if err != nil {

0 commit comments

Comments
 (0)