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"
|
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
|
|
|
|
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-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
|
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
|
|
|
|
}
|
|
|
|
log.Debugf("attach: stdin: begin")
|
|
|
|
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()
|
|
|
|
log.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 {
|
|
|
|
log.Errorf("attach: stdin: %s", err)
|
|
|
|
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
|
|
|
|
if stdinOnce && cStdin != nil {
|
|
|
|
stdin.Close()
|
|
|
|
cStdin.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()
|
|
|
|
log.Debugf("attach: %s: end", name)
|
2014-05-05 19:48:56 -04:00
|
|
|
}()
|
|
|
|
|
2014-12-04 16:12:29 -05:00
|
|
|
log.Debugf("attach: %s: begin", name)
|
|
|
|
_, err := io.Copy(stream, streamPipe)
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.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
|
|
|
|
})
|
|
|
|
}
|