2016-09-08 13:11:39 -04:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
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"
|
2017-07-05 13:40:08 -04:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2019-09-22 12:22:23 -04:00
|
|
|
"vbom.ml/util/sortorder"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2019-09-22 12:22:23 -04:00
|
|
|
type tasksSortable []swarm.Task
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2019-09-22 12:22:23 -04:00
|
|
|
func (t tasksSortable) Len() int {
|
2016-09-08 13:11:39 -04:00
|
|
|
return len(t)
|
|
|
|
}
|
|
|
|
|
2019-09-22 12:22:23 -04:00
|
|
|
func (t tasksSortable) Swap(i, j int) {
|
2016-09-08 13:11:39 -04:00
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
}
|
|
|
|
|
2019-09-22 12:22:23 -04:00
|
|
|
func (t tasksSortable) Less(i, j int) bool {
|
|
|
|
if t[i].Name != t[j].Name {
|
|
|
|
return sortorder.NaturalLess(t[i].Name, t[j].Name)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2019-09-22 12:22:23 -04:00
|
|
|
// Sort tasks for the same service and slot by most recent.
|
2016-09-08 13:11:39 -04:00
|
|
|
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.
|
2017-05-02 15:35:25 -04:00
|
|
|
func Print(ctx context.Context, dockerCli command.Cli, tasks []swarm.Task, resolver *idresolver.IDResolver, trunc, quiet bool, format string) error {
|
2019-09-22 12:22:23 -04:00
|
|
|
tasks, err := generateTaskNames(ctx, tasks, resolver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// First sort tasks, so that all tasks (including previous ones) of the same
|
|
|
|
// service and slot are together. This must be done first, to print "previous"
|
|
|
|
// tasks indented
|
|
|
|
sort.Stable(tasksSortable(tasks))
|
2016-09-08 13:11:39 -04:00
|
|
|
|
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(),
|
2018-10-23 11:05:44 -04:00
|
|
|
Format: NewTaskFormat(format, quiet),
|
2016-11-07 00:54:40 -05:00
|
|
|
Trunc: trunc,
|
2016-10-27 20:02:57 -04:00
|
|
|
}
|
|
|
|
|
2019-09-22 12:22:23 -04:00
|
|
|
var indent string
|
|
|
|
if tasksCtx.Format.IsTable() {
|
|
|
|
indent = ` \_ `
|
|
|
|
}
|
2016-12-06 21:52:47 -05:00
|
|
|
prevName := ""
|
2016-09-08 13:11:39 -04:00
|
|
|
for _, task := range tasks {
|
2019-09-22 12:22:23 -04:00
|
|
|
if task.Name == prevName {
|
|
|
|
// Indent previous tasks of the same slot
|
|
|
|
names[task.ID] = indent + task.Name
|
|
|
|
} else {
|
|
|
|
names[task.ID] = task.Name
|
2016-12-06 21:52:47 -05:00
|
|
|
}
|
2019-09-22 12:22:23 -04:00
|
|
|
prevName = task.Name
|
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
|
|
|
|
}
|
2019-09-22 12:22:23 -04:00
|
|
|
nodes[task.ID] = nodeValue
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2019-09-22 12:22:23 -04:00
|
|
|
return FormatWrite(tasksCtx, tasks, names, nodes)
|
|
|
|
}
|
2016-12-06 21:52:47 -05:00
|
|
|
|
2019-09-22 12:22:23 -04:00
|
|
|
// generateTaskNames generates names for the given tasks, and returns a copy of
|
|
|
|
// the slice with the 'Name' field set.
|
|
|
|
//
|
|
|
|
// Depending if the "--no-resolve" option is set, names have the following pattern:
|
|
|
|
//
|
|
|
|
// - ServiceName.Slot or ServiceID.Slot for tasks that are part of a replicated service
|
|
|
|
// - ServiceName.NodeName or ServiceID.NodeID for tasks that are part of a global service
|
|
|
|
//
|
|
|
|
// Task-names are not unique in cases where "tasks" contains previous/rotated tasks.
|
|
|
|
func generateTaskNames(ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver) ([]swarm.Task, error) {
|
|
|
|
// Use a copy of the tasks list, to not modify the original slice
|
|
|
|
t := append(tasks[:0:0], tasks...)
|
|
|
|
|
|
|
|
for i, task := range t {
|
|
|
|
serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2019-09-22 12:22:23 -04:00
|
|
|
if task.Slot != 0 {
|
|
|
|
t[i].Name = fmt.Sprintf("%v.%v", serviceName, task.Slot)
|
|
|
|
} else {
|
|
|
|
t[i].Name = fmt.Sprintf("%v.%v", serviceName, task.NodeID)
|
2016-11-17 01:21:18 -05:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2019-09-22 12:22:23 -04:00
|
|
|
return t, nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-07-05 13:40:08 -04:00
|
|
|
|
|
|
|
// DefaultFormat returns the default format from the config file, or table
|
|
|
|
// format if nothing is set in the config.
|
|
|
|
func DefaultFormat(configFile *configfile.ConfigFile, quiet bool) string {
|
|
|
|
if len(configFile.TasksFormat) > 0 && !quiet {
|
|
|
|
return configFile.TasksFormat
|
|
|
|
}
|
|
|
|
return formatter.TableFormatKey
|
|
|
|
}
|