create.go 3.4 KB

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