up.go 3.9 KB

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