2016-10-26 04:19:32 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-10-26 04:19:32 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-05-08 13:58:37 -04:00
|
|
|
"sort"
|
2016-12-06 21:57:22 -05:00
|
|
|
"strconv"
|
2016-10-26 04:19:32 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/idresolver"
|
2017-09-07 12:44:52 -04:00
|
|
|
"github.com/docker/cli/service/logs"
|
2016-10-26 04:19:32 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2023-10-13 14:34:32 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2016-10-26 04:19:32 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2023-05-10 08:43:18 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
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-05-08 13:58:37 -04:00
|
|
|
details bool
|
|
|
|
raw bool
|
2016-10-26 04:19:32 -04:00
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
target string
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func newLogsCommand(dockerCli command.Cli) *cobra.Command {
|
2016-10-26 04:19:32 -04:00
|
|
|
var opts logsOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2017-04-06 14:55:54 -04:00
|
|
|
Use: "logs [OPTIONS] SERVICE|TASK",
|
|
|
|
Short: "Fetch the logs of a service or task",
|
2016-10-26 04:19:32 -04:00
|
|
|
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)
|
|
|
|
},
|
2017-10-25 12:59:32 -04:00
|
|
|
Annotations: map[string]string{"version": "1.29"},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return CompletionFn(dockerCli)(cmd, args, toComplete)
|
|
|
|
},
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-04-06 14:55:54 -04:00
|
|
|
// options specific to service logs
|
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-05-08 13:58:37 -04:00
|
|
|
flags.BoolVar(&opts.raw, "raw", false, "Do not neatly format logs")
|
|
|
|
flags.SetAnnotation("raw", "version", []string{"1.30"})
|
2017-03-21 14:35:55 -04:00
|
|
|
flags.BoolVar(&opts.noTaskIDs, "no-task-ids", false, "Do not include task IDs in output")
|
2017-04-06 14:55:54 -04:00
|
|
|
// options identical to container logs
|
2016-10-26 04:19:32 -04:00
|
|
|
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.StringVar(&opts.since, "since", "", `Show logs since timestamp (e.g. "2013-01-02T13:23:37Z") 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")
|
2017-05-08 13:58:37 -04:00
|
|
|
flags.BoolVar(&opts.details, "details", false, "Show extra details provided to logs")
|
|
|
|
flags.SetAnnotation("details", "version", []string{"1.30"})
|
2020-08-06 10:35:27 -04:00
|
|
|
flags.StringVarP(&opts.tail, "tail", "n", "all", "Number of lines to show from the end of the logs")
|
2016-10-26 04:19:32 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func runLogs(dockerCli command.Cli, opts *logsOptions) error {
|
2016-10-26 04:19:32 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-10-13 14:34:32 -04:00
|
|
|
apiClient := dockerCli.Client()
|
2016-12-06 21:57:22 -05:00
|
|
|
|
2017-03-21 14:35:55 -04:00
|
|
|
var (
|
|
|
|
maxLength = 1
|
|
|
|
responseBody io.ReadCloser
|
2017-03-20 13:07:04 -04:00
|
|
|
tty bool
|
2017-05-08 13:58:37 -04:00
|
|
|
// logfunc is used to delay the call to logs so that we can do some
|
|
|
|
// processing before we actually get the logs
|
2023-10-13 14:34:32 -04:00
|
|
|
logfunc func(context.Context, string, container.LogsOptions) (io.ReadCloser, error)
|
2017-03-21 14:35:55 -04:00
|
|
|
)
|
2016-12-06 21:57:22 -05:00
|
|
|
|
2023-10-13 14:34:32 -04:00
|
|
|
service, _, err := apiClient.ServiceInspectWithRaw(ctx, opts.target, types.ServiceInspectOptions{})
|
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
|
2023-05-10 08:43:18 -04:00
|
|
|
if !errdefs.IsNotFound(err) {
|
2017-03-21 14:35:55 -04:00
|
|
|
return err
|
|
|
|
}
|
2023-10-13 14:34:32 -04:00
|
|
|
task, _, err := apiClient.TaskInspectWithRaw(ctx, opts.target)
|
2017-04-26 23:31:01 -04:00
|
|
|
if err != nil {
|
2023-05-10 08:43:18 -04:00
|
|
|
if errdefs.IsNotFound(err) {
|
2017-04-26 23:31:01 -04:00
|
|
|
// if the task isn't found, rewrite the error to be clear
|
|
|
|
// that we looked for services AND tasks and found none
|
2017-05-08 13:58:37 -04:00
|
|
|
err = fmt.Errorf("no such task or service: %v", opts.target)
|
2017-04-26 23:31:01 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
tty = task.Spec.ContainerSpec.TTY
|
2017-03-21 14:35:55 -04:00
|
|
|
maxLength = getMaxLength(task.Slot)
|
2017-05-08 13:58:37 -04:00
|
|
|
|
|
|
|
// use the TaskLogs api function
|
2023-10-13 14:34:32 -04:00
|
|
|
logfunc = apiClient.TaskLogs
|
2017-03-21 14:35:55 -04:00
|
|
|
} else {
|
2017-05-08 13:58:37 -04:00
|
|
|
// use ServiceLogs api function
|
2023-10-13 14:34:32 -04:00
|
|
|
logfunc = apiClient.ServiceLogs
|
2017-03-20 13:07:04 -04:00
|
|
|
tty = service.Spec.TaskTemplate.ContainerSpec.TTY
|
2017-03-21 14:35:55 -04:00
|
|
|
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
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
|
|
|
|
// we can't prettify tty logs. tell the user that this is the case.
|
|
|
|
// this is why we assign the logs function to a variable and delay calling
|
|
|
|
// it. we want to check this before we make the call and checking twice in
|
|
|
|
// each branch is even sloppier than this CLI disaster already is
|
|
|
|
if tty && !opts.raw {
|
|
|
|
return errors.New("tty service logs only supported with --raw")
|
|
|
|
}
|
|
|
|
|
|
|
|
// now get the logs
|
2023-10-13 14:34:32 -04:00
|
|
|
responseBody, err = logfunc(ctx, opts.target, container.LogsOptions{
|
|
|
|
ShowStdout: true,
|
|
|
|
ShowStderr: true,
|
|
|
|
Since: opts.since,
|
|
|
|
Timestamps: opts.timestamps,
|
|
|
|
Follow: opts.follow,
|
|
|
|
Tail: opts.tail,
|
|
|
|
// get the details if we request it OR if we're not doing raw mode
|
|
|
|
// (we need them for the context to pretty print)
|
|
|
|
Details: opts.details || !opts.raw,
|
|
|
|
})
|
2017-05-08 13:58:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-26 04:19:32 -04:00
|
|
|
defer responseBody.Close()
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
// tty logs get straight copied. they're not muxed with stdcopy
|
2017-03-20 13:07:04 -04:00
|
|
|
if tty {
|
|
|
|
_, err = io.Copy(dockerCli.Out(), responseBody)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
// otherwise, logs are multiplexed. if we're doing pretty printing, also
|
|
|
|
// create a task formatter.
|
|
|
|
var stdout, stderr io.Writer
|
|
|
|
stdout = dockerCli.Out()
|
|
|
|
stderr = dockerCli.Err()
|
|
|
|
if !opts.raw {
|
2023-10-13 14:34:32 -04:00
|
|
|
taskFormatter := newTaskFormatter(apiClient, opts, maxLength)
|
2016-10-26 04:19:32 -04:00
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
stdout = &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: stdout}
|
|
|
|
stderr = &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: stderr}
|
|
|
|
}
|
2016-10-26 04:19:32 -04:00
|
|
|
|
|
|
|
_, 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 {
|
2017-06-09 17:42:16 -04:00
|
|
|
return len(strconv.Itoa(i))
|
2017-03-21 14:35:55 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 21:57:22 -05:00
|
|
|
type taskFormatter struct {
|
|
|
|
client client.APIClient
|
|
|
|
opts *logsOptions
|
|
|
|
padding int
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
r *idresolver.IDResolver
|
|
|
|
// cache saves a pre-cooked logContext formatted string based on a
|
|
|
|
// logcontext object, so we don't have to resolve names every time
|
2016-12-06 21:57:22 -05:00
|
|
|
cache map[logContext]string
|
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func newTaskFormatter(apiClient client.APIClient, opts *logsOptions, padding int) *taskFormatter {
|
2016-12-06 21:57:22 -05:00
|
|
|
return &taskFormatter{
|
2023-11-20 11:38:50 -05:00
|
|
|
client: apiClient,
|
2016-12-06 21:57:22 -05:00
|
|
|
opts: opts,
|
|
|
|
padding: padding,
|
2023-11-20 11:38:50 -05:00
|
|
|
r: idresolver.New(apiClient, opts.noResolve),
|
2016-12-06 21:57:22 -05:00
|
|
|
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
|
|
|
|
2017-05-16 15:27:31 -04:00
|
|
|
paddingCount := f.padding - getMaxLength(task.Slot)
|
|
|
|
padding := ""
|
|
|
|
if paddingCount > 0 {
|
|
|
|
padding = strings.Repeat(" ", paddingCount)
|
|
|
|
}
|
|
|
|
formatted := taskName + "@" + nodeName + padding
|
2016-12-06 21:57:22 -05:00
|
|
|
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) {
|
2017-05-08 13:58:37 -04:00
|
|
|
// this works but ONLY because stdcopy calls write a whole line at a time.
|
|
|
|
// if this ends up horribly broken or panics, check to see if stdcopy has
|
2017-05-21 21:39:06 -04:00
|
|
|
// reneged on that assumption. (@god forgive me)
|
2017-05-08 13:58:37 -04:00
|
|
|
// also this only works because the logs format is, like, barely parsable.
|
|
|
|
// if something changes in the logs format, this is gonna break
|
|
|
|
|
|
|
|
// there should always be at least 2 parts: details and message. if there
|
|
|
|
// is no timestamp, details will be first (index 0) when we split on
|
|
|
|
// spaces. if there is a timestamp, details will be 2nd (`index 1)
|
|
|
|
detailsIndex := 0
|
2016-10-26 04:19:32 -04:00
|
|
|
numParts := 2
|
|
|
|
if lw.opts.timestamps {
|
2017-05-08 13:58:37 -04:00
|
|
|
detailsIndex++
|
2016-10-26 04:19:32 -04:00
|
|
|
numParts++
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
// break up the log line into parts.
|
2016-10-26 04:19:32 -04:00
|
|
|
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
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
// parse the details out
|
2017-09-07 12:44:52 -04:00
|
|
|
details, err := logs.ParseLogDetails(string(parts[detailsIndex]))
|
2017-05-08 13:58:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// and then create a context from the details
|
|
|
|
// this removes the context-specific details from the details map, so we
|
|
|
|
// can more easily print the details later
|
|
|
|
logCtx, err := lw.parseContext(details)
|
2016-10-26 04:19:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
output := []byte{}
|
2017-05-08 13:58:37 -04:00
|
|
|
// if we included timestamps, add them to the front
|
|
|
|
if lw.opts.timestamps {
|
|
|
|
output = append(output, parts[0]...)
|
|
|
|
output = append(output, ' ')
|
|
|
|
}
|
|
|
|
// add the context, nice and formatted
|
|
|
|
formatted, err := lw.f.format(lw.ctx, logCtx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
output = append(output, []byte(formatted+" | ")...)
|
|
|
|
// if the user asked for details, add them to be log message
|
|
|
|
if lw.opts.details {
|
|
|
|
// ugh i hate this it's basically a dupe of api/server/httputils/write_log_stream.go:stringAttrs()
|
|
|
|
// ok but we're gonna do it a bit different
|
|
|
|
|
|
|
|
// there are optimizations that can be made here. for starters, i'd
|
|
|
|
// suggest caching the details keys. then, we can maybe draw maps and
|
|
|
|
// slices from a pool to avoid alloc overhead on them. idk if it's
|
|
|
|
// worth the time yet.
|
|
|
|
|
|
|
|
// first we need a slice
|
|
|
|
d := make([]string, 0, len(details))
|
|
|
|
// then let's add all the pairs
|
|
|
|
for k := range details {
|
|
|
|
d = append(d, k+"="+details[k])
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
// then sort em
|
|
|
|
sort.Strings(d)
|
|
|
|
// then join and append
|
|
|
|
output = append(output, []byte(strings.Join(d, ","))...)
|
|
|
|
output = append(output, ' ')
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
|
|
|
|
// add the log message itself, finally
|
|
|
|
output = append(output, parts[detailsIndex+1]...)
|
|
|
|
|
2016-10-26 04:19:32 -04:00
|
|
|
_, err = lw.w.Write(output)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
// parseContext returns a log context and REMOVES the context from the details map
|
|
|
|
func (lw *logWriter) parseContext(details map[string]string) (logContext, error) {
|
|
|
|
nodeID, ok := details["com.docker.swarm.node.id"]
|
2016-10-26 04:19:32 -04:00
|
|
|
if !ok {
|
2017-05-08 13:58:37 -04:00
|
|
|
return logContext{}, errors.Errorf("missing node id in details: %v", details)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
delete(details, "com.docker.swarm.node.id")
|
2016-10-26 04:19:32 -04:00
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
serviceID, ok := details["com.docker.swarm.service.id"]
|
2016-10-26 04:19:32 -04:00
|
|
|
if !ok {
|
2017-05-08 13:58:37 -04:00
|
|
|
return logContext{}, errors.Errorf("missing service id in details: %v", details)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
delete(details, "com.docker.swarm.service.id")
|
2016-12-06 21:57:22 -05:00
|
|
|
|
2017-05-08 13:58:37 -04:00
|
|
|
taskID, ok := details["com.docker.swarm.task.id"]
|
2016-12-06 21:57:22 -05:00
|
|
|
if !ok {
|
2017-05-08 13:58:37 -04:00
|
|
|
return logContext{}, errors.Errorf("missing task id in details: %s", details)
|
2016-10-26 04:19:32 -04:00
|
|
|
}
|
2017-05-08 13:58:37 -04:00
|
|
|
delete(details, "com.docker.swarm.task.id")
|
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
|
|
|
}
|