up.go 5.1 KB

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