up.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "errors"
  17. "fmt"
  18. "os"
  19. "github.com/docker/compose-cli/api/client"
  20. "github.com/docker/compose-cli/api/compose"
  21. "github.com/docker/compose-cli/api/context/store"
  22. "github.com/docker/compose-cli/api/progress"
  23. "github.com/docker/compose-cli/cli/formatter"
  24. "github.com/compose-spec/compose-go/types"
  25. "github.com/spf13/cobra"
  26. )
  27. // composeOptions hold options common to `up` and `run` to run compose project
  28. type composeOptions struct {
  29. *projectOptions
  30. Build bool
  31. // ACI only
  32. DomainName string
  33. }
  34. type upOptions struct {
  35. *composeOptions
  36. Detach bool
  37. Environment []string
  38. removeOrphans bool
  39. }
  40. func upCommand(p *projectOptions, contextType string) *cobra.Command {
  41. opts := upOptions{
  42. composeOptions: &composeOptions{
  43. projectOptions: p,
  44. },
  45. }
  46. upCmd := &cobra.Command{
  47. Use: "up [SERVICE...]",
  48. Short: "Create and start containers",
  49. RunE: func(cmd *cobra.Command, args []string) error {
  50. switch contextType {
  51. case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
  52. return runCreateStart(cmd.Context(), opts, args)
  53. default:
  54. return runUp(cmd.Context(), opts, args)
  55. }
  56. },
  57. }
  58. flags := upCmd.Flags()
  59. flags.StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables")
  60. flags.BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background")
  61. flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
  62. flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
  63. if contextType == store.AciContextType {
  64. flags.StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
  65. }
  66. return upCmd
  67. }
  68. func runUp(ctx context.Context, opts upOptions, services []string) error {
  69. c, project, err := setup(ctx, *opts.composeOptions, services)
  70. if err != nil {
  71. return err
  72. }
  73. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  74. return "", c.ComposeService().Up(ctx, project, compose.UpOptions{
  75. Detach: opts.Detach,
  76. })
  77. })
  78. return err
  79. }
  80. func runCreateStart(ctx context.Context, opts upOptions, services []string) error {
  81. c, project, err := setup(ctx, *opts.composeOptions, services)
  82. if err != nil {
  83. return err
  84. }
  85. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  86. return "", c.ComposeService().Create(ctx, project, compose.CreateOptions{
  87. RemoveOrphans: opts.removeOrphans,
  88. })
  89. })
  90. if err != nil {
  91. return err
  92. }
  93. var consumer compose.LogConsumer
  94. if !opts.Detach {
  95. consumer = formatter.NewLogConsumer(ctx, os.Stdout)
  96. }
  97. err = c.ComposeService().Start(ctx, project, consumer)
  98. if errors.Is(ctx.Err(), context.Canceled) {
  99. fmt.Println("Gracefully stopping...")
  100. ctx = context.Background()
  101. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  102. return "", c.ComposeService().Down(ctx, project.Name, compose.DownOptions{})
  103. })
  104. }
  105. return err
  106. }
  107. func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
  108. c, err := client.NewWithDefaultLocalBackend(ctx)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. project, err := opts.toProject()
  113. if err != nil {
  114. return nil, nil, err
  115. }
  116. if opts.DomainName != "" {
  117. // arbitrarily set the domain name on the first service ; ACI backend will expose the entire project
  118. project.Services[0].DomainName = opts.DomainName
  119. }
  120. if opts.Build {
  121. for _, service := range project.Services {
  122. service.PullPolicy = types.PullPolicyBuild
  123. }
  124. }
  125. err = filter(project, services)
  126. if err != nil {
  127. return nil, nil, err
  128. }
  129. return c, project, nil
  130. }