2016-09-08 13:11:39 -04:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/cli/cli/command/idresolver"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-11-07 00:54:40 -05:00
|
|
|
// Print task information in a format.
|
2016-10-27 20:02:57 -04:00
|
|
|
// Besides this, command `docker node ps <node>`
|
|
|
|
// and `docker stack ps` will call this, too.
|
2016-11-07 00:54:40 -05:00
|
|
|
func Print(dockerCli command.Cli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, trunc, quiet bool, format string) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
sort.Stable(tasksBySlot(tasks))
|
|
|
|
|
2016-11-07 00:54:40 -05:00
|
|
|
names := map[string]string{}
|
|
|
|
nodes := map[string]string{}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-11-07 00:54:40 -05:00
|
|
|
tasksCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewTaskFormat(format, quiet),
|
|
|
|
Trunc: trunc,
|
2016-10-27 20:02:57 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 21:52:47 -05:00
|
|
|
prevName := ""
|
2016-09-08 13:11:39 -04:00
|
|
|
for _, task := range tasks {
|
2016-12-06 21:52:47 -05:00
|
|
|
serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-04 22:23:07 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:52:47 -05:00
|
|
|
name := ""
|
|
|
|
if task.Slot != 0 {
|
|
|
|
name = fmt.Sprintf("%v.%v", serviceName, task.Slot)
|
|
|
|
} else {
|
|
|
|
name = fmt.Sprintf("%v.%v", serviceName, task.NodeID)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
// Indent the name if necessary
|
|
|
|
indentedName := name
|
2016-12-06 21:52:47 -05:00
|
|
|
if name == prevName {
|
2016-09-08 13:11:39 -04:00
|
|
|
indentedName = fmt.Sprintf(" \\_ %s", indentedName)
|
|
|
|
}
|
2016-12-06 21:52:47 -05:00
|
|
|
prevName = name
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-11-07 00:54:40 -05:00
|
|
|
names[task.ID] = name
|
|
|
|
if tasksCtx.Format.IsTable() {
|
|
|
|
names[task.ID] = indentedName
|
2016-11-17 01:21:18 -05:00
|
|
|
}
|
2016-11-07 00:54:40 -05:00
|
|
|
nodes[task.ID] = nodeValue
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-11-07 00:54:40 -05:00
|
|
|
|
|
|
|
return formatter.TaskWrite(tasksCtx, tasks, names, nodes)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|