2014-05-06 03:48:56 +04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2015-11-03 20:33:13 +03:00
|
|
|
"time"
|
2014-05-06 03:48:56 +04:00
|
|
|
|
2015-11-03 20:33:13 +03:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-11-12 22:55:17 +03:00
|
|
|
"github.com/docker/docker/container"
|
2015-11-03 20:33:13 +03:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2015-05-05 23:25:05 +03:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2014-05-06 03:48:56 +04:00
|
|
|
)
|
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
// ContainerAttachWithLogsConfig holds the streams to use when connecting to a container to view logs.
|
2015-05-05 23:25:05 +03:00
|
|
|
type ContainerAttachWithLogsConfig struct {
|
|
|
|
InStream io.ReadCloser
|
|
|
|
OutStream io.Writer
|
|
|
|
UseStdin, UseStdout, UseStderr bool
|
|
|
|
Logs, Stream bool
|
2014-07-30 12:16:23 +04:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
// ContainerAttachWithLogs attaches to logs according to the config passed in. See ContainerAttachWithLogsConfig.
|
2015-09-29 20:51:40 +03:00
|
|
|
func (daemon *Daemon) ContainerAttachWithLogs(prefixOrName string, c *ContainerAttachWithLogsConfig) error {
|
2015-12-11 20:39:28 +03:00
|
|
|
container, err := daemon.GetContainer(prefixOrName)
|
2015-09-17 22:57:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-05 23:25:05 +03:00
|
|
|
var errStream io.Writer
|
2014-12-05 00:12:29 +03:00
|
|
|
|
2015-06-17 18:00:48 +03:00
|
|
|
if !container.Config.Tty {
|
2015-05-05 23:25:05 +03:00
|
|
|
errStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stderr)
|
|
|
|
c.OutStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stdout)
|
|
|
|
} else {
|
|
|
|
errStream = c.OutStream
|
2014-12-05 00:12:29 +03:00
|
|
|
}
|
|
|
|
|
2015-05-05 23:25:05 +03:00
|
|
|
var stdin io.ReadCloser
|
|
|
|
var stdout, stderr io.Writer
|
2015-01-06 04:30:38 +03:00
|
|
|
|
2015-05-05 23:25:05 +03:00
|
|
|
if c.UseStdin {
|
|
|
|
stdin = c.InStream
|
|
|
|
}
|
|
|
|
if c.UseStdout {
|
|
|
|
stdout = c.OutStream
|
|
|
|
}
|
|
|
|
if c.UseStderr {
|
|
|
|
stderr = errStream
|
2014-12-05 00:12:29 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 20:33:13 +03:00
|
|
|
return daemon.attachWithLogs(container, stdin, stdout, stderr, c.Logs, c.Stream)
|
2015-05-05 23:25:05 +03:00
|
|
|
}
|
2014-12-05 00:12:29 +03:00
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
// ContainerWsAttachWithLogsConfig attach with websockets, since all
|
2015-09-28 15:59:40 +03:00
|
|
|
// stream data is delegated to the websocket to handle there.
|
2015-05-05 23:25:05 +03:00
|
|
|
type ContainerWsAttachWithLogsConfig struct {
|
|
|
|
InStream io.ReadCloser
|
|
|
|
OutStream, ErrStream io.Writer
|
|
|
|
Logs, Stream bool
|
2014-05-06 03:48:56 +04:00
|
|
|
}
|
2015-03-30 00:17:23 +03:00
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
// ContainerWsAttachWithLogs websocket connection
|
2015-09-29 20:51:40 +03:00
|
|
|
func (daemon *Daemon) ContainerWsAttachWithLogs(prefixOrName string, c *ContainerWsAttachWithLogsConfig) error {
|
2015-12-11 20:39:28 +03:00
|
|
|
container, err := daemon.GetContainer(prefixOrName)
|
2015-09-17 22:57:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-03 20:33:13 +03:00
|
|
|
return daemon.attachWithLogs(container, c.InStream, c.OutStream, c.ErrStream, c.Logs, c.Stream)
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) attachWithLogs(container *container.Container, stdin io.ReadCloser, stdout, stderr io.Writer, logs, stream bool) error {
|
2015-11-03 20:33:13 +03:00
|
|
|
if logs {
|
2015-11-03 21:45:12 +03:00
|
|
|
logDriver, err := daemon.getLogger(container)
|
2015-11-03 20:33:13 +03: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
|
|
|
|
}
|
|
|
|
<-container.Attach(stdinPipe, stdout, stderr)
|
|
|
|
// 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-30 00:17:23 +03:00
|
|
|
}
|