attach.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package daemon
  2. import (
  3. "fmt"
  4. "io"
  5. "time"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/api/errors"
  8. "github.com/docker/docker/api/types/backend"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/container/stream"
  11. "github.com/docker/docker/daemon/logger"
  12. "github.com/docker/docker/pkg/stdcopy"
  13. "github.com/docker/docker/pkg/term"
  14. )
  15. // ContainerAttach attaches to logs according to the config passed in. See ContainerAttachConfig.
  16. func (daemon *Daemon) ContainerAttach(prefixOrName string, c *backend.ContainerAttachConfig) error {
  17. keys := []byte{}
  18. var err error
  19. if c.DetachKeys != "" {
  20. keys, err = term.ToBytes(c.DetachKeys)
  21. if err != nil {
  22. return fmt.Errorf("Invalid escape keys (%s) provided", c.DetachKeys)
  23. }
  24. }
  25. container, err := daemon.GetContainer(prefixOrName)
  26. if err != nil {
  27. return err
  28. }
  29. if container.IsPaused() {
  30. err := fmt.Errorf("Container %s is paused. Unpause the container before attach", prefixOrName)
  31. return errors.NewRequestConflictError(err)
  32. }
  33. cfg := stream.AttachConfig{
  34. UseStdin: c.UseStdin && container.Config.OpenStdin,
  35. UseStdout: c.UseStdout,
  36. UseStderr: c.UseStderr,
  37. TTY: container.Config.Tty,
  38. CloseStdin: container.Config.StdinOnce,
  39. DetachKeys: keys,
  40. }
  41. container.StreamConfig.AttachStreams(&cfg)
  42. inStream, outStream, errStream, err := c.GetStreams()
  43. if err != nil {
  44. return err
  45. }
  46. defer inStream.Close()
  47. if !container.Config.Tty && c.MuxStreams {
  48. errStream = stdcopy.NewStdWriter(errStream, stdcopy.Stderr)
  49. outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
  50. }
  51. if cfg.UseStdin {
  52. cfg.Stdin = inStream
  53. }
  54. if cfg.UseStdout {
  55. cfg.Stdout = outStream
  56. }
  57. if cfg.UseStderr {
  58. cfg.Stderr = errStream
  59. }
  60. if err := daemon.containerAttach(container, &cfg, c.Logs, c.Stream); err != nil {
  61. fmt.Fprintf(outStream, "Error attaching: %s\n", err)
  62. }
  63. return nil
  64. }
  65. // ContainerAttachRaw attaches the provided streams to the container's stdio
  66. func (daemon *Daemon) ContainerAttachRaw(prefixOrName string, stdin io.ReadCloser, stdout, stderr io.Writer, doStream bool) error {
  67. container, err := daemon.GetContainer(prefixOrName)
  68. if err != nil {
  69. return err
  70. }
  71. cfg := stream.AttachConfig{
  72. UseStdin: stdin != nil && container.Config.OpenStdin,
  73. UseStdout: stdout != nil,
  74. UseStderr: stderr != nil,
  75. TTY: container.Config.Tty,
  76. CloseStdin: container.Config.StdinOnce,
  77. }
  78. container.StreamConfig.AttachStreams(&cfg)
  79. if cfg.UseStdin {
  80. cfg.Stdin = stdin
  81. }
  82. if cfg.UseStdout {
  83. cfg.Stdout = stdout
  84. }
  85. if cfg.UseStderr {
  86. cfg.Stderr = stderr
  87. }
  88. return daemon.containerAttach(container, &cfg, false, doStream)
  89. }
  90. func (daemon *Daemon) containerAttach(c *container.Container, cfg *stream.AttachConfig, logs, doStream bool) error {
  91. if logs {
  92. logDriver, err := daemon.getLogger(c)
  93. if err != nil {
  94. return err
  95. }
  96. cLog, ok := logDriver.(logger.LogReader)
  97. if !ok {
  98. return logger.ErrReadLogsNotSupported
  99. }
  100. logs := cLog.ReadLogs(logger.ReadConfig{Tail: -1})
  101. LogLoop:
  102. for {
  103. select {
  104. case msg, ok := <-logs.Msg:
  105. if !ok {
  106. break LogLoop
  107. }
  108. if msg.Source == "stdout" && cfg.Stdout != nil {
  109. cfg.Stdout.Write(msg.Line)
  110. }
  111. if msg.Source == "stderr" && cfg.Stderr != nil {
  112. cfg.Stderr.Write(msg.Line)
  113. }
  114. case err := <-logs.Err:
  115. logrus.Errorf("Error streaming logs: %v", err)
  116. break LogLoop
  117. }
  118. }
  119. }
  120. daemon.LogContainerEvent(c, "attach")
  121. if !doStream {
  122. return nil
  123. }
  124. if cfg.Stdin != nil {
  125. r, w := io.Pipe()
  126. go func(stdin io.ReadCloser) {
  127. defer w.Close()
  128. defer logrus.Debug("Closing buffered stdin pipe")
  129. io.Copy(w, stdin)
  130. }(cfg.Stdin)
  131. cfg.Stdin = r
  132. }
  133. waitChan := make(chan struct{})
  134. if c.Config.StdinOnce && !c.Config.Tty {
  135. defer func() {
  136. <-waitChan
  137. }()
  138. go func() {
  139. c.WaitStop(-1 * time.Second)
  140. close(waitChan)
  141. }()
  142. }
  143. ctx := c.InitAttachContext()
  144. err := <-c.StreamConfig.CopyStreams(ctx, cfg)
  145. if err != nil {
  146. if _, ok := err.(stream.DetachError); ok {
  147. daemon.LogContainerEvent(c, "detach")
  148. } else {
  149. logrus.Errorf("attach failed with error: %v", err)
  150. }
  151. }
  152. return nil
  153. }