up.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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:
  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. if contextType == store.AciContextType {
  48. upCmd.Flags().StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name")
  49. }
  50. return upCmd
  51. }
  52. func runUp(ctx context.Context, opts composeOptions, services []string) error {
  53. c, project, err := setup(ctx, opts, services)
  54. if err != nil {
  55. return err
  56. }
  57. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  58. return "", c.ComposeService().Up(ctx, project, opts.Detach)
  59. })
  60. return err
  61. }
  62. func runCreateStart(ctx context.Context, opts composeOptions, services []string) error {
  63. c, project, err := setup(ctx, opts, services)
  64. if err != nil {
  65. return err
  66. }
  67. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  68. return "", c.ComposeService().Create(ctx, project)
  69. })
  70. if err != nil {
  71. return err
  72. }
  73. var consumer compose.LogConsumer
  74. if !opts.Detach {
  75. consumer = formatter.NewLogConsumer(ctx, os.Stdout)
  76. }
  77. err = c.ComposeService().Start(ctx, project, consumer)
  78. if errors.Is(ctx.Err(), context.Canceled) {
  79. fmt.Println("Gracefully stopping...")
  80. ctx = context.Background()
  81. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  82. return "", c.ComposeService().Down(ctx, project.Name)
  83. })
  84. }
  85. return err
  86. }
  87. func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
  88. c, err := client.NewWithDefaultLocalBackend(ctx)
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. options, err := opts.toProjectOptions()
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. project, err := cli.ProjectFromOptions(options)
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. if opts.DomainName != "" {
  101. // arbitrarily set the domain name on the first service ; ACI backend will expose the entire project
  102. project.Services[0].DomainName = opts.DomainName
  103. }
  104. err = filter(project, services)
  105. if err != nil {
  106. return nil, nil, err
  107. }
  108. return c, project, nil
  109. }