2014-05-05 23:48:56 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2014-07-30 08:16:23 +00:00
|
|
|
"encoding/json"
|
2014-05-05 23:48:56 +00:00
|
|
|
"io"
|
2014-07-30 08:16:23 +00:00
|
|
|
"os"
|
2015-01-06 01:30:38 +00:00
|
|
|
"sync"
|
2014-07-30 08:16:23 +00:00
|
|
|
"time"
|
2014-05-05 23:48:56 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-08-06 23:45:04 +00:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2014-09-30 06:16:27 +00:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2014-05-05 23:48:56 +00:00
|
|
|
)
|
|
|
|
|
2015-04-06 19:19:38 +00:00
|
|
|
func (c *Container) AttachWithLogs(stdin io.ReadCloser, stdout, stderr io.Writer, logs, stream bool) error {
|
2014-07-30 08:16:23 +00:00
|
|
|
if logs {
|
2015-04-06 19:19:38 +00:00
|
|
|
cLog, err := c.ReadLog("json")
|
2014-07-30 08:16:23 +00:00
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
// Legacy logs
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Old logs format")
|
2015-04-06 19:19:38 +00:00
|
|
|
if stdout != nil {
|
|
|
|
cLog, err := c.ReadLog("stdout")
|
2014-07-30 08:16:23 +00:00
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error reading logs (stdout): %s", err)
|
2015-04-06 19:19:38 +00:00
|
|
|
} else if _, err := io.Copy(stdout, cLog); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error streaming logs (stdout): %s", err)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-06 19:19:38 +00:00
|
|
|
if stderr != nil {
|
|
|
|
cLog, err := c.ReadLog("stderr")
|
2014-07-30 08:16:23 +00:00
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error reading logs (stderr): %s", err)
|
2015-04-06 19:19:38 +00:00
|
|
|
} else if _, err := io.Copy(stderr, cLog); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error streaming logs (stderr): %s", err)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error reading logs (json): %s", err)
|
2014-07-30 08:16:23 +00:00
|
|
|
} else {
|
|
|
|
dec := json.NewDecoder(cLog)
|
|
|
|
for {
|
2014-08-06 23:45:04 +00:00
|
|
|
l := &jsonlog.JSONLog{}
|
2014-07-30 08:16:23 +00:00
|
|
|
|
|
|
|
if err := dec.Decode(l); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error streaming logs: %s", err)
|
2014-07-30 08:16:23 +00:00
|
|
|
break
|
|
|
|
}
|
2015-04-06 19:19:38 +00:00
|
|
|
if l.Stream == "stdout" && stdout != nil {
|
|
|
|
io.WriteString(stdout, l.Log)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
2015-04-06 19:19:38 +00:00
|
|
|
if l.Stream == "stderr" && stderr != nil {
|
|
|
|
io.WriteString(stderr, l.Log)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//stream
|
|
|
|
if stream {
|
2015-04-06 19:19:38 +00:00
|
|
|
var stdinPipe io.ReadCloser
|
2015-04-09 16:58:44 +00:00
|
|
|
if stdin != nil {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
defer logrus.Debugf("Closing buffered stdin pipe")
|
|
|
|
io.Copy(w, stdin)
|
|
|
|
}()
|
|
|
|
stdinPipe = r
|
|
|
|
}
|
2015-04-06 19:19:38 +00:00
|
|
|
<-c.Attach(stdinPipe, stdout, stderr)
|
2014-07-30 08:16:23 +00:00
|
|
|
// If we are in stdinonce mode, wait for the process to end
|
|
|
|
// otherwise, simply return
|
2015-04-06 19:19:38 +00:00
|
|
|
if c.Config.StdinOnce && !c.Config.Tty {
|
|
|
|
c.WaitStop(-1 * time.Second)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 19:19:38 +00:00
|
|
|
func (c *Container) Attach(stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error {
|
|
|
|
return attach(&c.StreamConfig, c.Config.OpenStdin, c.Config.StdinOnce, c.Config.Tty, stdin, stdout, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func attach(streamConfig *StreamConfig, openStdin, stdinOnce, tty bool, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error {
|
2014-05-05 23:48:56 +00:00
|
|
|
var (
|
|
|
|
cStdout, cStderr io.ReadCloser
|
2014-12-04 21:12:29 +00:00
|
|
|
cStdin io.WriteCloser
|
2015-01-06 01:30:38 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
errors = make(chan error, 3)
|
2014-05-05 23:48:56 +00:00
|
|
|
)
|
|
|
|
|
2014-09-04 05:29:19 +00:00
|
|
|
if stdin != nil && openStdin {
|
2014-12-04 21:12:29 +00:00
|
|
|
cStdin = streamConfig.StdinPipe()
|
2015-01-06 01:30:38 +00:00
|
|
|
wg.Add(1)
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
2014-12-04 21:12:29 +00:00
|
|
|
|
2014-05-05 23:48:56 +00:00
|
|
|
if stdout != nil {
|
2014-12-04 21:12:29 +00:00
|
|
|
cStdout = streamConfig.StdoutPipe()
|
2015-01-06 01:30:38 +00:00
|
|
|
wg.Add(1)
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
2014-12-04 21:12:29 +00:00
|
|
|
|
2014-05-05 23:48:56 +00:00
|
|
|
if stderr != nil {
|
2014-12-04 21:12:29 +00:00
|
|
|
cStderr = streamConfig.StderrPipe()
|
2015-01-06 01:30:38 +00:00
|
|
|
wg.Add(1)
|
2014-12-04 21:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect stdin of container to the http conn.
|
2015-01-06 01:30:38 +00:00
|
|
|
go func() {
|
|
|
|
if stdin == nil || !openStdin {
|
|
|
|
return
|
|
|
|
}
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("attach: stdin: begin")
|
2015-01-06 01:30:38 +00:00
|
|
|
defer func() {
|
|
|
|
if stdinOnce && !tty {
|
|
|
|
cStdin.Close()
|
2014-05-05 23:48:56 +00:00
|
|
|
} else {
|
2015-01-06 01:30:38 +00:00
|
|
|
// No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
|
|
|
|
if cStdout != nil {
|
|
|
|
cStdout.Close()
|
|
|
|
}
|
|
|
|
if cStderr != nil {
|
|
|
|
cStderr.Close()
|
|
|
|
}
|
2014-12-04 21:12:29 +00:00
|
|
|
}
|
2015-01-06 01:30:38 +00:00
|
|
|
wg.Done()
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("attach: stdin: end")
|
2014-05-05 23:48:56 +00:00
|
|
|
}()
|
2015-01-06 01:30:38 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if tty {
|
2015-03-29 21:17:23 +00:00
|
|
|
_, err = copyEscapable(cStdin, stdin)
|
2015-01-06 01:30:38 +00:00
|
|
|
} else {
|
|
|
|
_, err = io.Copy(cStdin, stdin)
|
|
|
|
|
|
|
|
}
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("attach: stdin: %s", err)
|
2015-01-06 01:30:38 +00:00
|
|
|
errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2014-05-05 23:48:56 +00:00
|
|
|
|
2014-12-04 21:12:29 +00:00
|
|
|
attachStream := func(name string, stream io.Writer, streamPipe io.ReadCloser) {
|
|
|
|
if stream == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-05-05 23:48:56 +00:00
|
|
|
defer func() {
|
2014-12-04 21:12:29 +00:00
|
|
|
// Make sure stdin gets closed
|
2015-01-13 23:33:05 +00:00
|
|
|
if stdin != nil {
|
2014-12-04 21:12:29 +00:00
|
|
|
stdin.Close()
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
2014-12-04 21:12:29 +00:00
|
|
|
streamPipe.Close()
|
2015-01-06 01:30:38 +00:00
|
|
|
wg.Done()
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("attach: %s: end", name)
|
2014-05-05 23:48:56 +00:00
|
|
|
}()
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("attach: %s: begin", name)
|
2014-12-04 21:12:29 +00:00
|
|
|
_, err := io.Copy(stream, streamPipe)
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("attach: %s: %v", name, err)
|
2015-01-06 01:30:38 +00:00
|
|
|
errors <- err
|
2014-12-04 21:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go attachStream("stdout", stdout, cStdout)
|
|
|
|
go attachStream("stderr", stderr, cStderr)
|
|
|
|
|
|
|
|
return promise.Go(func() error {
|
2015-01-06 01:30:38 +00:00
|
|
|
wg.Wait()
|
|
|
|
close(errors)
|
|
|
|
for err := range errors {
|
2014-12-04 21:12:29 +00:00
|
|
|
if err != nil {
|
2014-05-05 23:48:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2015-03-29 21:17:23 +00:00
|
|
|
|
|
|
|
// Code c/c from io.Copy() modified to handle escape sequence
|
|
|
|
func copyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) {
|
|
|
|
buf := make([]byte, 32*1024)
|
|
|
|
for {
|
|
|
|
nr, er := src.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
// ---- Docker addition
|
|
|
|
// char 16 is C-p
|
|
|
|
if nr == 1 && buf[0] == 16 {
|
|
|
|
nr, er = src.Read(buf)
|
|
|
|
// char 17 is C-q
|
|
|
|
if nr == 1 && buf[0] == 17 {
|
|
|
|
if err := src.Close(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ---- End of docker
|
|
|
|
nw, ew := dst.Write(buf[0:nr])
|
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw)
|
|
|
|
}
|
|
|
|
if ew != nil {
|
|
|
|
err = ew
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
err = er
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written, err
|
|
|
|
}
|