up.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. printer := newLogPrinter(options.Start.Attach)
  44. signalChan := make(chan os.Signal, 1)
  45. signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
  46. stopFunc := func() error {
  47. fmt.Fprintln(s.stderr(), "Aborting on container exit...")
  48. ctx := context.Background()
  49. return progress.Run(ctx, func(ctx context.Context) error {
  50. go func() {
  51. <-signalChan
  52. s.Kill(ctx, project.Name, api.KillOptions{ //nolint:errcheck
  53. Services: options.Create.Services,
  54. Project: project,
  55. })
  56. }()
  57. return s.Stop(ctx, project.Name, api.StopOptions{
  58. Services: options.Create.Services,
  59. Project: project,
  60. })
  61. }, s.stderr())
  62. }
  63. var isTerminated bool
  64. eg, ctx := errgroup.WithContext(ctx)
  65. go func() {
  66. <-signalChan
  67. isTerminated = true
  68. printer.Cancel()
  69. fmt.Fprintln(s.stderr(), "Gracefully stopping... (press Ctrl+C again to force)")
  70. eg.Go(stopFunc)
  71. }()
  72. var exitCode int
  73. eg.Go(func() error {
  74. code, err := printer.Run(options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc)
  75. exitCode = code
  76. return err
  77. })
  78. err = s.start(ctx, project.Name, options.Start, printer.HandleEvent)
  79. if err != nil && !isTerminated { // Ignore error if the process is terminated
  80. return err
  81. }
  82. printer.Stop()
  83. err = eg.Wait()
  84. if exitCode != 0 {
  85. errMsg := ""
  86. if err != nil {
  87. errMsg = err.Error()
  88. }
  89. return cli.StatusError{StatusCode: exitCode, Status: errMsg}
  90. }
  91. return err
  92. }