scale.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/v2/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(ctx, dockerCli, services)
  53. if err != nil {
  54. return err
  55. }
  56. if opts.noDeps {
  57. if project, err = project.WithSelectedServices(services, types.IgnoreDependencies); err != nil {
  58. return err
  59. }
  60. }
  61. for key, value := range serviceReplicaTuples {
  62. service, err := project.GetService(key)
  63. if err != nil {
  64. return err
  65. }
  66. service.SetScale(value)
  67. project.Services[key] = service
  68. }
  69. return backend.Scale(ctx, project, api.ScaleOptions{Services: services})
  70. }
  71. func parseServicesReplicasArgs(args []string) (map[string]int, error) {
  72. serviceReplicaTuples := map[string]int{}
  73. for _, arg := range args {
  74. key, val, ok := strings.Cut(arg, "=")
  75. if !ok || key == "" || val == "" {
  76. return nil, fmt.Errorf("invalid scale specifier: %s", arg)
  77. }
  78. intValue, err := strconv.Atoi(val)
  79. if err != nil {
  80. return nil, fmt.Errorf("invalid scale specifier: can't parse replica value as int: %v", arg)
  81. }
  82. serviceReplicaTuples[key] = intValue
  83. }
  84. return serviceReplicaTuples, nil
  85. }