hook.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. Source: 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. AttachStdout: !detached,
  44. AttachStderr: !detached,
  45. })
  46. if err != nil {
  47. return err
  48. }
  49. if detached {
  50. return s.runWaitExec(ctx, exec.ID, service, listener)
  51. }
  52. height, width := s.stdout().GetTtySize()
  53. consoleSize := &[2]uint{height, width}
  54. attach, err := s.apiClient().ContainerExecAttach(ctx, exec.ID, container.ExecAttachOptions{
  55. Tty: service.Tty,
  56. ConsoleSize: consoleSize,
  57. })
  58. if err != nil {
  59. return err
  60. }
  61. defer attach.Close()
  62. if service.Tty {
  63. _, err = io.Copy(wOut, attach.Reader)
  64. } else {
  65. _, err = stdcopy.StdCopy(wOut, wOut, attach.Reader)
  66. }
  67. if err != nil {
  68. return err
  69. }
  70. inspected, err := s.apiClient().ContainerExecInspect(ctx, exec.ID)
  71. if err != nil {
  72. return err
  73. }
  74. if inspected.ExitCode != 0 {
  75. return fmt.Errorf("%s hook exited with status %d", service.Name, inspected.ExitCode)
  76. }
  77. return nil
  78. }
  79. func (s composeService) runWaitExec(ctx context.Context, execID string, service types.ServiceConfig, listener api.ContainerEventListener) error {
  80. err := s.apiClient().ContainerExecStart(ctx, execID, container.ExecStartOptions{
  81. Detach: listener == nil,
  82. Tty: service.Tty,
  83. })
  84. if err != nil {
  85. return nil
  86. }
  87. // We miss a ContainerExecWait API
  88. tick := time.NewTicker(100 * time.Millisecond)
  89. for {
  90. select {
  91. case <-ctx.Done():
  92. return nil
  93. case <-tick.C:
  94. inspect, err := s.apiClient().ContainerExecInspect(ctx, execID)
  95. if err != nil {
  96. return nil
  97. }
  98. if !inspect.Running {
  99. if inspect.ExitCode != 0 {
  100. return fmt.Errorf("%s hook exited with status %d", service.Name, inspect.ExitCode)
  101. }
  102. return nil
  103. }
  104. }
  105. }
  106. }