up.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "fmt"
  17. "os"
  18. "os/signal"
  19. "syscall"
  20. "github.com/compose-spec/compose-go/v2/types"
  21. "github.com/docker/cli/cli"
  22. "github.com/docker/compose/v2/internal/tracing"
  23. "github.com/docker/compose/v2/pkg/api"
  24. "github.com/docker/compose/v2/pkg/progress"
  25. "github.com/hashicorp/go-multierror"
  26. )
  27. func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error { //nolint:gocyclo
  28. err := progress.Run(ctx, tracing.SpanWrapFunc("project/up", tracing.ProjectOptions(ctx, project), func(ctx context.Context) error {
  29. w := progress.ContextWriter(ctx)
  30. err := s.create(ctx, project, options.Create, options.Start.Attach != nil)
  31. if err != nil {
  32. return err
  33. }
  34. if options.Start.Attach == nil {
  35. w.HasMore(false)
  36. return s.start(ctx, project.Name, options.Start, nil)
  37. }
  38. return nil
  39. }), s.stdinfo())
  40. if err != nil {
  41. return err
  42. }
  43. if options.Start.Attach == nil {
  44. return err
  45. }
  46. if s.dryRun {
  47. fmt.Fprintln(s.stdout(), "end of 'compose up' output, interactive run is not supported in dry-run mode")
  48. return err
  49. }
  50. var eg multierror.Group
  51. // if we get a second signal during shutdown, we kill the services
  52. // immediately, so the channel needs to have sufficient capacity or
  53. // we might miss a signal while setting up the second channel read
  54. // (this is also why signal.Notify is used vs signal.NotifyContext)
  55. signalChan := make(chan os.Signal, 2)
  56. signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
  57. defer close(signalChan)
  58. var isTerminated bool
  59. printer := newLogPrinter(options.Start.Attach)
  60. doneCh := make(chan bool)
  61. eg.Go(func() error {
  62. first := true
  63. gracefulTeardown := func() {
  64. printer.Cancel()
  65. fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
  66. eg.Go(func() error {
  67. err := s.Stop(context.Background(), project.Name, api.StopOptions{
  68. Services: options.Create.Services,
  69. Project: project,
  70. })
  71. isTerminated = true
  72. close(doneCh)
  73. return err
  74. })
  75. first = false
  76. }
  77. for {
  78. select {
  79. case <-doneCh:
  80. return nil
  81. case <-ctx.Done():
  82. if first {
  83. gracefulTeardown()
  84. }
  85. case <-signalChan:
  86. if first {
  87. gracefulTeardown()
  88. } else {
  89. eg.Go(func() error {
  90. return s.Kill(context.Background(), project.Name, api.KillOptions{
  91. Services: options.Create.Services,
  92. Project: project,
  93. })
  94. })
  95. return nil
  96. }
  97. }
  98. }
  99. })
  100. var exitCode int
  101. eg.Go(func() error {
  102. code, err := printer.Run(options.Start.CascadeStop, options.Start.ExitCodeFrom, func() error {
  103. fmt.Fprintln(s.stdinfo(), "Aborting on container exit...")
  104. return progress.Run(ctx, func(ctx context.Context) error {
  105. return s.Stop(ctx, project.Name, api.StopOptions{
  106. Services: options.Create.Services,
  107. Project: project,
  108. })
  109. }, s.stdinfo())
  110. })
  111. exitCode = code
  112. return err
  113. })
  114. if options.Start.Watch {
  115. eg.Go(func() error {
  116. buildOpts := *options.Create.Build
  117. buildOpts.Quiet = true
  118. return s.Watch(ctx, project, options.Start.Services, api.WatchOptions{
  119. Build: &buildOpts,
  120. LogTo: options.Start.Attach,
  121. })
  122. })
  123. }
  124. // We don't use parent (cancelable) context as we manage sigterm to stop the stack
  125. err = s.start(context.Background(), project.Name, options.Start, printer.HandleEvent)
  126. if err != nil && !isTerminated { // Ignore error if the process is terminated
  127. return err
  128. }
  129. printer.Stop()
  130. if !isTerminated {
  131. // signal for the signal-handler goroutines to stop
  132. close(doneCh)
  133. }
  134. err = eg.Wait().ErrorOrNil()
  135. if exitCode != 0 {
  136. errMsg := ""
  137. if err != nil {
  138. errMsg = err.Error()
  139. }
  140. return cli.StatusError{StatusCode: exitCode, Status: errMsg}
  141. }
  142. return err
  143. }