2014-05-05 23:48:56 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2015-09-10 22:01:18 +00:00
|
|
|
"github.com/docker/docker/context"
|
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 {
|
|
|
|
InStream io.ReadCloser
|
|
|
|
OutStream io.Writer
|
|
|
|
UseStdin, UseStdout, UseStderr bool
|
|
|
|
Logs, Stream bool
|
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-10 22:01:18 +00:00
|
|
|
func (daemon *Daemon) ContainerAttachWithLogs(ctx context.Context, prefixOrName string, c *ContainerAttachWithLogsConfig) error {
|
|
|
|
container, err := daemon.Get(ctx, prefixOrName)
|
2015-09-17 19:57:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-05-05 20:25:05 +00:00
|
|
|
errStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stderr)
|
|
|
|
c.OutStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stdout)
|
|
|
|
} else {
|
|
|
|
errStream = c.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 {
|
|
|
|
stdin = c.InStream
|
|
|
|
}
|
|
|
|
if c.UseStdout {
|
|
|
|
stdout = c.OutStream
|
|
|
|
}
|
|
|
|
if c.UseStderr {
|
|
|
|
stderr = errStream
|
2014-12-04 21:12:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 22:01:18 +00:00
|
|
|
return container.attachWithLogs(ctx, stdin, stdout, stderr, c.Logs, c.Stream)
|
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
|
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-10 22:01:18 +00:00
|
|
|
func (daemon *Daemon) ContainerWsAttachWithLogs(ctx context.Context, prefixOrName string, c *ContainerWsAttachWithLogsConfig) error {
|
|
|
|
container, err := daemon.Get(ctx, prefixOrName)
|
2015-09-17 19:57:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-10 22:01:18 +00:00
|
|
|
return container.attachWithLogs(ctx, c.InStream, c.OutStream, c.ErrStream, c.Logs, c.Stream)
|
2015-03-29 21:17:23 +00:00
|
|
|
}
|