attach.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package daemon
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "time"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/container"
  9. "github.com/docker/docker/daemon/logger"
  10. derr "github.com/docker/docker/errors"
  11. "github.com/docker/docker/pkg/stdcopy"
  12. )
  13. // ContainerAttachWithLogsConfig holds the streams to use when connecting to a container to view logs.
  14. type ContainerAttachWithLogsConfig struct {
  15. Hijacker http.Hijacker
  16. Upgrade bool
  17. UseStdin bool
  18. UseStdout bool
  19. UseStderr bool
  20. Logs bool
  21. Stream bool
  22. DetachKeys []byte
  23. }
  24. // ContainerAttachWithLogs attaches to logs according to the config passed in. See ContainerAttachWithLogsConfig.
  25. func (daemon *Daemon) ContainerAttachWithLogs(prefixOrName string, c *ContainerAttachWithLogsConfig) error {
  26. if c.Hijacker == nil {
  27. return derr.ErrorCodeNoHijackConnection.WithArgs(prefixOrName)
  28. }
  29. container, err := daemon.GetContainer(prefixOrName)
  30. if err != nil {
  31. return derr.ErrorCodeNoSuchContainer.WithArgs(prefixOrName)
  32. }
  33. if container.IsPaused() {
  34. return derr.ErrorCodePausedContainer.WithArgs(prefixOrName)
  35. }
  36. conn, _, err := c.Hijacker.Hijack()
  37. if err != nil {
  38. return err
  39. }
  40. defer conn.Close()
  41. // Flush the options to make sure the client sets the raw mode
  42. conn.Write([]byte{})
  43. inStream := conn.(io.ReadCloser)
  44. outStream := conn.(io.Writer)
  45. if c.Upgrade {
  46. fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
  47. } else {
  48. fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
  49. }
  50. var errStream io.Writer
  51. if !container.Config.Tty {
  52. errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
  53. outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
  54. } else {
  55. errStream = outStream
  56. }
  57. var stdin io.ReadCloser
  58. var stdout, stderr io.Writer
  59. if c.UseStdin {
  60. stdin = inStream
  61. }
  62. if c.UseStdout {
  63. stdout = outStream
  64. }
  65. if c.UseStderr {
  66. stderr = errStream
  67. }
  68. if err := daemon.attachWithLogs(container, stdin, stdout, stderr, c.Logs, c.Stream, c.DetachKeys); err != nil {
  69. fmt.Fprintf(outStream, "Error attaching: %s\n", err)
  70. }
  71. return nil
  72. }
  73. // ContainerWsAttachWithLogsConfig attach with websockets, since all
  74. // stream data is delegated to the websocket to handle there.
  75. type ContainerWsAttachWithLogsConfig struct {
  76. InStream io.ReadCloser
  77. OutStream, ErrStream io.Writer
  78. Logs, Stream bool
  79. DetachKeys []byte
  80. }
  81. // ContainerWsAttachWithLogs websocket connection
  82. func (daemon *Daemon) ContainerWsAttachWithLogs(prefixOrName string, c *ContainerWsAttachWithLogsConfig) error {
  83. container, err := daemon.GetContainer(prefixOrName)
  84. if err != nil {
  85. return err
  86. }
  87. return daemon.attachWithLogs(container, c.InStream, c.OutStream, c.ErrStream, c.Logs, c.Stream, c.DetachKeys)
  88. }
  89. func (daemon *Daemon) attachWithLogs(container *container.Container, stdin io.ReadCloser, stdout, stderr io.Writer, logs, stream bool, keys []byte) error {
  90. if logs {
  91. logDriver, err := daemon.getLogger(container)
  92. if err != nil {
  93. return err
  94. }
  95. cLog, ok := logDriver.(logger.LogReader)
  96. if !ok {
  97. return logger.ErrReadLogsNotSupported
  98. }
  99. logs := cLog.ReadLogs(logger.ReadConfig{Tail: -1})
  100. LogLoop:
  101. for {
  102. select {
  103. case msg, ok := <-logs.Msg:
  104. if !ok {
  105. break LogLoop
  106. }
  107. if msg.Source == "stdout" && stdout != nil {
  108. stdout.Write(msg.Line)
  109. }
  110. if msg.Source == "stderr" && stderr != nil {
  111. stderr.Write(msg.Line)
  112. }
  113. case err := <-logs.Err:
  114. logrus.Errorf("Error streaming logs: %v", err)
  115. break LogLoop
  116. }
  117. }
  118. }
  119. daemon.LogContainerEvent(container, "attach")
  120. //stream
  121. if stream {
  122. var stdinPipe io.ReadCloser
  123. if stdin != nil {
  124. r, w := io.Pipe()
  125. go func() {
  126. defer w.Close()
  127. defer logrus.Debugf("Closing buffered stdin pipe")
  128. io.Copy(w, stdin)
  129. }()
  130. stdinPipe = r
  131. }
  132. <-container.Attach(stdinPipe, stdout, stderr, keys)
  133. // If we are in stdinonce mode, wait for the process to end
  134. // otherwise, simply return
  135. if container.Config.StdinOnce && !container.Config.Tty {
  136. container.WaitStop(-1 * time.Second)
  137. }
  138. }
  139. return nil
  140. }