hook.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "io"
  18. "time"
  19. "github.com/compose-spec/compose-go/v2/types"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/docker/compose/v2/pkg/utils"
  22. moby "github.com/docker/docker/api/types"
  23. containerType "github.com/docker/docker/api/types/container"
  24. "github.com/docker/docker/pkg/stdcopy"
  25. )
  26. func (s composeService) runHook(ctx context.Context, container moby.Container, service types.ServiceConfig, hook types.ServiceHook, listener api.ContainerEventListener) error {
  27. wOut := utils.GetWriter(func(line string) {
  28. listener(api.ContainerEvent{
  29. Type: api.HookEventLog,
  30. Container: getContainerNameWithoutProject(container) + " ->",
  31. ID: container.ID,
  32. Service: service.Name,
  33. Line: line,
  34. })
  35. })
  36. defer wOut.Close() //nolint:errcheck
  37. detached := listener == nil
  38. exec, err := s.apiClient().ContainerExecCreate(ctx, container.ID, containerType.ExecOptions{
  39. User: hook.User,
  40. Privileged: hook.Privileged,
  41. Env: ToMobyEnv(hook.Environment),
  42. WorkingDir: hook.WorkingDir,
  43. Cmd: hook.Command,
  44. Detach: detached,
  45. AttachStdout: !detached,
  46. AttachStderr: !detached,
  47. })
  48. if err != nil {
  49. return err
  50. }
  51. if detached {
  52. return s.runWaitExec(ctx, exec, service, listener)
  53. }
  54. height, width := s.stdout().GetTtySize()
  55. consoleSize := &[2]uint{height, width}
  56. attach, err := s.apiClient().ContainerExecAttach(ctx, exec.ID, containerType.ExecAttachOptions{
  57. Tty: service.Tty,
  58. ConsoleSize: consoleSize,
  59. })
  60. if err != nil {
  61. return err
  62. }
  63. defer attach.Close()
  64. if service.Tty {
  65. _, err = io.Copy(wOut, attach.Reader)
  66. } else {
  67. _, err = stdcopy.StdCopy(wOut, wOut, attach.Reader)
  68. }
  69. if err != nil {
  70. return err
  71. }
  72. inspected, err := s.apiClient().ContainerExecInspect(ctx, exec.ID)
  73. if err != nil {
  74. return err
  75. }
  76. if inspected.ExitCode != 0 {
  77. return fmt.Errorf("%s hook exited with status %d", service.Name, inspected.ExitCode)
  78. }
  79. return nil
  80. }
  81. func (s composeService) runWaitExec(ctx context.Context, exec moby.IDResponse, service types.ServiceConfig, listener api.ContainerEventListener) error {
  82. err := s.apiClient().ContainerExecStart(ctx, exec.ID, containerType.ExecStartOptions{
  83. Detach: listener == nil,
  84. Tty: service.Tty,
  85. })
  86. if err != nil {
  87. return nil
  88. }
  89. // We miss a ContainerExecWait API
  90. tick := time.NewTicker(100 * time.Millisecond)
  91. for {
  92. select {
  93. case <-ctx.Done():
  94. return nil
  95. case <-tick.C:
  96. inspect, err := s.apiClient().ContainerExecInspect(ctx, exec.ID)
  97. if err != nil {
  98. return nil
  99. }
  100. if !inspect.Running {
  101. if inspect.ExitCode != 0 {
  102. return fmt.Errorf("%s hook exited with status %d", service.Name, inspect.ExitCode)
  103. }
  104. return nil
  105. }
  106. }
  107. }
  108. }