-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Guillaume Lours <[email protected]>
- Loading branch information
Showing
15 changed files
with
404 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package compose | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/docker/cli/cli/command" | ||
|
||
"github.com/compose-spec/compose-go/types" | ||
"github.com/pkg/errors" | ||
"golang.org/x/exp/maps" | ||
|
||
"github.com/docker/compose/v2/pkg/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type scaleOptions struct { | ||
*ProjectOptions | ||
noDeps bool | ||
} | ||
|
||
func scaleCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command { | ||
opts := scaleOptions{ | ||
ProjectOptions: p, | ||
} | ||
scaleCmd := &cobra.Command{ | ||
Use: "scale [SERVICE=REPLICAS...]", | ||
Short: "Scale services ", | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: Adapt(func(ctx context.Context, args []string) error { | ||
serviceTuples, err := parseServicesReplicasArgs(args) | ||
if err != nil { | ||
return err | ||
} | ||
return runScale(ctx, dockerCli, backend, opts, serviceTuples) | ||
}), | ||
ValidArgsFunction: completeServiceNames(dockerCli, p), | ||
} | ||
flags := scaleCmd.Flags() | ||
flags.BoolVar(&opts.noDeps, "no-deps", false, "Don't start linked services.") | ||
|
||
return scaleCmd | ||
} | ||
|
||
func runScale(ctx context.Context, dockerCli command.Cli, backend api.Service, opts scaleOptions, serviceReplicaTuples map[string]int) error { | ||
services := maps.Keys(serviceReplicaTuples) | ||
project, err := opts.ToProject(dockerCli, services) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if opts.noDeps { | ||
if err := project.ForServices(services, types.IgnoreDependencies); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
for key, value := range serviceReplicaTuples { | ||
for i, service := range project.Services { | ||
if service.Name != key { | ||
continue | ||
} | ||
if service.Deploy == nil { | ||
service.Deploy = &types.DeployConfig{} | ||
} | ||
scale := uint64(value) | ||
service.Deploy.Replicas = &scale | ||
project.Services[i] = service | ||
break | ||
} | ||
} | ||
|
||
return backend.Scale(ctx, project, api.ScaleOptions{Services: services}) | ||
} | ||
|
||
func parseServicesReplicasArgs(args []string) (map[string]int, error) { | ||
serviceReplicaTuples := map[string]int{} | ||
for _, arg := range args { | ||
key, val, ok := strings.Cut(arg, "=") | ||
if !ok || key == "" || val == "" { | ||
return nil, errors.Errorf("Invalide scale specifier %q.", arg) | ||
} | ||
intValue, err := strconv.Atoi(val) | ||
|
||
if err != nil { | ||
return nil, errors.Errorf("Invalide scale specifier, can't parse replicate value to int %q.", arg) | ||
} | ||
serviceReplicaTuples[key] = intValue | ||
} | ||
return serviceReplicaTuples, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# docker compose alpha scale | ||
|
||
<!---MARKER_GEN_START--> | ||
Scale services | ||
|
||
### Options | ||
|
||
| Name | Type | Default | Description | | ||
|:------------|:-----|:--------|:--------------------------------| | ||
| `--dry-run` | | | Execute command in dry run mode | | ||
| `--no-deps` | | | Don't start linked services. | | ||
|
||
|
||
<!---MARKER_GEN_END--> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# docker compose scale | ||
|
||
<!---MARKER_GEN_START--> | ||
Scale services | ||
|
||
### Options | ||
|
||
| Name | Type | Default | Description | | ||
|:------------|:-----|:--------|:--------------------------------| | ||
| `--dry-run` | | | Execute command in dry run mode | | ||
| `--no-deps` | | | Don't start linked services. | | ||
|
||
|
||
<!---MARKER_GEN_END--> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
command: docker compose alpha scale | ||
short: Scale services | ||
long: Scale services | ||
usage: docker compose alpha scale [SERVICE=REPLICAS...] | ||
pname: docker compose alpha | ||
plink: docker_compose_alpha.yaml | ||
options: | ||
- option: no-deps | ||
value_type: bool | ||
default_value: "false" | ||
description: Don't start linked services. | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
inherited_options: | ||
- option: dry-run | ||
value_type: bool | ||
default_value: "false" | ||
description: Execute command in dry run mode | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: true | ||
kubernetes: false | ||
swarm: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
command: docker compose scale | ||
short: Scale services | ||
long: Scale services | ||
usage: docker compose scale [SERVICE=REPLICAS...] | ||
pname: docker compose | ||
plink: docker_compose.yaml | ||
options: | ||
- option: no-deps | ||
value_type: bool | ||
default_value: "false" | ||
description: Don't start linked services. | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
inherited_options: | ||
- option: dry-run | ||
value_type: bool | ||
default_value: "false" | ||
description: Execute command in dry run mode | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package compose | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/compose-spec/compose-go/types" | ||
"github.com/docker/compose/v2/internal/tracing" | ||
"github.com/docker/compose/v2/pkg/api" | ||
"github.com/docker/compose/v2/pkg/progress" | ||
) | ||
|
||
func (s *composeService) Scale(ctx context.Context, project *types.Project, options api.ScaleOptions) error { | ||
return progress.Run(ctx, tracing.SpanWrapFunc("project/scale", tracing.ProjectOptions(project), func(ctx context.Context) error { | ||
err := s.create(ctx, project, api.CreateOptions{Services: options.Services}) | ||
if err != nil { | ||
return err | ||
} | ||
return s.start(ctx, project.Name, api.StartOptions{Project: project, Services: options.Services}, nil) | ||
|
||
}), s.stdinfo()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
services: | ||
back: | ||
image: nginx:alpine | ||
depends_on: | ||
- db | ||
db: | ||
image: nginx:alpine | ||
front: | ||
image: nginx:alpine | ||
deploy: | ||
replicas: 2 | ||
dbadmin: | ||
image: nginx:alpine | ||
deploy: | ||
replicas: 0 |
Oops, something went wrong.