create.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }
  59. flags := cmd.Flags()
  60. flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
  61. flags.BoolVar(&opts.noBuild, "no-build", false, "Don't build an image, even if it's missing.")
  62. flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
  63. flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
  64. return cmd
  65. }
  66. func (opts createOptions) recreateStrategy() string {
  67. if opts.noRecreate {
  68. return api.RecreateNever
  69. }
  70. if opts.forceRecreate {
  71. return api.RecreateForce
  72. }
  73. return api.RecreateDiverged
  74. }
  75. func (opts createOptions) dependenciesRecreateStrategy() string {
  76. if opts.noRecreate {
  77. return api.RecreateNever
  78. }
  79. if opts.recreateDeps {
  80. return api.RecreateForce
  81. }
  82. return api.RecreateDiverged
  83. }
  84. func (opts createOptions) GetTimeout() *time.Duration {
  85. if opts.timeChanged {
  86. t := time.Duration(opts.timeout) * time.Second
  87. return &t
  88. }
  89. return nil
  90. }
  91. func (opts createOptions) Apply(project *types.Project) {
  92. if opts.Build {
  93. for i, service := range project.Services {
  94. if service.Build == nil {
  95. continue
  96. }
  97. service.PullPolicy = types.PullPolicyBuild
  98. project.Services[i] = service
  99. }
  100. }
  101. if opts.noBuild {
  102. for i, service := range project.Services {
  103. service.Build = nil
  104. project.Services[i] = service
  105. }
  106. }
  107. }