hook.go 3.2 KB

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