up.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/pkg/api"
  21. "github.com/docker/compose/v2/pkg/progress"
  22. "github.com/compose-spec/compose-go/types"
  23. "github.com/docker/cli/cli"
  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. ctx := context.Background()
  48. return progress.Run(ctx, func(ctx context.Context) error {
  49. go func() {
  50. <-signalChan
  51. s.Kill(ctx, project.Name, api.KillOptions{ // nolint:errcheck
  52. Services: options.Create.Services,
  53. })
  54. }()
  55. return s.Stop(ctx, project.Name, api.StopOptions{
  56. Services: options.Create.Services,
  57. })
  58. })
  59. }
  60. go func() {
  61. <-signalChan
  62. printer.Cancel()
  63. fmt.Println("Gracefully stopping... (press Ctrl+C again to force)")
  64. stopFunc() // nolint:errcheck
  65. }()
  66. var exitCode int
  67. eg, ctx := errgroup.WithContext(ctx)
  68. eg.Go(func() error {
  69. code, err := printer.Run(context.Background(), options.Start.CascadeStop, options.Start.ExitCodeFrom, stopFunc)
  70. exitCode = code
  71. return err
  72. })
  73. err = s.start(ctx, project.Name, options.Start, printer.HandleEvent)
  74. if err != nil {
  75. return err
  76. }
  77. err = eg.Wait()
  78. if exitCode != 0 {
  79. errMsg := ""
  80. if err != nil {
  81. errMsg = err.Error()
  82. }
  83. return cli.StatusError{StatusCode: exitCode, Status: errMsg}
  84. }
  85. return err
  86. }