create.go 3.4 KB

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