2014-05-05 19:48:56 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2014-07-30 04:16:23 -04:00
|
|
|
"encoding/json"
|
2015-03-25 03:44:12 -04:00
|
|
|
"fmt"
|
2014-05-05 19:48:56 -04:00
|
|
|
"io"
|
2014-07-30 04:16:23 -04:00
|
|
|
"os"
|
2015-01-05 20:30:38 -05:00
|
|
|
"sync"
|
2014-07-30 04:16:23 -04:00
|
|
|
"time"
|
2014-05-05 19:48:56 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-07-30 04:16:23 -04:00
|
|
|
"github.com/docker/docker/engine"
|
2014-08-06 19:45:04 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2014-09-30 02:16:27 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2014-05-05 19:48:56 -04:00
|
|
|
)
|
|
|
|
|
2015-03-25 03:44:12 -04:00
|
|
|
func (daemon *Daemon) ContainerAttach(job *engine.Job) error {
|
2014-07-30 04:16:23 -04:00
|
|
|
if len(job.Args) != 1 {
|
2015-03-25 03:44:12 -04:00
|
|
|
return fmt.Errorf("Usage: %s CONTAINER\n", job.Name)
|
2014-07-30 04:16:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
name = job.Args[0]
|
|
|
|
logs = job.GetenvBool("logs")
|
|
|
|
stream = job.GetenvBool("stream")
|
|
|
|
stdin = job.GetenvBool("stdin")
|
|
|
|
stdout = job.GetenvBool("stdout")
|
|
|
|
stderr = job.GetenvBool("stderr")
|
|
|
|
)
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(name)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-07-30 04:16:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//logs
|
|
|
|
if logs {
|
|
|
|
cLog, err := container.ReadLog("json")
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
// Legacy logs
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Old logs format")
|
2014-07-30 04:16:23 -04:00
|
|
|
if stdout {
|
|
|
|
cLog, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error reading logs (stdout): %s", err)
|
2014-07-30 04:16:23 -04:00
|
|
|
} else if _, err := io.Copy(job.Stdout, cLog); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error streaming logs (stdout): %s", err)
|
2014-07-30 04:16:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
cLog, err := container.ReadLog("stderr")
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error reading logs (stderr): %s", err)
|
2014-07-30 04:16:23 -04:00
|
|
|
} else if _, err := io.Copy(job.Stderr, cLog); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error streaming logs (stderr): %s", err)
|
2014-07-30 04:16:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error reading logs (json): %s", err)
|
2014-07-30 04:16:23 -04:00
|
|
|
} else {
|
|
|
|
dec := json.NewDecoder(cLog)
|
|
|
|
for {
|
2014-08-06 19:45:04 -04:00
|
|
|
l := &jsonlog.JSONLog{}
|
2014-07-30 04:16:23 -04:00
|
|
|
|
|
|
|
if err := dec.Decode(l); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error streaming logs: %s", err)
|
2014-07-30 04:16:23 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if l.Stream == "stdout" && stdout {
|
2014-09-22 14:14:49 -04:00
|
|
|
io.WriteString(job.Stdout, l.Log)
|
2014-07-30 04:16:23 -04:00
|
|
|
}
|
|
|
|
if l.Stream == "stderr" && stderr {
|
2014-09-22 14:14:49 -04:00
|
|
|
io.WriteString(job.Stderr, l.Log)
|
2014-07-30 04:16:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//stream
|
|
|
|
if stream {
|
|
|
|
var (
|
|
|
|
cStdin io.ReadCloser
|
|
|
|
cStdout, cStderr io.Writer
|
|
|
|
)
|
|
|
|
|
|
|
|
if stdin {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
2015-03-26 18:22:04 -04:00
|
|
|
defer logrus.Debugf("Closing buffered stdin pipe")
|
2014-07-30 04:16:23 -04:00
|
|
|
io.Copy(w, job.Stdin)
|
|
|
|
}()
|
|
|
|
cStdin = r
|
|
|
|
}
|
|
|
|
if stdout {
|
|
|
|
cStdout = job.Stdout
|
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
cStderr = job.Stderr
|
|
|
|
}
|
|
|
|
|
2015-02-04 18:37:14 -05:00
|
|
|
<-daemon.Attach(&container.StreamConfig, container.Config.OpenStdin, container.Config.StdinOnce, container.Config.Tty, cStdin, cStdout, cStderr)
|
2014-07-30 04:16:23 -04:00
|
|
|
// If we are in stdinonce mode, wait for the process to end
|
|
|
|
// otherwise, simply return
|
|
|
|
if container.Config.StdinOnce && !container.Config.Tty {
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2014-07-30 04:16:23 -04:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-30 04:16:23 -04:00
|
|
|
}
|
|
|
|
|
2015-02-04 18:37:14 -05:00
|
|
|
func (daemon *Daemon) Attach(streamConfig *StreamConfig, openStdin, stdinOnce, tty bool, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error {
|
2014-05-05 19:48:56 -04:00
|
|
|
var (
|
|
|
|
cStdout, cStderr io.ReadCloser
|
2014-12-04 16:12:29 -05:00
|
|
|
cStdin io.WriteCloser
|
2015-01-05 20:30:38 -05:00
|
|
|
wg sync.WaitGroup
|
|
|
|
errors = make(chan error, 3)
|
2014-05-05 19:48:56 -04:00
|
|
|
)
|
|
|
|
|
2014-09-04 01:29:19 -04:00
|
|
|
if stdin != nil && openStdin {
|
2014-12-04 16:12:29 -05:00
|
|
|
cStdin = streamConfig.StdinPipe()
|
2015-01-05 20:30:38 -05:00
|
|
|
wg.Add(1)
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
2014-12-04 16:12:29 -05:00
|
|
|
|
2014-05-05 19:48:56 -04:00
|
|
|
if stdout != nil {
|
2014-12-04 16:12:29 -05:00
|
|
|
cStdout = streamConfig.StdoutPipe()
|
2015-01-05 20:30:38 -05:00
|
|
|
wg.Add(1)
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
2014-12-04 16:12:29 -05:00
|
|
|
|
2014-05-05 19:48:56 -04:00
|
|
|
if stderr != nil {
|
2014-12-04 16:12:29 -05:00
|
|
|
cStderr = streamConfig.StderrPipe()
|
2015-01-05 20:30:38 -05:00
|
|
|
wg.Add(1)
|
2014-12-04 16:12:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect stdin of container to the http conn.
|
2015-01-05 20:30:38 -05:00
|
|
|
go func() {
|
|
|
|
if stdin == nil || !openStdin {
|
|
|
|
return
|
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("attach: stdin: begin")
|
2015-01-05 20:30:38 -05:00
|
|
|
defer func() {
|
|
|
|
if stdinOnce && !tty {
|
|
|
|
cStdin.Close()
|
2014-05-05 19:48:56 -04:00
|
|
|
} else {
|
2015-01-05 20:30:38 -05: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 16:12:29 -05:00
|
|
|
}
|
2015-01-05 20:30:38 -05:00
|
|
|
wg.Done()
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("attach: stdin: end")
|
2014-05-05 19:48:56 -04:00
|
|
|
}()
|
2015-01-05 20:30:38 -05:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if tty {
|
|
|
|
_, err = utils.CopyEscapable(cStdin, stdin)
|
|
|
|
} else {
|
|
|
|
_, err = io.Copy(cStdin, stdin)
|
|
|
|
|
|
|
|
}
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("attach: stdin: %s", err)
|
2015-01-05 20:30:38 -05:00
|
|
|
errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2014-05-05 19:48:56 -04:00
|
|
|
|
2014-12-04 16:12:29 -05:00
|
|
|
attachStream := func(name string, stream io.Writer, streamPipe io.ReadCloser) {
|
|
|
|
if stream == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-05-05 19:48:56 -04:00
|
|
|
defer func() {
|
2014-12-04 16:12:29 -05:00
|
|
|
// Make sure stdin gets closed
|
2015-01-13 18:33:05 -05:00
|
|
|
if stdin != nil {
|
2014-12-04 16:12:29 -05:00
|
|
|
stdin.Close()
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
2014-12-04 16:12:29 -05:00
|
|
|
streamPipe.Close()
|
2015-01-05 20:30:38 -05:00
|
|
|
wg.Done()
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("attach: %s: end", name)
|
2014-05-05 19:48:56 -04:00
|
|
|
}()
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("attach: %s: begin", name)
|
2014-12-04 16:12:29 -05:00
|
|
|
_, err := io.Copy(stream, streamPipe)
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("attach: %s: %v", name, err)
|
2015-01-05 20:30:38 -05:00
|
|
|
errors <- err
|
2014-12-04 16:12:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go attachStream("stdout", stdout, cStdout)
|
|
|
|
go attachStream("stderr", stderr, cStderr)
|
|
|
|
|
|
|
|
return promise.Go(func() error {
|
2015-01-05 20:30:38 -05:00
|
|
|
wg.Wait()
|
|
|
|
close(errors)
|
|
|
|
for err := range errors {
|
2014-12-04 16:12:29 -05:00
|
|
|
if err != nil {
|
2014-05-05 19:48:56 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|