scale.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "github.com/docker/cli/cli/command"
  20. "github.com/compose-spec/compose-go/types"
  21. "golang.org/x/exp/maps"
  22. "github.com/docker/compose/v2/pkg/api"
  23. "github.com/spf13/cobra"
  24. )
  25. type scaleOptions struct {
  26. *ProjectOptions
  27. noDeps bool
  28. }
  29. func scaleCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  30. opts := scaleOptions{
  31. ProjectOptions: p,
  32. }
  33. scaleCmd := &cobra.Command{
  34. Use: "scale [SERVICE=REPLICAS...]",
  35. Short: "Scale services ",
  36. Args: cobra.MinimumNArgs(1),
  37. RunE: Adapt(func(ctx context.Context, args []string) error {
  38. serviceTuples, err := parseServicesReplicasArgs(args)
  39. if err != nil {
  40. return err
  41. }
  42. return runScale(ctx, dockerCli, backend, opts, serviceTuples)
  43. }),
  44. ValidArgsFunction: completeServiceNames(dockerCli, p),
  45. }
  46. flags := scaleCmd.Flags()
  47. flags.BoolVar(&opts.noDeps, "no-deps", false, "Don't start linked services.")
  48. return scaleCmd
  49. }
  50. func runScale(ctx context.Context, dockerCli command.Cli, backend api.Service, opts scaleOptions, serviceReplicaTuples map[string]int) error {
  51. services := maps.Keys(serviceReplicaTuples)
  52. project, err := opts.ToProject(dockerCli, services)
  53. if err != nil {
  54. return err
  55. }
  56. if opts.noDeps {
  57. if err := project.ForServices(services, types.IgnoreDependencies); err != nil {
  58. return err
  59. }
  60. }
  61. for key, value := range serviceReplicaTuples {
  62. for i, service := range project.Services {
  63. if service.Name != key {
  64. continue
  65. }
  66. if service.Deploy == nil {
  67. service.Deploy = &types.DeployConfig{}
  68. }
  69. scale := uint64(value)
  70. service.Deploy.Replicas = &scale
  71. project.Services[i] = service
  72. break
  73. }
  74. }
  75. return backend.Scale(ctx, project, api.ScaleOptions{Services: services})
  76. }
  77. func parseServicesReplicasArgs(args []string) (map[string]int, error) {
  78. serviceReplicaTuples := map[string]int{}
  79. for _, arg := range args {
  80. key, val, ok := strings.Cut(arg, "=")
  81. if !ok || key == "" || val == "" {
  82. return nil, fmt.Errorf("invalid scale specifier: %s", arg)
  83. }
  84. intValue, err := strconv.Atoi(val)
  85. if err != nil {
  86. return nil, fmt.Errorf("invalid scale specifier: can't parse replica value as int: %v", arg)
  87. }
  88. serviceReplicaTuples[key] = intValue
  89. }
  90. return serviceReplicaTuples, nil
  91. }