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