2016-06-13 22:56:23 -04:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/api/client/idresolver"
|
|
|
|
"github.com/docker/engine-api/types/swarm"
|
|
|
|
"github.com/docker/go-units"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-07-21 22:19:11 -04:00
|
|
|
psTaskItemFmt = "%s\t%s\t%s\t%s\t%s\t%s %s ago\t%s\n"
|
|
|
|
maxErrLength = 30
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type tasksBySlot []swarm.Task
|
|
|
|
|
|
|
|
func (t tasksBySlot) Len() int {
|
|
|
|
return len(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t tasksBySlot) Swap(i, j int) {
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t tasksBySlot) Less(i, j int) bool {
|
|
|
|
// Sort by slot.
|
|
|
|
if t[i].Slot != t[j].Slot {
|
|
|
|
return t[i].Slot < t[j].Slot
|
|
|
|
}
|
|
|
|
|
|
|
|
// If same slot, sort by most recent.
|
|
|
|
return t[j].Meta.CreatedAt.Before(t[i].CreatedAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print task information in a table format
|
2016-08-02 12:25:14 -04:00
|
|
|
func Print(dockerCli *client.DockerCli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
|
2016-06-13 22:56:23 -04:00
|
|
|
sort.Stable(tasksBySlot(tasks))
|
|
|
|
|
|
|
|
writer := tabwriter.NewWriter(dockerCli.Out(), 0, 4, 2, ' ', 0)
|
|
|
|
|
|
|
|
// Ignore flushing errors
|
|
|
|
defer writer.Flush()
|
2016-07-21 22:19:11 -04:00
|
|
|
fmt.Fprintln(writer, strings.Join([]string{"ID", "NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR"}, "\t"))
|
|
|
|
|
|
|
|
prevName := ""
|
2016-06-13 22:56:23 -04:00
|
|
|
for _, task := range tasks {
|
|
|
|
serviceValue, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-21 22:19:11 -04:00
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
name := serviceValue
|
|
|
|
if task.Slot > 0 {
|
|
|
|
name = fmt.Sprintf("%s.%d", name, task.Slot)
|
|
|
|
}
|
2016-07-21 22:19:11 -04:00
|
|
|
|
|
|
|
// Indent the name if necessary
|
|
|
|
indentedName := name
|
|
|
|
if prevName == name {
|
|
|
|
indentedName = fmt.Sprintf(" \\_ %s", indentedName)
|
|
|
|
}
|
|
|
|
prevName = name
|
|
|
|
|
|
|
|
// Trim and quote the error message.
|
|
|
|
taskErr := task.Status.Err
|
2016-08-02 12:25:14 -04:00
|
|
|
if !noTrunc && len(taskErr) > maxErrLength {
|
2016-07-21 22:19:11 -04:00
|
|
|
taskErr = fmt.Sprintf("%s…", taskErr[:maxErrLength-1])
|
|
|
|
}
|
|
|
|
if len(taskErr) > 0 {
|
|
|
|
taskErr = fmt.Sprintf("\"%s\"", taskErr)
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
fmt.Fprintf(
|
|
|
|
writer,
|
|
|
|
psTaskItemFmt,
|
|
|
|
task.ID,
|
2016-07-21 22:19:11 -04:00
|
|
|
indentedName,
|
2016-06-13 22:56:23 -04:00
|
|
|
task.Spec.ContainerSpec.Image,
|
2016-07-21 22:19:11 -04:00
|
|
|
nodeValue,
|
|
|
|
client.PrettyPrint(task.DesiredState),
|
2016-06-13 22:56:23 -04:00
|
|
|
client.PrettyPrint(task.Status.State),
|
2016-07-01 20:55:52 -04:00
|
|
|
strings.ToLower(units.HumanDuration(time.Since(task.Status.Timestamp))),
|
2016-07-21 22:19:11 -04:00
|
|
|
taskErr,
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|