up.go 3.7 KB

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