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