up.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/docker/compose/v2/internal/tracing"
  21. "github.com/compose-spec/compose-go/types"
  22. "github.com/docker/cli/cli"
  23. "github.com/docker/compose/v2/pkg/api"
  24. "github.com/docker/compose/v2/pkg/progress"
  25. "golang.org/x/sync/errgroup"
  26. )
  27. func (s *composeService) Up(ctx context.Context, project *types.Project, options api.UpOptions) error {
  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. printer := newLogPrinter(options.Start.Attach)
  49. signalChan := make(chan os.Signal, 1)
  50. signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
  51. stopFunc := func() error {
  52. fmt.Fprintln(s.stdinfo(), "Aborting on container exit...")
  53. ctx := context.Background()
  54. return progress.Run(ctx, func(ctx context.Context) error {
  55. go func() {
  56. <-signalChan
  57. s.Kill(ctx, project.Name, api.KillOptions{ //nolint:errcheck
  58. Services: options.Create.Services,
  59. Project: project,
  60. })
  61. }()
  62. return s.Stop(ctx, project.Name, api.StopOptions{
  63. Services: options.Create.Services,
  64. Project: project,
  65. })
  66. }, s.stdinfo())
  67. }
  68. var isTerminated bool
  69. eg, ctx := errgroup.WithContext(ctx)
  70. go func() {
  71. <-signalChan
  72. isTerminated = true
  73. printer.Cancel()
  74. fmt.Fprintln(s.stdinfo(), "Gracefully stopping... (press Ctrl+C again to force)")
  75. eg.Go(stopFunc)
  76. }()
  77. var exitCode int
  78. eg.Go(func() error {
  79. code, err := printer.Run(options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc)
  80. exitCode = code
  81. return err
  82. })
  83. err = s.start(ctx, project.Name, options.Start, printer.HandleEvent)
  84. if err != nil && !isTerminated { // Ignore error if the process is terminated
  85. return err
  86. }
  87. printer.Stop()
  88. err = eg.Wait()
  89. if exitCode != 0 {
  90. errMsg := ""
  91. if err != nil {
  92. errMsg = err.Error()
  93. }
  94. return cli.StatusError{StatusCode: exitCode, Status: errMsg}
  95. }
  96. return err
  97. }