up.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. })
  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. })
  62. }
  63. go func() {
  64. <-signalChan
  65. printer.Cancel()
  66. fmt.Fprintln(s.stderr(), "Gracefully stopping... (press Ctrl+C again to force)")
  67. stopFunc() //nolint:errcheck
  68. }()
  69. var exitCode int
  70. eg, ctx := errgroup.WithContext(ctx)
  71. eg.Go(func() error {
  72. code, err := printer.Run(options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc)
  73. exitCode = code
  74. return err
  75. })
  76. err = s.start(ctx, project.Name, options.Start, printer.HandleEvent)
  77. if err != nil {
  78. return err
  79. }
  80. printer.Stop()
  81. err = eg.Wait()
  82. if exitCode != 0 {
  83. errMsg := ""
  84. if err != nil {
  85. errMsg = err.Error()
  86. }
  87. return cli.StatusError{StatusCode: exitCode, Status: errMsg}
  88. }
  89. return err
  90. }