up.go 3.5 KB

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