attach.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package daemon
  2. import (
  3. "encoding/json"
  4. "io"
  5. "os"
  6. "time"
  7. log "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/engine"
  9. "github.com/docker/docker/pkg/ioutils"
  10. "github.com/docker/docker/pkg/jsonlog"
  11. "github.com/docker/docker/pkg/promise"
  12. "github.com/docker/docker/utils"
  13. )
  14. func (daemon *Daemon) ContainerAttach(job *engine.Job) engine.Status {
  15. if len(job.Args) != 1 {
  16. return job.Errorf("Usage: %s CONTAINER\n", job.Name)
  17. }
  18. var (
  19. name = job.Args[0]
  20. logs = job.GetenvBool("logs")
  21. stream = job.GetenvBool("stream")
  22. stdin = job.GetenvBool("stdin")
  23. stdout = job.GetenvBool("stdout")
  24. stderr = job.GetenvBool("stderr")
  25. )
  26. container := daemon.Get(name)
  27. if container == nil {
  28. return job.Errorf("No such container: %s", name)
  29. }
  30. //logs
  31. if logs {
  32. cLog, err := container.ReadLog("json")
  33. if err != nil && os.IsNotExist(err) {
  34. // Legacy logs
  35. log.Debugf("Old logs format")
  36. if stdout {
  37. cLog, err := container.ReadLog("stdout")
  38. if err != nil {
  39. log.Errorf("Error reading logs (stdout): %s", err)
  40. } else if _, err := io.Copy(job.Stdout, cLog); err != nil {
  41. log.Errorf("Error streaming logs (stdout): %s", err)
  42. }
  43. }
  44. if stderr {
  45. cLog, err := container.ReadLog("stderr")
  46. if err != nil {
  47. log.Errorf("Error reading logs (stderr): %s", err)
  48. } else if _, err := io.Copy(job.Stderr, cLog); err != nil {
  49. log.Errorf("Error streaming logs (stderr): %s", err)
  50. }
  51. }
  52. } else if err != nil {
  53. log.Errorf("Error reading logs (json): %s", err)
  54. } else {
  55. dec := json.NewDecoder(cLog)
  56. for {
  57. l := &jsonlog.JSONLog{}
  58. if err := dec.Decode(l); err == io.EOF {
  59. break
  60. } else if err != nil {
  61. log.Errorf("Error streaming logs: %s", err)
  62. break
  63. }
  64. if l.Stream == "stdout" && stdout {
  65. io.WriteString(job.Stdout, l.Log)
  66. }
  67. if l.Stream == "stderr" && stderr {
  68. io.WriteString(job.Stderr, l.Log)
  69. }
  70. }
  71. }
  72. }
  73. //stream
  74. if stream {
  75. var (
  76. cStdin io.ReadCloser
  77. cStdout, cStderr io.Writer
  78. )
  79. if stdin {
  80. r, w := io.Pipe()
  81. go func() {
  82. defer w.Close()
  83. defer log.Debugf("Closing buffered stdin pipe")
  84. io.Copy(w, job.Stdin)
  85. }()
  86. cStdin = r
  87. }
  88. if stdout {
  89. cStdout = job.Stdout
  90. }
  91. if stderr {
  92. cStderr = job.Stderr
  93. }
  94. <-daemon.attach(&container.StreamConfig, container.Config.OpenStdin, container.Config.StdinOnce, container.Config.Tty, cStdin, cStdout, cStderr)
  95. // If we are in stdinonce mode, wait for the process to end
  96. // otherwise, simply return
  97. if container.Config.StdinOnce && !container.Config.Tty {
  98. container.WaitStop(-1 * time.Second)
  99. }
  100. }
  101. return engine.StatusOK
  102. }
  103. func (daemon *Daemon) attach(streamConfig *StreamConfig, openStdin, stdinOnce, tty bool, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error {
  104. var (
  105. cStdout, cStderr io.ReadCloser
  106. nJobs int
  107. errors = make(chan error, 3)
  108. )
  109. // Connect stdin of container to the http conn.
  110. if stdin != nil && openStdin {
  111. nJobs++
  112. // Get the stdin pipe.
  113. if cStdin, err := streamConfig.StdinPipe(); err != nil {
  114. errors <- err
  115. } else {
  116. go func() {
  117. log.Debugf("attach: stdin: begin")
  118. defer log.Debugf("attach: stdin: end")
  119. if stdinOnce && !tty {
  120. defer cStdin.Close()
  121. } else {
  122. // No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
  123. defer func() {
  124. if cStdout != nil {
  125. cStdout.Close()
  126. }
  127. if cStderr != nil {
  128. cStderr.Close()
  129. }
  130. }()
  131. }
  132. if tty {
  133. _, err = utils.CopyEscapable(cStdin, stdin)
  134. } else {
  135. _, err = io.Copy(cStdin, stdin)
  136. }
  137. if err == io.ErrClosedPipe {
  138. err = nil
  139. }
  140. if err != nil {
  141. log.Errorf("attach: stdin: %s", err)
  142. }
  143. errors <- err
  144. }()
  145. }
  146. }
  147. if stdout != nil {
  148. nJobs++
  149. // Get a reader end of a pipe that is attached as stdout to the container.
  150. if p, err := streamConfig.StdoutPipe(); err != nil {
  151. errors <- err
  152. } else {
  153. cStdout = p
  154. go func() {
  155. log.Debugf("attach: stdout: begin")
  156. defer log.Debugf("attach: stdout: end")
  157. // If we are in StdinOnce mode, then close stdin
  158. if stdinOnce && stdin != nil {
  159. defer stdin.Close()
  160. }
  161. _, err := io.Copy(stdout, cStdout)
  162. if err == io.ErrClosedPipe {
  163. err = nil
  164. }
  165. if err != nil {
  166. log.Errorf("attach: stdout: %s", err)
  167. }
  168. errors <- err
  169. }()
  170. }
  171. } else {
  172. // Point stdout of container to a no-op writer.
  173. go func() {
  174. if cStdout, err := streamConfig.StdoutPipe(); err != nil {
  175. log.Errorf("attach: stdout pipe: %s", err)
  176. } else {
  177. io.Copy(&ioutils.NopWriter{}, cStdout)
  178. }
  179. }()
  180. }
  181. if stderr != nil {
  182. nJobs++
  183. if p, err := streamConfig.StderrPipe(); err != nil {
  184. errors <- err
  185. } else {
  186. cStderr = p
  187. go func() {
  188. log.Debugf("attach: stderr: begin")
  189. defer log.Debugf("attach: stderr: end")
  190. // If we are in StdinOnce mode, then close stdin
  191. // Why are we closing stdin here and above while handling stdout?
  192. if stdinOnce && stdin != nil {
  193. defer stdin.Close()
  194. }
  195. _, err := io.Copy(stderr, cStderr)
  196. if err == io.ErrClosedPipe {
  197. err = nil
  198. }
  199. if err != nil {
  200. log.Errorf("attach: stderr: %s", err)
  201. }
  202. errors <- err
  203. }()
  204. }
  205. } else {
  206. // Point stderr at a no-op writer.
  207. go func() {
  208. if cStderr, err := streamConfig.StderrPipe(); err != nil {
  209. log.Errorf("attach: stdout pipe: %s", err)
  210. } else {
  211. io.Copy(&ioutils.NopWriter{}, cStderr)
  212. }
  213. }()
  214. }
  215. return promise.Go(func() error {
  216. defer func() {
  217. if cStdout != nil {
  218. cStdout.Close()
  219. }
  220. if cStderr != nil {
  221. cStderr.Close()
  222. }
  223. }()
  224. for i := 0; i < nJobs; i++ {
  225. log.Debugf("attach: waiting for job %d/%d", i+1, nJobs)
  226. if err := <-errors; err != nil {
  227. log.Errorf("attach: job %d returned error %s, aborting all jobs", i+1, err)
  228. return err
  229. }
  230. log.Debugf("attach: job %d completed successfully", i+1)
  231. }
  232. log.Debugf("attach: all jobs completed successfully")
  233. return nil
  234. })
  235. }