pull.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "os"
  18. "github.com/compose-spec/compose-go/v2/types"
  19. "github.com/docker/cli/cli/command"
  20. "github.com/morikuni/aec"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/compose/v2/pkg/api"
  23. )
  24. type pullOptions struct {
  25. *ProjectOptions
  26. composeOptions
  27. quiet bool
  28. parallel bool
  29. noParallel bool
  30. includeDeps bool
  31. ignorePullFailures bool
  32. noBuildable bool
  33. policy string
  34. }
  35. func pullCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  36. opts := pullOptions{
  37. ProjectOptions: p,
  38. }
  39. cmd := &cobra.Command{
  40. Use: "pull [OPTIONS] [SERVICE...]",
  41. Short: "Pull service images",
  42. PreRunE: func(cmd *cobra.Command, args []string) error {
  43. if cmd.Flags().Changed("no-parallel") {
  44. fmt.Fprint(os.Stderr, aec.Apply("option '--no-parallel' is DEPRECATED and will be ignored.\n", aec.RedF))
  45. }
  46. if cmd.Flags().Changed("parallel") {
  47. fmt.Fprint(os.Stderr, aec.Apply("option '--parallel' is DEPRECATED and will be ignored.\n", aec.RedF))
  48. }
  49. return nil
  50. },
  51. RunE: Adapt(func(ctx context.Context, args []string) error {
  52. return runPull(ctx, dockerCli, backend, opts, args)
  53. }),
  54. ValidArgsFunction: completeServiceNames(dockerCli, p),
  55. }
  56. flags := cmd.Flags()
  57. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Pull without printing progress information")
  58. cmd.Flags().BoolVar(&opts.includeDeps, "include-deps", false, "Also pull services declared as dependencies")
  59. cmd.Flags().BoolVar(&opts.parallel, "parallel", true, "DEPRECATED pull multiple images in parallel")
  60. flags.MarkHidden("parallel") //nolint:errcheck
  61. cmd.Flags().BoolVar(&opts.noParallel, "no-parallel", true, "DEPRECATED disable parallel pulling")
  62. flags.MarkHidden("no-parallel") //nolint:errcheck
  63. cmd.Flags().BoolVar(&opts.ignorePullFailures, "ignore-pull-failures", false, "Pull what it can and ignores images with pull failures")
  64. cmd.Flags().BoolVar(&opts.noBuildable, "ignore-buildable", false, "Ignore images that can be built")
  65. cmd.Flags().StringVar(&opts.policy, "policy", "", `Apply pull policy ("missing"|"always")`)
  66. return cmd
  67. }
  68. func (opts pullOptions) apply(project *types.Project, services []string) (*types.Project, error) {
  69. if !opts.includeDeps {
  70. var err error
  71. project, err = project.WithSelectedServices(services, types.IgnoreDependencies)
  72. if err != nil {
  73. return nil, err
  74. }
  75. }
  76. if opts.policy != "" {
  77. for i, service := range project.Services {
  78. if service.Image == "" {
  79. continue
  80. }
  81. service.PullPolicy = opts.policy
  82. project.Services[i] = service
  83. }
  84. }
  85. return project, nil
  86. }
  87. func runPull(ctx context.Context, dockerCli command.Cli, backend api.Service, opts pullOptions, services []string) error {
  88. project, _, err := opts.ToProject(ctx, dockerCli, services)
  89. if err != nil {
  90. return err
  91. }
  92. project, err = opts.apply(project, services)
  93. if err != nil {
  94. return err
  95. }
  96. return backend.Pull(ctx, project, api.PullOptions{
  97. Quiet: opts.quiet,
  98. IgnoreFailures: opts.ignorePullFailures,
  99. IgnoreBuildable: opts.noBuildable,
  100. })
  101. }