up.go 5.1 KB

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