2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-07-31 17:03:21 -04:00
|
|
|
|
|
|
|
import (
|
2016-12-25 01:37:31 -05:00
|
|
|
"errors"
|
2014-07-31 17:03:21 -04:00
|
|
|
"strconv"
|
2016-01-27 17:09:42 -05:00
|
|
|
"time"
|
2014-07-31 17:03:21 -04:00
|
|
|
|
2016-03-25 14:33:54 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-03-20 13:07:04 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-01-27 17:09:42 -05:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 14:18:12 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
timetypes "github.com/docker/docker/api/types/time"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2015-06-30 20:40:13 -04:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-07-31 17:03:21 -04:00
|
|
|
)
|
|
|
|
|
2017-03-20 13:07:04 -04:00
|
|
|
// ContainerLogs copies the container's log channel to the channel provided in
|
|
|
|
// the config. If ContainerLogs returns an error, no messages have been copied.
|
|
|
|
// and the channel will be closed without data.
|
|
|
|
//
|
|
|
|
// if it returns nil, the config channel will be active and return log
|
|
|
|
// messages until it runs out or the context is canceled.
|
2017-07-19 10:20:13 -04:00
|
|
|
func (daemon *Daemon) ContainerLogs(ctx context.Context, containerName string, config *types.ContainerLogsOptions) (<-chan *backend.LogMessage, bool, error) {
|
2017-03-20 13:07:04 -04:00
|
|
|
lg := logrus.WithFields(logrus.Fields{
|
|
|
|
"module": "daemon",
|
|
|
|
"method": "(*Daemon).ContainerLogs",
|
|
|
|
"container": containerName,
|
|
|
|
})
|
|
|
|
|
2016-11-22 08:51:22 -05:00
|
|
|
if !(config.ShowStdout || config.ShowStderr) {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, false, errdefs.InvalidParameter(errors.New("You must choose at least one stream"))
|
2016-11-22 08:51:22 -05:00
|
|
|
}
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := daemon.GetContainer(containerName)
|
2015-09-28 16:36:29 -04:00
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, false, err
|
2015-09-28 16:36:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-24 03:55:55 -04:00
|
|
|
if container.RemovalInProgress || container.Dead {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, false, errdefs.Conflict(errors.New("can not get logs from container which is dead or marked for removal"))
|
2017-03-24 03:55:55 -04:00
|
|
|
}
|
|
|
|
|
2016-11-22 08:51:22 -05:00
|
|
|
if container.HostConfig.LogConfig.Type == "none" {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, false, logger.ErrReadLogsNotSupported{}
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
2015-04-11 17:49:14 -04:00
|
|
|
|
2017-04-20 06:17:06 -04:00
|
|
|
cLog, cLogCreated, err := daemon.getLogger(container)
|
2015-04-09 00:23:30 -04:00
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, false, err
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
2017-04-20 06:17:06 -04:00
|
|
|
if cLogCreated {
|
|
|
|
defer func() {
|
|
|
|
if err = cLog.Close(); err != nil {
|
|
|
|
logrus.Errorf("Error closing logger: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-03-20 13:07:04 -04:00
|
|
|
|
2015-07-03 09:50:06 -04:00
|
|
|
logReader, ok := cLog.(logger.LogReader)
|
2015-06-30 20:40:13 -04:00
|
|
|
if !ok {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, false, logger.ErrReadLogsNotSupported{}
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
2015-05-06 21:09:27 -04:00
|
|
|
|
2017-03-15 11:41:12 -04:00
|
|
|
follow := config.Follow && !cLogCreated
|
2015-07-03 09:50:06 -04:00
|
|
|
tailLines, err := strconv.Atoi(config.Tail)
|
|
|
|
if err != nil {
|
|
|
|
tailLines = -1
|
|
|
|
}
|
2015-04-23 18:08:41 -04:00
|
|
|
|
2016-01-27 17:09:42 -05:00
|
|
|
var since time.Time
|
|
|
|
if config.Since != "" {
|
|
|
|
s, n, err := timetypes.ParseTimestamps(config.Since, 0)
|
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, false, err
|
2016-01-27 17:09:42 -05:00
|
|
|
}
|
|
|
|
since = time.Unix(s, n)
|
|
|
|
}
|
2017-03-20 13:07:04 -04:00
|
|
|
|
2017-04-28 07:53:00 -04:00
|
|
|
var until time.Time
|
|
|
|
if config.Until != "" && config.Until != "0" {
|
|
|
|
s, n, err := timetypes.ParseTimestamps(config.Until, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
until = time.Unix(s, n)
|
|
|
|
}
|
|
|
|
|
2015-07-03 09:50:06 -04:00
|
|
|
readConfig := logger.ReadConfig{
|
2016-01-27 17:09:42 -05:00
|
|
|
Since: since,
|
2017-04-28 07:53:00 -04:00
|
|
|
Until: until,
|
2015-07-03 09:50:06 -04:00
|
|
|
Tail: tailLines,
|
|
|
|
Follow: follow,
|
|
|
|
}
|
2014-10-30 15:33:26 -04:00
|
|
|
|
2017-03-20 13:07:04 -04:00
|
|
|
logs := logReader.ReadLogs(readConfig)
|
2015-12-19 09:43:10 -05:00
|
|
|
|
2017-03-20 13:07:04 -04:00
|
|
|
// past this point, we can't possibly return any errors, so we can just
|
|
|
|
// start a goroutine and return to tell the caller not to expect errors
|
|
|
|
// (if the caller wants to give up on logs, they have to cancel the context)
|
|
|
|
// this goroutine functions as a shim between the logger and the caller.
|
|
|
|
messageChan := make(chan *backend.LogMessage, 1)
|
|
|
|
go func() {
|
|
|
|
// set up some defers
|
2017-04-20 06:17:06 -04:00
|
|
|
defer logs.Close()
|
|
|
|
|
2017-03-20 13:07:04 -04:00
|
|
|
// close the messages channel. closing is the only way to signal above
|
|
|
|
// that we're doing with logs (other than context cancel i guess).
|
|
|
|
defer close(messageChan)
|
|
|
|
|
|
|
|
lg.Debug("begin logs")
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// i do not believe as the system is currently designed any error
|
|
|
|
// is possible, but we should be prepared to handle it anyway. if
|
|
|
|
// we do get an error, copy only the error field to a new object so
|
|
|
|
// we don't end up with partial data in the other fields
|
|
|
|
case err := <-logs.Err:
|
|
|
|
lg.Errorf("Error streaming logs: %v", err)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case messageChan <- &backend.LogMessage{Err: err}:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case <-ctx.Done():
|
2017-07-18 11:54:03 -04:00
|
|
|
lg.Debugf("logs: end stream, ctx is done: %v", ctx.Err())
|
2017-03-20 13:07:04 -04:00
|
|
|
return
|
|
|
|
case msg, ok := <-logs.Msg:
|
|
|
|
// there is some kind of pool or ring buffer in the logger that
|
|
|
|
// produces these messages, and a possible future optimization
|
|
|
|
// might be to use that pool and reuse message objects
|
|
|
|
if !ok {
|
|
|
|
lg.Debug("end logs")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m := msg.AsLogMessage() // just a pointer conversion, does not copy data
|
|
|
|
|
|
|
|
// there could be a case where the reader stops accepting
|
|
|
|
// messages and the context is canceled. we need to check that
|
|
|
|
// here, or otherwise we risk blocking forever on the message
|
|
|
|
// send.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case messageChan <- m:
|
|
|
|
}
|
2014-10-30 15:33:26 -04:00
|
|
|
}
|
2014-07-31 17:03:21 -04:00
|
|
|
}
|
2017-03-20 13:07:04 -04:00
|
|
|
}()
|
2017-07-19 10:20:13 -04:00
|
|
|
return messageChan, container.Config.Tty, nil
|
2015-06-30 20:40:13 -04:00
|
|
|
}
|
2015-11-03 13:45:12 -05:00
|
|
|
|
2017-04-20 06:17:06 -04:00
|
|
|
func (daemon *Daemon) getLogger(container *container.Container) (l logger.Logger, created bool, err error) {
|
|
|
|
container.Lock()
|
|
|
|
if container.State.Running {
|
|
|
|
l = container.LogDriver
|
|
|
|
}
|
|
|
|
container.Unlock()
|
|
|
|
if l == nil {
|
|
|
|
created = true
|
|
|
|
l, err = container.StartLogger()
|
2015-11-03 13:45:12 -05:00
|
|
|
}
|
2017-04-20 06:17:06 -04:00
|
|
|
return
|
2015-11-03 13:45:12 -05:00
|
|
|
}
|
|
|
|
|
2016-03-12 07:50:37 -05:00
|
|
|
// mergeLogConfig merges the daemon log config to the container's log config if the container's log driver is not specified.
|
|
|
|
func (daemon *Daemon) mergeAndVerifyLogConfig(cfg *containertypes.LogConfig) error {
|
|
|
|
if cfg.Type == "" {
|
|
|
|
cfg.Type = daemon.defaultLogConfig.Type
|
|
|
|
}
|
|
|
|
|
2016-07-12 15:05:44 -04:00
|
|
|
if cfg.Config == nil {
|
|
|
|
cfg.Config = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2016-03-12 07:50:37 -05:00
|
|
|
if cfg.Type == daemon.defaultLogConfig.Type {
|
|
|
|
for k, v := range daemon.defaultLogConfig.Config {
|
|
|
|
if _, ok := cfg.Config[k]; !ok {
|
|
|
|
cfg.Config[k] = v
|
|
|
|
}
|
2016-03-02 07:22:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 07:50:37 -05:00
|
|
|
return logger.ValidateLogOpts(cfg.Type, cfg.Config)
|
2016-03-02 07:22:18 -05:00
|
|
|
}
|