forked from docker/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
298 lines (255 loc) · 7.94 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package service
import (
"bytes"
"fmt"
"io"
"strconv"
"strings"
"golang.org/x/net/context"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/idresolver"
"github.com/docker/cli/client"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/docker/pkg/stringid"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type logsOptions struct {
noResolve bool
noTrunc bool
noTaskIDs bool
follow bool
since string
timestamps bool
tail string
target string
}
// 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.
func newLogsCommand(dockerCli *command.DockerCli) *cobra.Command {
var opts logsOptions
cmd := &cobra.Command{
Use: "logs [OPTIONS] SERVICE|TASK",
Short: "Fetch the logs of a service or task",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.target = args[0]
return runLogs(dockerCli, &opts)
},
Tags: map[string]string{"version": "1.29"},
}
flags := cmd.Flags()
// options specific to service logs
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names in output")
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
flags.BoolVar(&opts.noTaskIDs, "no-task-ids", false, "Do not include task IDs in output")
// options identical to container logs
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output")
flags.StringVar(&opts.since, "since", "", "Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)")
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,
Details: true,
}
cli := dockerCli.Client()
var (
maxLength = 1
responseBody io.ReadCloser
tty bool
)
service, _, err := cli.ServiceInspectWithRaw(ctx, opts.target, types.ServiceInspectOptions{})
if err != nil {
// 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)
tty = task.Spec.ContainerSpec.TTY
// TODO(dperny) hot fix until we get a nice details system squared away,
// ignores details (including task context) if we have a TTY log
// if we don't do this, we'll vomit the huge context verbatim into the
// TTY log lines and that's Undesirable.
if tty {
options.Details = false
}
responseBody, err = cli.TaskLogs(ctx, opts.target, options)
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 {
tty = service.Spec.TaskTemplate.ContainerSpec.TTY
// TODO(dperny) hot fix until we get a nice details system squared away,
// ignores details (including task context) if we have a TTY log
if tty {
options.Details = false
}
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))
}
}
defer responseBody.Close()
if tty {
_, err = io.Copy(dockerCli.Out(), responseBody)
return err
}
taskFormatter := newTaskFormatter(cli, opts, maxLength)
stdout := &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: dockerCli.Out()}
stderr := &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: dockerCli.Err()}
// TODO(aluzzardi): Do an io.Copy for services with TTY enabled.
_, err = stdcopy.StdCopy(stdout, stderr, responseBody)
return err
}
// getMaxLength gets the maximum length of the number in base 10
func getMaxLength(i int) int {
return len(strconv.FormatInt(int64(i), 10))
}
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)
if !f.opts.noTaskIDs {
if f.opts.noTrunc {
taskName += fmt.Sprintf(".%s", task.ID)
} else {
taskName += fmt.Sprintf(".%s", stringid.TruncateID(task.ID))
}
}
padding := strings.Repeat(" ", f.padding-getMaxLength(task.Slot))
formatted := fmt.Sprintf("%s@%s%s", taskName, nodeName, padding)
f.cache[logCtx] = formatted
return formatted, nil
}
type logWriter struct {
ctx context.Context
opts *logsOptions
f *taskFormatter
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 {
return 0, errors.Errorf("invalid context in log message: %v", string(buf))
}
logCtx, err := lw.parseContext(string(parts[contextIndex]))
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 {
formatted, err := lw.f.format(lw.ctx, logCtx)
if err != nil {
return 0, err
}
output = append(output, []byte(fmt.Sprintf("%s |", formatted))...)
} else {
output = append(output, part...)
}
}
_, err = lw.w.Write(output)
if err != nil {
return 0, err
}
return len(buf), nil
}
func (lw *logWriter) parseContext(input string) (logContext, error) {
context := make(map[string]string)
components := strings.Split(input, ",")
for _, component := range components {
parts := strings.SplitN(component, "=", 2)
if len(parts) != 2 {
return logContext{}, errors.Errorf("invalid context: %s", input)
}
context[parts[0]] = parts[1]
}
nodeID, ok := context["com.docker.swarm.node.id"]
if !ok {
return logContext{}, errors.Errorf("missing node id in context: %s", input)
}
serviceID, ok := context["com.docker.swarm.service.id"]
if !ok {
return logContext{}, errors.Errorf("missing service id in context: %s", input)
}
taskID, ok := context["com.docker.swarm.task.id"]
if !ok {
return logContext{}, errors.Errorf("missing task id in context: %s", input)
}
return logContext{
nodeID: nodeID,
serviceID: serviceID,
taskID: taskID,
}, nil
}
type logContext struct {
nodeID string
serviceID string
taskID string
}