up.go 2.8 KB

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