create.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "time"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/spf13/cobra"
  20. "github.com/docker/compose/v2/pkg/api"
  21. )
  22. type createOptions struct {
  23. Build bool
  24. noBuild bool
  25. Pull string
  26. removeOrphans bool
  27. ignoreOrphans bool
  28. forceRecreate bool
  29. noRecreate bool
  30. recreateDeps bool
  31. noInherit bool
  32. timeChanged bool
  33. timeout int
  34. quietPull bool
  35. }
  36. func createCommand(p *projectOptions, backend api.Service) *cobra.Command {
  37. opts := createOptions{}
  38. cmd := &cobra.Command{
  39. Use: "create [SERVICE...]",
  40. Short: "Creates containers for a service.",
  41. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  42. if opts.Build && opts.noBuild {
  43. return fmt.Errorf("--build and --no-build are incompatible")
  44. }
  45. if opts.forceRecreate && opts.noRecreate {
  46. return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
  47. }
  48. return nil
  49. }),
  50. RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
  51. opts.Apply(project)
  52. return backend.Create(ctx, project, api.CreateOptions{
  53. RemoveOrphans: opts.removeOrphans,
  54. IgnoreOrphans: opts.ignoreOrphans,
  55. Recreate: opts.recreateStrategy(),
  56. RecreateDependencies: opts.dependenciesRecreateStrategy(),
  57. Inherit: !opts.noInherit,
  58. Timeout: opts.GetTimeout(),
  59. QuietPull: false,
  60. })
  61. }),
  62. ValidArgsFunction: serviceCompletion(p),
  63. }
  64. flags := cmd.Flags()
  65. flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
  66. flags.BoolVar(&opts.noBuild, "no-build", false, "Don't build an image, even if it's missing.")
  67. flags.StringVar(&opts.Pull, "pull", "missing", `Pull image before running ("always"|"missing"|"never")`)
  68. flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
  69. flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
  70. return cmd
  71. }
  72. func (opts createOptions) recreateStrategy() string {
  73. if opts.noRecreate {
  74. return api.RecreateNever
  75. }
  76. if opts.forceRecreate {
  77. return api.RecreateForce
  78. }
  79. return api.RecreateDiverged
  80. }
  81. func (opts createOptions) dependenciesRecreateStrategy() string {
  82. if opts.noRecreate {
  83. return api.RecreateNever
  84. }
  85. if opts.recreateDeps {
  86. return api.RecreateForce
  87. }
  88. return api.RecreateDiverged
  89. }
  90. func (opts createOptions) GetTimeout() *time.Duration {
  91. if opts.timeChanged {
  92. t := time.Duration(opts.timeout) * time.Second
  93. return &t
  94. }
  95. return nil
  96. }
  97. func (opts createOptions) Apply(project *types.Project) {
  98. if opts.Pull != "" {
  99. for i, service := range project.Services {
  100. service.PullPolicy = opts.Pull
  101. project.Services[i] = service
  102. }
  103. }
  104. if opts.Build {
  105. for i, service := range project.Services {
  106. if service.Build == nil {
  107. continue
  108. }
  109. service.PullPolicy = types.PullPolicyBuild
  110. project.Services[i] = service
  111. }
  112. }
  113. if opts.noBuild {
  114. for i, service := range project.Services {
  115. service.Build = nil
  116. project.Services[i] = service
  117. }
  118. }
  119. }