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"
|
|
|
|
"time"
|
2014-05-05 23:48:56 +00:00
|
|
|
|
2014-07-30 08:16:23 +00:00
|
|
|
"github.com/docker/docker/engine"
|
2014-09-09 04:19:32 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2014-08-06 23:45:04 +00:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2014-10-24 17:12:35 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-09-30 06:16:27 +00:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/utils"
|
2014-05-05 23:48:56 +00:00
|
|
|
)
|
|
|
|
|
2014-07-30 08:16:23 +00:00
|
|
|
func (daemon *Daemon) ContainerAttach(job *engine.Job) engine.Status {
|
|
|
|
if len(job.Args) != 1 {
|
|
|
|
return job.Errorf("Usage: %s CONTAINER\n", job.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
)
|
|
|
|
|
|
|
|
container := daemon.Get(name)
|
|
|
|
if container == nil {
|
|
|
|
return job.Errorf("No such container: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
//logs
|
|
|
|
if logs {
|
|
|
|
cLog, err := container.ReadLog("json")
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
// Legacy logs
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Debugf("Old logs format")
|
2014-07-30 08:16:23 +00:00
|
|
|
if stdout {
|
|
|
|
cLog, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("Error reading logs (stdout): %s", err)
|
2014-07-30 08:16:23 +00:00
|
|
|
} else if _, err := io.Copy(job.Stdout, cLog); err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("Error streaming logs (stdout): %s", err)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
cLog, err := container.ReadLog("stderr")
|
|
|
|
if err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("Error reading logs (stderr): %s", err)
|
2014-07-30 08:16:23 +00:00
|
|
|
} else if _, err := io.Copy(job.Stderr, cLog); err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("Error streaming logs (stderr): %s", err)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.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 {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("Error streaming logs: %s", err)
|
2014-07-30 08:16:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if l.Stream == "stdout" && stdout {
|
2014-09-22 18:14:49 +00:00
|
|
|
io.WriteString(job.Stdout, l.Log)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
if l.Stream == "stderr" && stderr {
|
2014-09-22 18:14:49 +00:00
|
|
|
io.WriteString(job.Stderr, l.Log)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//stream
|
|
|
|
if stream {
|
|
|
|
var (
|
|
|
|
cStdin io.ReadCloser
|
|
|
|
cStdout, cStderr io.Writer
|
|
|
|
cStdinCloser io.Closer
|
|
|
|
)
|
|
|
|
|
|
|
|
if stdin {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
2014-07-24 20:37:44 +00:00
|
|
|
defer log.Debugf("Closing buffered stdin pipe")
|
2014-07-30 08:16:23 +00:00
|
|
|
io.Copy(w, job.Stdin)
|
|
|
|
}()
|
|
|
|
cStdin = r
|
|
|
|
cStdinCloser = job.Stdin
|
|
|
|
}
|
|
|
|
if stdout {
|
|
|
|
cStdout = job.Stdout
|
|
|
|
}
|
|
|
|
if stderr {
|
|
|
|
cStderr = job.Stderr
|
|
|
|
}
|
|
|
|
|
2014-10-06 18:57:18 +00:00
|
|
|
<-daemon.attach(&container.StreamConfig, container.Config.OpenStdin, container.Config.StdinOnce, container.Config.Tty, cStdin, cStdinCloser, cStdout, cStderr)
|
2014-07-30 08:16:23 +00: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 15:20:35 +00:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2014-07-30 08:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|
|
|
|
|
2014-10-06 18:57:18 +00:00
|
|
|
func (daemon *Daemon) attach(streamConfig *StreamConfig, openStdin, stdinOnce, tty bool, stdin io.ReadCloser, stdinCloser io.Closer, stdout io.Writer, stderr io.Writer) chan error {
|
2014-05-05 23:48:56 +00:00
|
|
|
var (
|
|
|
|
cStdout, cStderr io.ReadCloser
|
|
|
|
nJobs int
|
|
|
|
errors = make(chan error, 3)
|
|
|
|
)
|
|
|
|
|
2014-09-04 05:29:19 +00:00
|
|
|
// Connect stdin of container to the http conn.
|
|
|
|
if stdin != nil && openStdin {
|
2014-09-11 00:06:59 +00:00
|
|
|
nJobs++
|
2014-09-04 05:29:19 +00:00
|
|
|
// Get the stdin pipe.
|
2014-09-09 04:19:32 +00:00
|
|
|
if cStdin, err := streamConfig.StdinPipe(); err != nil {
|
2014-05-05 23:48:56 +00:00
|
|
|
errors <- err
|
|
|
|
} else {
|
|
|
|
go func() {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Debugf("attach: stdin: begin")
|
|
|
|
defer log.Debugf("attach: stdin: end")
|
2014-05-05 23:48:56 +00:00
|
|
|
// No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
|
2014-09-04 05:29:19 +00:00
|
|
|
if stdinOnce && !tty {
|
2014-05-05 23:48:56 +00:00
|
|
|
defer cStdin.Close()
|
|
|
|
} else {
|
|
|
|
defer func() {
|
|
|
|
if cStdout != nil {
|
|
|
|
cStdout.Close()
|
|
|
|
}
|
|
|
|
if cStderr != nil {
|
|
|
|
cStderr.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-09-04 05:29:19 +00:00
|
|
|
if tty {
|
2014-05-05 23:48:56 +00:00
|
|
|
_, err = utils.CopyEscapable(cStdin, stdin)
|
|
|
|
} else {
|
|
|
|
_, err = io.Copy(cStdin, stdin)
|
2014-09-04 05:29:19 +00:00
|
|
|
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("attach: stdin: %s", err)
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
|
|
|
errors <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if stdout != nil {
|
2014-09-11 00:06:59 +00:00
|
|
|
nJobs++
|
2014-09-04 05:29:19 +00:00
|
|
|
// Get a reader end of a pipe that is attached as stdout to the container.
|
2014-09-09 04:19:32 +00:00
|
|
|
if p, err := streamConfig.StdoutPipe(); err != nil {
|
2014-05-05 23:48:56 +00:00
|
|
|
errors <- err
|
|
|
|
} else {
|
|
|
|
cStdout = p
|
|
|
|
go func() {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Debugf("attach: stdout: begin")
|
|
|
|
defer log.Debugf("attach: stdout: end")
|
2014-05-05 23:48:56 +00:00
|
|
|
// If we are in StdinOnce mode, then close stdin
|
2014-09-04 05:29:19 +00:00
|
|
|
if stdinOnce && stdin != nil {
|
2014-05-05 23:48:56 +00:00
|
|
|
defer stdin.Close()
|
|
|
|
}
|
|
|
|
if stdinCloser != nil {
|
|
|
|
defer stdinCloser.Close()
|
|
|
|
}
|
|
|
|
_, err := io.Copy(stdout, cStdout)
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("attach: stdout: %s", err)
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
|
|
|
errors <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-04 05:29:19 +00:00
|
|
|
// Point stdout of container to a no-op writer.
|
2014-05-05 23:48:56 +00:00
|
|
|
go func() {
|
|
|
|
if stdinCloser != nil {
|
|
|
|
defer stdinCloser.Close()
|
|
|
|
}
|
2014-09-09 04:19:32 +00:00
|
|
|
if cStdout, err := streamConfig.StdoutPipe(); err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("attach: stdout pipe: %s", err)
|
2014-05-05 23:48:56 +00:00
|
|
|
} else {
|
2014-08-12 16:10:43 +00:00
|
|
|
io.Copy(&ioutils.NopWriter{}, cStdout)
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if stderr != nil {
|
2014-09-16 06:43:43 +00:00
|
|
|
nJobs++
|
2014-09-09 04:19:32 +00:00
|
|
|
if p, err := streamConfig.StderrPipe(); err != nil {
|
2014-05-05 23:48:56 +00:00
|
|
|
errors <- err
|
|
|
|
} else {
|
|
|
|
cStderr = p
|
|
|
|
go func() {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Debugf("attach: stderr: begin")
|
|
|
|
defer log.Debugf("attach: stderr: end")
|
2014-05-05 23:48:56 +00:00
|
|
|
// If we are in StdinOnce mode, then close stdin
|
2014-09-04 05:29:19 +00:00
|
|
|
// Why are we closing stdin here and above while handling stdout?
|
|
|
|
if stdinOnce && stdin != nil {
|
2014-05-05 23:48:56 +00:00
|
|
|
defer stdin.Close()
|
|
|
|
}
|
|
|
|
if stdinCloser != nil {
|
|
|
|
defer stdinCloser.Close()
|
|
|
|
}
|
|
|
|
_, err := io.Copy(stderr, cStderr)
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("attach: stderr: %s", err)
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
|
|
|
errors <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-04 05:29:19 +00:00
|
|
|
// Point stderr at a no-op writer.
|
2014-05-05 23:48:56 +00:00
|
|
|
go func() {
|
|
|
|
if stdinCloser != nil {
|
|
|
|
defer stdinCloser.Close()
|
|
|
|
}
|
|
|
|
|
2014-09-09 04:19:32 +00:00
|
|
|
if cStderr, err := streamConfig.StderrPipe(); err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("attach: stdout pipe: %s", err)
|
2014-05-05 23:48:56 +00:00
|
|
|
} else {
|
2014-08-12 16:10:43 +00:00
|
|
|
io.Copy(&ioutils.NopWriter{}, cStderr)
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-09-30 06:16:27 +00:00
|
|
|
return promise.Go(func() error {
|
2014-05-05 23:48:56 +00:00
|
|
|
defer func() {
|
|
|
|
if cStdout != nil {
|
|
|
|
cStdout.Close()
|
|
|
|
}
|
|
|
|
if cStderr != nil {
|
|
|
|
cStderr.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// FIXME: how to clean up the stdin goroutine without the unwanted side effect
|
|
|
|
// of closing the passed stdin? Add an intermediary io.Pipe?
|
2014-09-11 00:06:59 +00:00
|
|
|
for i := 0; i < nJobs; i++ {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Debugf("attach: waiting for job %d/%d", i+1, nJobs)
|
2014-05-05 23:48:56 +00:00
|
|
|
if err := <-errors; err != nil {
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Errorf("attach: job %d returned error %s, aborting all jobs", i+1, err)
|
2014-05-05 23:48:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Debugf("attach: job %d completed successfully", i+1)
|
2014-05-05 23:48:56 +00:00
|
|
|
}
|
2014-07-24 20:37:44 +00:00
|
|
|
log.Debugf("attach: all jobs completed successfully")
|
2014-05-05 23:48:56 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|