pull.go 3.9 KB

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