2014-05-05 19:48:56 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-12-16 01:41:46 -05:00
|
|
|
"fmt"
|
2014-05-05 19:48:56 -04:00
|
|
|
"io"
|
2015-11-03 12:33:13 -05:00
|
|
|
"time"
|
2014-05-05 19:48:56 -04:00
|
|
|
|
2015-11-03 12:33:13 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-01-27 17:09:42 -05:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2015-11-03 12:33:13 -05:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2015-12-16 01:41:46 -05:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-05-05 16:25:05 -04:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2014-05-05 19:48:56 -04:00
|
|
|
)
|
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
// ContainerAttach attaches to logs according to the config passed in. See ContainerAttachConfig.
|
|
|
|
func (daemon *Daemon) ContainerAttach(prefixOrName string, c *backend.ContainerAttachConfig) error {
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := daemon.GetContainer(prefixOrName)
|
2015-12-16 01:41:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return derr.ErrorCodeNoSuchContainer.WithArgs(prefixOrName)
|
|
|
|
}
|
|
|
|
if container.IsPaused() {
|
|
|
|
return derr.ErrorCodePausedContainer.WithArgs(prefixOrName)
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
inStream, outStream, errStream, err := c.GetStreams()
|
2015-09-17 15:57:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-05 16:23:24 -05:00
|
|
|
defer inStream.Close()
|
2015-09-17 15:57:57 -04:00
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
if !container.Config.Tty && c.MuxStreams {
|
|
|
|
errStream = stdcopy.NewStdWriter(errStream, stdcopy.Stderr)
|
2015-12-16 01:41:46 -05:00
|
|
|
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
|
2014-12-04 16:12:29 -05:00
|
|
|
}
|
|
|
|
|
2015-05-05 16:25:05 -04:00
|
|
|
var stdin io.ReadCloser
|
|
|
|
var stdout, stderr io.Writer
|
2015-01-05 20:30:38 -05:00
|
|
|
|
2015-05-05 16:25:05 -04:00
|
|
|
if c.UseStdin {
|
2015-12-16 01:41:46 -05:00
|
|
|
stdin = inStream
|
2015-05-05 16:25:05 -04:00
|
|
|
}
|
|
|
|
if c.UseStdout {
|
2015-12-16 01:41:46 -05:00
|
|
|
stdout = outStream
|
2015-05-05 16:25:05 -04:00
|
|
|
}
|
|
|
|
if c.UseStderr {
|
|
|
|
stderr = errStream
|
2014-12-04 16:12:29 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
if err := daemon.containerAttach(container, stdin, stdout, stderr, c.Logs, c.Stream, c.DetachKeys); err != nil {
|
2015-12-16 01:41:46 -05:00
|
|
|
fmt.Fprintf(outStream, "Error attaching: %s\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
2015-05-05 16:25:05 -04:00
|
|
|
}
|
2014-12-04 16:12:29 -05:00
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
// ContainerAttachRaw attaches the provided streams to the container's stdio
|
|
|
|
func (daemon *Daemon) ContainerAttachRaw(prefixOrName string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool) error {
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := daemon.GetContainer(prefixOrName)
|
2015-09-17 15:57:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-05 16:23:24 -05:00
|
|
|
return daemon.containerAttach(container, stdin, stdout, stderr, false, stream, nil)
|
2016-01-20 18:32:02 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 16:23:24 -05:00
|
|
|
func (daemon *Daemon) containerAttach(container *container.Container, stdin io.ReadCloser, stdout, stderr io.Writer, logs, stream bool, keys []byte) error {
|
2015-11-03 12:33:13 -05:00
|
|
|
if logs {
|
2015-11-03 13:45:12 -05:00
|
|
|
logDriver, err := daemon.getLogger(container)
|
2015-11-03 12:33:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cLog, ok := logDriver.(logger.LogReader)
|
|
|
|
if !ok {
|
|
|
|
return logger.ErrReadLogsNotSupported
|
|
|
|
}
|
|
|
|
logs := cLog.ReadLogs(logger.ReadConfig{Tail: -1})
|
|
|
|
|
|
|
|
LogLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg, ok := <-logs.Msg:
|
|
|
|
if !ok {
|
|
|
|
break LogLoop
|
|
|
|
}
|
|
|
|
if msg.Source == "stdout" && stdout != nil {
|
|
|
|
stdout.Write(msg.Line)
|
|
|
|
}
|
|
|
|
if msg.Source == "stderr" && stderr != nil {
|
|
|
|
stderr.Write(msg.Line)
|
|
|
|
}
|
|
|
|
case err := <-logs.Err:
|
|
|
|
logrus.Errorf("Error streaming logs: %v", err)
|
|
|
|
break LogLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon.LogContainerEvent(container, "attach")
|
|
|
|
|
|
|
|
//stream
|
|
|
|
if stream {
|
|
|
|
var stdinPipe io.ReadCloser
|
|
|
|
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
|
|
|
|
}
|
2016-01-03 17:03:39 -05:00
|
|
|
<-container.Attach(stdinPipe, stdout, stderr, keys)
|
2015-11-03 12:33:13 -05:00
|
|
|
// If we are in stdinonce mode, wait for the process to end
|
|
|
|
// otherwise, simply return
|
|
|
|
if container.Config.StdinOnce && !container.Config.Tty {
|
|
|
|
container.WaitStop(-1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2015-03-29 17:17:23 -04:00
|
|
|
}
|