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-12-16 01:41:46 -05:00
|
|
|
"net/http"
|
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-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
|
|
|
)
|
|
|
|
|
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 {
|
2015-12-16 01:41:46 -05:00
|
|
|
Hijacker http.Hijacker
|
|
|
|
Upgrade bool
|
|
|
|
UseStdin bool
|
|
|
|
UseStdout bool
|
|
|
|
UseStderr bool
|
|
|
|
Logs bool
|
|
|
|
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 {
|
2015-12-16 01:41:46 -05:00
|
|
|
if c.Hijacker == nil {
|
|
|
|
return derr.ErrorCodeNoHijackConnection.WithArgs(prefixOrName)
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, _, err := c.Hijacker.Hijack()
|
2015-09-17 15:57:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-16 01:41:46 -05:00
|
|
|
defer conn.Close()
|
|
|
|
// Flush the options to make sure the client sets the raw mode
|
|
|
|
conn.Write([]byte{})
|
|
|
|
inStream := conn.(io.ReadCloser)
|
|
|
|
outStream := conn.(io.Writer)
|
|
|
|
|
|
|
|
if c.Upgrade {
|
|
|
|
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
|
|
|
|
}
|
2015-09-17 15:57:57 -04:00
|
|
|
|
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-12-16 01:41:46 -05:00
|
|
|
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
|
|
|
|
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
|
2015-05-05 16:25:05 -04:00
|
|
|
} else {
|
2015-12-16 01:41:46 -05:00
|
|
|
errStream = 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 {
|
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
|
|
|
}
|
|
|
|
|
2015-12-16 01:41:46 -05:00
|
|
|
if err := daemon.attachWithLogs(container, stdin, stdout, stderr, c.Logs, c.Stream); err != nil {
|
|
|
|
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
|
|
|
|
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 {
|
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
|
|
|
|
}
|
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
|
|
|
}
|