| 32 | ) |
| 33 | |
| 34 | func (s *composeService) runHook(ctx context.Context, ctr container.Summary, service types.ServiceConfig, hook types.ServiceHook, listener api.ContainerEventListener) error { |
| 35 | wOut := utils.GetWriter(func(line string) { |
| 36 | listener(api.ContainerEvent{ |
| 37 | Type: api.HookEventLog, |
| 38 | Source: getContainerNameWithoutProject(ctr) + " ->", |
| 39 | ID: ctr.ID, |
| 40 | Service: service.Name, |
| 41 | Line: line, |
| 42 | }) |
| 43 | }) |
| 44 | defer wOut.Close() //nolint:errcheck |
| 45 | |
| 46 | detached := listener == nil |
| 47 | exec, err := s.apiClient().ExecCreate(ctx, ctr.ID, client.ExecCreateOptions{ |
| 48 | User: hook.User, |
| 49 | Privileged: hook.Privileged, |
| 50 | Env: ToMobyEnv(hook.Environment), |
| 51 | WorkingDir: hook.WorkingDir, |
| 52 | Cmd: hook.Command, |
| 53 | AttachStdout: !detached, |
| 54 | AttachStderr: !detached, |
| 55 | }) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | if detached { |
| 61 | return s.runWaitExec(ctx, exec.ID, service, listener) |
| 62 | } |
| 63 | |
| 64 | attachOptions := client.ExecAttachOptions{ |
| 65 | TTY: service.Tty, |
| 66 | } |
| 67 | if service.Tty { |
| 68 | height, width := s.stdout().GetTtySize() |
| 69 | attachOptions.ConsoleSize = client.ConsoleSize{ |
| 70 | Width: width, |
| 71 | Height: height, |
| 72 | } |
| 73 | } |
| 74 | attach, err := s.apiClient().ExecAttach(ctx, exec.ID, attachOptions) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | defer attach.Close() |
| 79 | |
| 80 | if service.Tty { |
| 81 | _, err = io.Copy(wOut, attach.Reader) |
| 82 | } else { |
| 83 | _, err = stdcopy.StdCopy(wOut, wOut, attach.Reader) |
| 84 | } |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | |
| 89 | inspected, err := s.apiClient().ExecInspect(ctx, exec.ID, client.ExecInspectOptions{}) |
| 90 | if err != nil { |
| 91 | return err |