- Align output. Previously, output would end up unaligned because of
longer task names (e.g. web.1 vs web.10)
- Truncate task IDs and add a --no-trunc option
- Added a --no-ids option to remove IDs altogether
- Got rid of the generic ID Resolver as we need more customization.
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
| ... | ... |
@@ -7,7 +7,6 @@ import ( |
| 7 | 7 |
|
| 8 | 8 |
"github.com/docker/docker/api/types/swarm" |
| 9 | 9 |
"github.com/docker/docker/client" |
| 10 |
- "github.com/docker/docker/pkg/stringid" |
|
| 11 | 10 |
) |
| 12 | 11 |
|
| 13 | 12 |
// IDResolver provides ID to Name resolution. |
| ... | ... |
@@ -27,7 +26,7 @@ func New(client client.APIClient, noResolve bool) *IDResolver {
|
| 27 | 27 |
} |
| 28 | 28 |
|
| 29 | 29 |
func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string, error) {
|
| 30 |
- switch t := t.(type) {
|
|
| 30 |
+ switch t.(type) {
|
|
| 31 | 31 |
case swarm.Node: |
| 32 | 32 |
node, _, err := r.client.NodeInspectWithRaw(ctx, id) |
| 33 | 33 |
if err != nil {
|
| ... | ... |
@@ -46,25 +45,6 @@ func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string,
|
| 46 | 46 |
return id, nil |
| 47 | 47 |
} |
| 48 | 48 |
return service.Spec.Annotations.Name, nil |
| 49 |
- case swarm.Task: |
|
| 50 |
- // If the caller passes the full task there's no need to do a lookup. |
|
| 51 |
- if t.ID == "" {
|
|
| 52 |
- var err error |
|
| 53 |
- |
|
| 54 |
- t, _, err = r.client.TaskInspectWithRaw(ctx, id) |
|
| 55 |
- if err != nil {
|
|
| 56 |
- return id, nil |
|
| 57 |
- } |
|
| 58 |
- } |
|
| 59 |
- taskID := stringid.TruncateID(t.ID) |
|
| 60 |
- if t.ServiceID == "" {
|
|
| 61 |
- return taskID, nil |
|
| 62 |
- } |
|
| 63 |
- service, err := r.Resolve(ctx, swarm.Service{}, t.ServiceID)
|
|
| 64 |
- if err != nil {
|
|
| 65 |
- return "", err |
|
| 66 |
- } |
|
| 67 |
- return fmt.Sprintf("%s.%d.%s", service, t.Slot, taskID), nil
|
|
| 68 | 49 |
default: |
| 69 | 50 |
return "", fmt.Errorf("unsupported type")
|
| 70 | 51 |
} |
| ... | ... |
@@ -4,6 +4,7 @@ import ( |
| 4 | 4 |
"bytes" |
| 5 | 5 |
"fmt" |
| 6 | 6 |
"io" |
| 7 |
+ "strconv" |
|
| 7 | 8 |
"strings" |
| 8 | 9 |
|
| 9 | 10 |
"golang.org/x/net/context" |
| ... | ... |
@@ -13,12 +14,16 @@ import ( |
| 13 | 13 |
"github.com/docker/docker/cli" |
| 14 | 14 |
"github.com/docker/docker/cli/command" |
| 15 | 15 |
"github.com/docker/docker/cli/command/idresolver" |
| 16 |
+ "github.com/docker/docker/client" |
|
| 16 | 17 |
"github.com/docker/docker/pkg/stdcopy" |
| 18 |
+ "github.com/docker/docker/pkg/stringid" |
|
| 17 | 19 |
"github.com/spf13/cobra" |
| 18 | 20 |
) |
| 19 | 21 |
|
| 20 | 22 |
type logsOptions struct {
|
| 21 | 23 |
noResolve bool |
| 24 |
+ noTrunc bool |
|
| 25 |
+ noIDs bool |
|
| 22 | 26 |
follow bool |
| 23 | 27 |
since string |
| 24 | 28 |
timestamps bool |
| ... | ... |
@@ -44,6 +49,8 @@ func newLogsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
| 44 | 44 |
|
| 45 | 45 |
flags := cmd.Flags() |
| 46 | 46 |
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names") |
| 47 |
+ flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output") |
|
| 48 |
+ flags.BoolVar(&opts.noIDs, "no-ids", false, "Do not include task IDs") |
|
| 47 | 49 |
flags.BoolVarP(&opts.follow, "follow", "f", false, "Follow log output") |
| 48 | 50 |
flags.StringVar(&opts.since, "since", "", "Show logs since timestamp") |
| 49 | 51 |
flags.BoolVarP(&opts.timestamps, "timestamps", "t", false, "Show timestamps") |
| ... | ... |
@@ -66,26 +73,91 @@ func runLogs(dockerCli *command.DockerCli, opts *logsOptions) error {
|
| 66 | 66 |
} |
| 67 | 67 |
|
| 68 | 68 |
client := dockerCli.Client() |
| 69 |
+ |
|
| 70 |
+ service, _, err := client.ServiceInspectWithRaw(ctx, opts.service) |
|
| 71 |
+ if err != nil {
|
|
| 72 |
+ return err |
|
| 73 |
+ } |
|
| 74 |
+ |
|
| 69 | 75 |
responseBody, err := client.ServiceLogs(ctx, opts.service, options) |
| 70 | 76 |
if err != nil {
|
| 71 | 77 |
return err |
| 72 | 78 |
} |
| 73 | 79 |
defer responseBody.Close() |
| 74 | 80 |
|
| 75 |
- resolver := idresolver.New(client, opts.noResolve) |
|
| 81 |
+ var replicas uint64 |
|
| 82 |
+ if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
|
|
| 83 |
+ replicas = *service.Spec.Mode.Replicated.Replicas |
|
| 84 |
+ } |
|
| 85 |
+ padding := len(strconv.FormatUint(replicas, 10)) |
|
| 86 |
+ |
|
| 87 |
+ taskFormatter := newTaskFormatter(client, opts, padding) |
|
| 76 | 88 |
|
| 77 |
- stdout := &logWriter{ctx: ctx, opts: opts, r: resolver, w: dockerCli.Out()}
|
|
| 78 |
- stderr := &logWriter{ctx: ctx, opts: opts, r: resolver, w: dockerCli.Err()}
|
|
| 89 |
+ stdout := &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: dockerCli.Out()}
|
|
| 90 |
+ stderr := &logWriter{ctx: ctx, opts: opts, f: taskFormatter, w: dockerCli.Err()}
|
|
| 79 | 91 |
|
| 80 | 92 |
// TODO(aluzzardi): Do an io.Copy for services with TTY enabled. |
| 81 | 93 |
_, err = stdcopy.StdCopy(stdout, stderr, responseBody) |
| 82 | 94 |
return err |
| 83 | 95 |
} |
| 84 | 96 |
|
| 97 |
+type taskFormatter struct {
|
|
| 98 |
+ client client.APIClient |
|
| 99 |
+ opts *logsOptions |
|
| 100 |
+ padding int |
|
| 101 |
+ |
|
| 102 |
+ r *idresolver.IDResolver |
|
| 103 |
+ cache map[logContext]string |
|
| 104 |
+} |
|
| 105 |
+ |
|
| 106 |
+func newTaskFormatter(client client.APIClient, opts *logsOptions, padding int) *taskFormatter {
|
|
| 107 |
+ return &taskFormatter{
|
|
| 108 |
+ client: client, |
|
| 109 |
+ opts: opts, |
|
| 110 |
+ padding: padding, |
|
| 111 |
+ r: idresolver.New(client, opts.noResolve), |
|
| 112 |
+ cache: make(map[logContext]string), |
|
| 113 |
+ } |
|
| 114 |
+} |
|
| 115 |
+ |
|
| 116 |
+func (f *taskFormatter) format(ctx context.Context, logCtx logContext) (string, error) {
|
|
| 117 |
+ if cached, ok := f.cache[logCtx]; ok {
|
|
| 118 |
+ return cached, nil |
|
| 119 |
+ } |
|
| 120 |
+ |
|
| 121 |
+ nodeName, err := f.r.Resolve(ctx, swarm.Node{}, logCtx.nodeID)
|
|
| 122 |
+ if err != nil {
|
|
| 123 |
+ return "", err |
|
| 124 |
+ } |
|
| 125 |
+ |
|
| 126 |
+ serviceName, err := f.r.Resolve(ctx, swarm.Service{}, logCtx.serviceID)
|
|
| 127 |
+ if err != nil {
|
|
| 128 |
+ return "", err |
|
| 129 |
+ } |
|
| 130 |
+ |
|
| 131 |
+ task, _, err := f.client.TaskInspectWithRaw(ctx, logCtx.taskID) |
|
| 132 |
+ if err != nil {
|
|
| 133 |
+ return "", err |
|
| 134 |
+ } |
|
| 135 |
+ |
|
| 136 |
+ taskName := fmt.Sprintf("%s.%d", serviceName, task.Slot)
|
|
| 137 |
+ if !f.opts.noIDs {
|
|
| 138 |
+ if f.opts.noTrunc {
|
|
| 139 |
+ taskName += fmt.Sprintf(".%s", task.ID)
|
|
| 140 |
+ } else {
|
|
| 141 |
+ taskName += fmt.Sprintf(".%s", stringid.TruncateID(task.ID))
|
|
| 142 |
+ } |
|
| 143 |
+ } |
|
| 144 |
+ padding := strings.Repeat(" ", f.padding-len(strconv.FormatInt(int64(task.Slot), 10)))
|
|
| 145 |
+ formatted := fmt.Sprintf("%s@%s%s", taskName, nodeName, padding)
|
|
| 146 |
+ f.cache[logCtx] = formatted |
|
| 147 |
+ return formatted, nil |
|
| 148 |
+} |
|
| 149 |
+ |
|
| 85 | 150 |
type logWriter struct {
|
| 86 | 151 |
ctx context.Context |
| 87 | 152 |
opts *logsOptions |
| 88 |
- r *idresolver.IDResolver |
|
| 153 |
+ f *taskFormatter |
|
| 89 | 154 |
w io.Writer |
| 90 | 155 |
} |
| 91 | 156 |
|
| ... | ... |
@@ -102,7 +174,7 @@ func (lw *logWriter) Write(buf []byte) (int, error) {
|
| 102 | 102 |
return 0, fmt.Errorf("invalid context in log message: %v", string(buf))
|
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 |
- taskName, nodeName, err := lw.parseContext(string(parts[contextIndex])) |
|
| 105 |
+ logCtx, err := lw.parseContext(string(parts[contextIndex])) |
|
| 106 | 106 |
if err != nil {
|
| 107 | 107 |
return 0, err |
| 108 | 108 |
} |
| ... | ... |
@@ -115,8 +187,11 @@ func (lw *logWriter) Write(buf []byte) (int, error) {
|
| 115 | 115 |
} |
| 116 | 116 |
|
| 117 | 117 |
if i == contextIndex {
|
| 118 |
- // TODO(aluzzardi): Consider constant padding. |
|
| 119 |
- output = append(output, []byte(fmt.Sprintf("%s@%s |", taskName, nodeName))...)
|
|
| 118 |
+ formatted, err := lw.f.format(lw.ctx, logCtx) |
|
| 119 |
+ if err != nil {
|
|
| 120 |
+ return 0, err |
|
| 121 |
+ } |
|
| 122 |
+ output = append(output, []byte(fmt.Sprintf("%s |", formatted))...)
|
|
| 120 | 123 |
} else {
|
| 121 | 124 |
output = append(output, part...) |
| 122 | 125 |
} |
| ... | ... |
@@ -129,35 +204,42 @@ func (lw *logWriter) Write(buf []byte) (int, error) {
|
| 129 | 129 |
return len(buf), nil |
| 130 | 130 |
} |
| 131 | 131 |
|
| 132 |
-func (lw *logWriter) parseContext(input string) (string, string, error) {
|
|
| 132 |
+func (lw *logWriter) parseContext(input string) (logContext, error) {
|
|
| 133 | 133 |
context := make(map[string]string) |
| 134 | 134 |
|
| 135 | 135 |
components := strings.Split(input, ",") |
| 136 | 136 |
for _, component := range components {
|
| 137 | 137 |
parts := strings.SplitN(component, "=", 2) |
| 138 | 138 |
if len(parts) != 2 {
|
| 139 |
- return "", "", fmt.Errorf("invalid context: %s", input)
|
|
| 139 |
+ return logContext{}, fmt.Errorf("invalid context: %s", input)
|
|
| 140 | 140 |
} |
| 141 | 141 |
context[parts[0]] = parts[1] |
| 142 | 142 |
} |
| 143 | 143 |
|
| 144 |
- taskID, ok := context["com.docker.swarm.task.id"] |
|
| 144 |
+ nodeID, ok := context["com.docker.swarm.node.id"] |
|
| 145 | 145 |
if !ok {
|
| 146 |
- return "", "", fmt.Errorf("missing task id in context: %s", input)
|
|
| 147 |
- } |
|
| 148 |
- taskName, err := lw.r.Resolve(lw.ctx, swarm.Task{}, taskID)
|
|
| 149 |
- if err != nil {
|
|
| 150 |
- return "", "", err |
|
| 146 |
+ return logContext{}, fmt.Errorf("missing node id in context: %s", input)
|
|
| 151 | 147 |
} |
| 152 | 148 |
|
| 153 |
- nodeID, ok := context["com.docker.swarm.node.id"] |
|
| 149 |
+ serviceID, ok := context["com.docker.swarm.service.id"] |
|
| 154 | 150 |
if !ok {
|
| 155 |
- return "", "", fmt.Errorf("missing node id in context: %s", input)
|
|
| 151 |
+ return logContext{}, fmt.Errorf("missing service id in context: %s", input)
|
|
| 156 | 152 |
} |
| 157 |
- nodeName, err := lw.r.Resolve(lw.ctx, swarm.Node{}, nodeID)
|
|
| 158 |
- if err != nil {
|
|
| 159 |
- return "", "", err |
|
| 153 |
+ |
|
| 154 |
+ taskID, ok := context["com.docker.swarm.task.id"] |
|
| 155 |
+ if !ok {
|
|
| 156 |
+ return logContext{}, fmt.Errorf("missing task id in context: %s", input)
|
|
| 160 | 157 |
} |
| 161 | 158 |
|
| 162 |
- return taskName, nodeName, nil |
|
| 159 |
+ return logContext{
|
|
| 160 |
+ nodeID: nodeID, |
|
| 161 |
+ serviceID: serviceID, |
|
| 162 |
+ taskID: taskID, |
|
| 163 |
+ }, nil |
|
| 164 |
+} |
|
| 165 |
+ |
|
| 166 |
+type logContext struct {
|
|
| 167 |
+ nodeID string |
|
| 168 |
+ serviceID string |
|
| 169 |
+ taskID string |
|
| 163 | 170 |
} |