up.go 4.0 KB

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