up.go 4.0 KB

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