2014-05-05 19:48:56 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2014-07-30 04:16:23 -04:00
|
|
|
"encoding/json"
|
2014-05-05 19:48:56 -04:00
|
|
|
"io"
|
2014-07-30 04:16:23 -04:00
|
|
|
"os"
|
|
|
|
"time"
|
2014-05-05 19:48:56 -04:00
|
|
|
|
2014-10-24 18:11:48 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2014-07-30 04:16:23 -04:00
|
|
|
"github.com/docker/docker/engine"
|
2014-09-09 00:19:32 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
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
|
|
|
)
|
|
|
|
|
2014-07-30 04:16:23 -04: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 16:37:44 -04:00
|
|
|
log.Debugf("Old logs format")
|
2014-07-30 04:16:23 -04:00
|
|
|
if stdout {
|
|
|
|
cLog, err := container.ReadLog("stdout")
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.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 {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.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 {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.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 {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("Error streaming logs (stderr): %s", err)
|
2014-07-30 04:16:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.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 {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.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()
|
2014-07-24 16:37:44 -04:00
|
|
|
defer log.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
|
|
|
|
}
|
|
|
|
|
2014-10-03 13:38:44 -04: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
|
|
|
}
|
|
|
|
}
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|
|
|
|
|
2014-10-03 13:38:44 -04: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
|
|
|
|
nJobs int
|
|
|
|
errors = make(chan error, 3)
|
|
|
|
)
|
|
|
|
|
2014-09-04 01:29:19 -04:00
|
|
|
// Connect stdin of container to the http conn.
|
|
|
|
if stdin != nil && openStdin {
|
2014-09-10 20:06:59 -04:00
|
|
|
nJobs++
|
2014-09-04 01:29:19 -04:00
|
|
|
// Get the stdin pipe.
|
2014-09-09 00:19:32 -04:00
|
|
|
if cStdin, err := streamConfig.StdinPipe(); err != nil {
|
2014-05-05 19:48:56 -04:00
|
|
|
errors <- err
|
|
|
|
} else {
|
|
|
|
go func() {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("attach: stdin: begin")
|
|
|
|
defer log.Debugf("attach: stdin: end")
|
2014-09-04 01:29:19 -04:00
|
|
|
if stdinOnce && !tty {
|
2014-05-05 19:48:56 -04:00
|
|
|
defer cStdin.Close()
|
|
|
|
} else {
|
2014-10-03 13:38:44 -04:00
|
|
|
// No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
|
2014-05-05 19:48:56 -04:00
|
|
|
defer func() {
|
|
|
|
if cStdout != nil {
|
|
|
|
cStdout.Close()
|
|
|
|
}
|
|
|
|
if cStderr != nil {
|
|
|
|
cStderr.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-09-04 01:29:19 -04:00
|
|
|
if tty {
|
2014-05-05 19:48:56 -04:00
|
|
|
_, err = utils.CopyEscapable(cStdin, stdin)
|
|
|
|
} else {
|
|
|
|
_, err = io.Copy(cStdin, stdin)
|
2014-09-04 01:29:19 -04:00
|
|
|
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("attach: stdin: %s", err)
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
|
|
|
errors <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if stdout != nil {
|
2014-09-10 20:06:59 -04:00
|
|
|
nJobs++
|
2014-09-04 01:29:19 -04:00
|
|
|
// Get a reader end of a pipe that is attached as stdout to the container.
|
2014-09-09 00:19:32 -04:00
|
|
|
if p, err := streamConfig.StdoutPipe(); err != nil {
|
2014-05-05 19:48:56 -04:00
|
|
|
errors <- err
|
|
|
|
} else {
|
|
|
|
cStdout = p
|
|
|
|
go func() {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("attach: stdout: begin")
|
|
|
|
defer log.Debugf("attach: stdout: end")
|
2014-05-05 19:48:56 -04:00
|
|
|
// If we are in StdinOnce mode, then close stdin
|
2014-09-04 01:29:19 -04:00
|
|
|
if stdinOnce && stdin != nil {
|
2014-05-05 19:48:56 -04:00
|
|
|
defer stdin.Close()
|
|
|
|
}
|
|
|
|
_, err := io.Copy(stdout, cStdout)
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("attach: stdout: %s", err)
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
|
|
|
errors <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-04 01:29:19 -04:00
|
|
|
// Point stdout of container to a no-op writer.
|
2014-05-05 19:48:56 -04:00
|
|
|
go func() {
|
2014-09-09 00:19:32 -04:00
|
|
|
if cStdout, err := streamConfig.StdoutPipe(); err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("attach: stdout pipe: %s", err)
|
2014-05-05 19:48:56 -04:00
|
|
|
} else {
|
2014-08-12 12:10:43 -04:00
|
|
|
io.Copy(&ioutils.NopWriter{}, cStdout)
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if stderr != nil {
|
2014-09-16 02:43:43 -04:00
|
|
|
nJobs++
|
2014-09-09 00:19:32 -04:00
|
|
|
if p, err := streamConfig.StderrPipe(); err != nil {
|
2014-05-05 19:48:56 -04:00
|
|
|
errors <- err
|
|
|
|
} else {
|
|
|
|
cStderr = p
|
|
|
|
go func() {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("attach: stderr: begin")
|
|
|
|
defer log.Debugf("attach: stderr: end")
|
2014-05-05 19:48:56 -04:00
|
|
|
// If we are in StdinOnce mode, then close stdin
|
2014-09-04 01:29:19 -04:00
|
|
|
// Why are we closing stdin here and above while handling stdout?
|
|
|
|
if stdinOnce && stdin != nil {
|
2014-05-05 19:48:56 -04:00
|
|
|
defer stdin.Close()
|
|
|
|
}
|
|
|
|
_, err := io.Copy(stderr, cStderr)
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("attach: stderr: %s", err)
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
|
|
|
errors <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-04 01:29:19 -04:00
|
|
|
// Point stderr at a no-op writer.
|
2014-05-05 19:48:56 -04:00
|
|
|
go func() {
|
2014-09-09 00:19:32 -04:00
|
|
|
if cStderr, err := streamConfig.StderrPipe(); err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("attach: stdout pipe: %s", err)
|
2014-05-05 19:48:56 -04:00
|
|
|
} else {
|
2014-08-12 12:10:43 -04:00
|
|
|
io.Copy(&ioutils.NopWriter{}, cStderr)
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-09-30 02:16:27 -04:00
|
|
|
return promise.Go(func() error {
|
2014-05-05 19:48:56 -04:00
|
|
|
defer func() {
|
|
|
|
if cStdout != nil {
|
|
|
|
cStdout.Close()
|
|
|
|
}
|
|
|
|
if cStderr != nil {
|
|
|
|
cStderr.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-09-10 20:06:59 -04:00
|
|
|
for i := 0; i < nJobs; i++ {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("attach: waiting for job %d/%d", i+1, nJobs)
|
2014-05-05 19:48:56 -04:00
|
|
|
if err := <-errors; err != nil {
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Errorf("attach: job %d returned error %s, aborting all jobs", i+1, err)
|
2014-05-05 19:48:56 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("attach: job %d completed successfully", i+1)
|
2014-05-05 19:48:56 -04:00
|
|
|
}
|
2014-07-24 16:37:44 -04:00
|
|
|
log.Debugf("attach: all jobs completed successfully")
|
2014-05-05 19:48:56 -04:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|