2016-10-26 04:19:32 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-12-06 21:57:22 -05:00
|
|
|
"strconv"
|
2016-10-26 04:19:32 -04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/cli/command/idresolver"
|
2016-12-06 21:57:22 -05:00
|
|
|
"github.com/docker/docker/client"
|
2016-10-26 04:19:32 -04:00
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
2016-12-06 21:57:22 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-10-26 04:19:32 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type logsOptions struct {
|
|
|
|
noResolve bool
|
2016-12-06 21:57:22 -05:00
|
|
|
noTrunc bool
|
2017-03-08 19:28:21 -05:00
|
|
|
noTaskIDs bool
|
2016-10-26 04:19:32 -04:00
|
|
|
follow bool
|
|
|
|
since string
|
|
|
|
timestamps bool
|
|
|
|
tail string
|
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
target string
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
// TODO(dperny) the whole CLI for this is kind of a mess IMHOIRL and it needs
|
|
|
|
// to be refactored agressively. There may be changes to the implementation of
|
|
|
|
// details, which will be need to be reflected in this code. The refactoring
|
|
|
|
// should be put off until we make those changes, tho, because I think the
|
|
|
|
// decisions made WRT details will impact the design of the CLI.
|
2016-10-26 04:19:32 -04:00
|
|
|
func newLogsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
var opts logsOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "logs [OPTIONS] SERVICE",
|
|
|
|
Short: "Fetch the logs of a service",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-03-21 14:35:55 -04:00
|
|
|
opts.target = args[0]
|
2016-10-26 04:19:32 -04:00
|
|
|
return runLogs(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
Tags: map[string]string{"experimental": ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-03-21 14:35:55 -04:00
|
|
|
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names in output")
|
2016-12-06 21:57:22 -05:00
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
|
2017-03-21 14:35:55 -04:00
|
|
|
flags.BoolVar(&opts.noTaskIDs, "no-task-ids", false, "Do not include task IDs in output")
|
2016-10-26 04:19:32 -04:00
|
|
|
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
|
2017-03-01 19:37:25 -05:00
|
|
|
flags.StringVar(&opts.since, "since", "", "Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)")
|
2016-10-26 04:19:32 -04:00
|
|
|
flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps")
|
|
|
|
flags.StringVar(&opts.tail, "tail", "all", "Number of lines to show from the end of the logs")
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLogs(dockerCli *command.DockerCli, opts *logsOptions) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
options := types.ContainerLogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Since: opts.since,
|
|
|
|
Timestamps: opts.timestamps,
|
|
|
|
Follow: opts.follow,
|
|
|
|
Tail: opts.tail,
|
|
|
|
}
|
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
cli := dockerCli.Client()
|
2016-12-06 21:57:22 -05:00
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
var (
|
|
|
|
maxLength = 1
|
|
|
|
responseBody io.ReadCloser
|
|
|
|
)
|
2016-12-06 21:57:22 -05:00
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
service, _, err := cli.ServiceInspectWithRaw(ctx, opts.target)
|
2016-10-26 04:19:32 -04:00
|
|
|
if err != nil {
|
2017-03-21 14:35:55 -04:00
|
|
|
// if it's any error other than service not found, it's Real
|
|
|
|
if !client.IsErrServiceNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
task, _, err := cli.TaskInspectWithRaw(ctx, opts.target)
|
|
|
|
if err != nil {
|
|
|
|
if client.IsErrTaskNotFound(err) {
|
|
|
|
// if the task ALSO isn't found, rewrite the error to be clear
|
|
|
|
// that we looked for services AND tasks
|
|
|
|
err = fmt.Errorf("No such task or service")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
maxLength = getMaxLength(task.Slot)
|
|
|
|
responseBody, err = cli.TaskLogs(ctx, opts.target, options)
|
|
|
|
} else {
|
|
|
|
responseBody, err = cli.ServiceLogs(ctx, opts.target, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
|
|
|
|
// if replicas are initialized, figure out if we need to pad them
|
|
|
|
replicas := *service.Spec.Mode.Replicated.Replicas
|
|
|
|
maxLength = getMaxLength(int(replicas))
|
|
|
|
}
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
taskFormatter := newTaskFormatter(cli, opts, maxLength)
|
2016-10-26 04:19:32 -04:00
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
stdout := &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: dockerCli.Out()}
|
|
|
|
stderr := &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: dockerCli.Err()}
|
2016-10-26 04:19:32 -04:00
|
|
|
|
|
|
|
// TODO(aluzzardi): Do an io.Copy for services with TTY enabled.
|
|
|
|
_, err = stdcopy.StdCopy(stdout, stderr, responseBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
// getMaxLength gets the maximum length of the number in base 10
|
|
|
|
func getMaxLength(i int) int {
|
|
|
|
return len(strconv.FormatInt(int64(i), 10))
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
type taskFormatter struct {
|
|
|
|
client client.APIClient
|
|
|
|
opts *logsOptions
|
|
|
|
padding int
|
|
|
|
|
|
|
|
r *idresolver.IDResolver
|
|
|
|
cache map[logContext]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTaskFormatter(client client.APIClient, opts *logsOptions, padding int) *taskFormatter {
|
|
|
|
return &taskFormatter{
|
|
|
|
client: client,
|
|
|
|
opts: opts,
|
|
|
|
padding: padding,
|
|
|
|
r: idresolver.New(client, opts.noResolve),
|
|
|
|
cache: make(map[logContext]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *taskFormatter) format(ctx context.Context, logCtx logContext) (string, error) {
|
|
|
|
if cached, ok := f.cache[logCtx]; ok {
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeName, err := f.r.Resolve(ctx, swarm.Node{}, logCtx.nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceName, err := f.r.Resolve(ctx, swarm.Service{}, logCtx.serviceID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
task, _, err := f.client.TaskInspectWithRaw(ctx, logCtx.taskID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
taskName := fmt.Sprintf("%s.%d", serviceName, task.Slot)
|
2017-03-08 19:28:21 -05:00
|
|
|
if !f.opts.noTaskIDs {
|
2016-12-06 21:57:22 -05:00
|
|
|
if f.opts.noTrunc {
|
|
|
|
taskName += fmt.Sprintf(".%s", task.ID)
|
|
|
|
} else {
|
|
|
|
taskName += fmt.Sprintf(".%s", stringid.TruncateID(task.ID))
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 14:35:55 -04:00
|
|
|
|
|
|
|
padding := strings.Repeat(" ", f.padding-getMaxLength(task.Slot))
|
2016-12-06 21:57:22 -05:00
|
|
|
formatted := fmt.Sprintf("%s@%s%s", taskName, nodeName, padding)
|
|
|
|
f.cache[logCtx] = formatted
|
|
|
|
return formatted, nil
|
|
|
|
}
|
|
|
|
|
2016-10-26 04:19:32 -04:00
|
|
|
type logWriter struct {
|
|
|
|
ctx context.Context
|
|
|
|
opts *logsOptions
|
2016-12-06 21:57:22 -05:00
|
|
|
f *taskFormatter
|
2016-10-26 04:19:32 -04:00
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw *logWriter) Write(buf []byte) (int, error) {
|
|
|
|
contextIndex := 0
|
|
|
|
numParts := 2
|
|
|
|
if lw.opts.timestamps {
|
|
|
|
contextIndex++
|
|
|
|
numParts++
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := bytes.SplitN(buf, []byte(" "), numParts)
|
|
|
|
if len(parts) != numParts {
|
2017-03-09 13:23:45 -05:00
|
|
|
return 0, errors.Errorf("invalid context in log message: %v", string(buf))
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
logCtx, err := lw.parseContext(string(parts[contextIndex]))
|
2016-10-26 04:19:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
output := []byte{}
|
|
|
|
for i, part := range parts {
|
|
|
|
// First part doesn't get space separation.
|
|
|
|
if i > 0 {
|
|
|
|
output = append(output, []byte(" ")...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == contextIndex {
|
2016-12-06 21:57:22 -05:00
|
|
|
formatted, err := lw.f.format(lw.ctx, logCtx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
output = append(output, []byte(fmt.Sprintf("%s |", formatted))...)
|
2016-10-26 04:19:32 -04:00
|
|
|
} else {
|
|
|
|
output = append(output, part...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = lw.w.Write(output)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
func (lw *logWriter) parseContext(input string) (logContext, error) {
|
2016-10-26 04:19:32 -04:00
|
|
|
context := make(map[string]string)
|
|
|
|
|
|
|
|
components := strings.Split(input, ",")
|
|
|
|
for _, component := range components {
|
|
|
|
parts := strings.SplitN(component, "=", 2)
|
|
|
|
if len(parts) != 2 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return logContext{}, errors.Errorf("invalid context: %s", input)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
context[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
nodeID, ok := context["com.docker.swarm.node.id"]
|
2016-10-26 04:19:32 -04:00
|
|
|
if !ok {
|
2017-03-09 13:23:45 -05:00
|
|
|
return logContext{}, errors.Errorf("missing node id in context: %s", input)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
serviceID, ok := context["com.docker.swarm.service.id"]
|
2016-10-26 04:19:32 -04:00
|
|
|
if !ok {
|
2017-03-09 13:23:45 -05:00
|
|
|
return logContext{}, errors.Errorf("missing service id in context: %s", input)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2016-12-06 21:57:22 -05:00
|
|
|
|
|
|
|
taskID, ok := context["com.docker.swarm.task.id"]
|
|
|
|
if !ok {
|
2017-03-09 13:23:45 -05:00
|
|
|
return logContext{}, errors.Errorf("missing task id in context: %s", input)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
return logContext{
|
|
|
|
nodeID: nodeID,
|
|
|
|
serviceID: serviceID,
|
|
|
|
taskID: taskID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type logContext struct {
|
|
|
|
nodeID string
|
|
|
|
serviceID string
|
|
|
|
taskID string
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|