2016-09-08 13:11:39 -04:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-10-27 20:02:57 -04:00
|
|
|
"io"
|
2016-09-08 13:11:39 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-11-17 01:21:18 -05:00
|
|
|
distreference "github.com/docker/distribution/reference"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/cli/command/idresolver"
|
|
|
|
"github.com/docker/go-units"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-11-10 15:13:26 -05:00
|
|
|
psTaskItemFmt = "%s\t%s\t%s\t%s\t%s %s ago\t%s\t%s\n"
|
2016-09-08 13:11:39 -04:00
|
|
|
maxErrLength = 30
|
|
|
|
)
|
|
|
|
|
2016-11-10 15:13:26 -05:00
|
|
|
type portStatus swarm.PortStatus
|
|
|
|
|
|
|
|
func (ps portStatus) String() string {
|
|
|
|
if len(ps.Ports) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
str := fmt.Sprintf("*:%d->%d/%s", ps.Ports[0].PublishedPort, ps.Ports[0].TargetPort, ps.Ports[0].Protocol)
|
|
|
|
for _, pConfig := range ps.Ports[1:] {
|
|
|
|
str += fmt.Sprintf(",*:%d->%d/%s", pConfig.PublishedPort, pConfig.TargetPort, pConfig.Protocol)
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -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)
|
|
|
|
}
|
|
|
|
|
2016-10-27 20:02:57 -04:00
|
|
|
// Print task information in a table format.
|
|
|
|
// Besides this, command `docker node ps <node>`
|
|
|
|
// and `docker stack ps` will call this, too.
|
2016-09-08 13:11:39 -04:00
|
|
|
func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
|
|
|
|
sort.Stable(tasksBySlot(tasks))
|
|
|
|
|
|
|
|
writer := tabwriter.NewWriter(dockerCli.Out(), 0, 4, 2, ' ', 0)
|
|
|
|
|
|
|
|
// Ignore flushing errors
|
|
|
|
defer writer.Flush()
|
2016-11-10 15:13:26 -05:00
|
|
|
fmt.Fprintln(writer, strings.Join([]string{"NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR", "PORTS"}, "\t"))
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-10-27 20:02:57 -04:00
|
|
|
if err := print(writer, ctx, tasks, resolver, noTrunc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintQuiet shows task list in a quiet way.
|
|
|
|
func PrintQuiet(dockerCli *command.DockerCli, tasks []swarm.Task) error {
|
|
|
|
sort.Stable(tasksBySlot(tasks))
|
|
|
|
|
|
|
|
out := dockerCli.Out()
|
|
|
|
|
|
|
|
for _, task := range tasks {
|
|
|
|
fmt.Fprintln(out, task.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func print(out io.Writer, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
|
2016-11-04 22:23:07 -04:00
|
|
|
prevService := ""
|
2016-07-23 12:58:58 -04:00
|
|
|
prevSlot := 0
|
2016-09-08 13:11:39 -04:00
|
|
|
for _, task := range tasks {
|
2016-11-04 22:23:07 -04:00
|
|
|
name, err := resolver.Resolve(ctx, task, task.ID)
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indent the name if necessary
|
|
|
|
indentedName := name
|
2016-07-23 12:58:58 -04:00
|
|
|
// Since the new format of the task name is <ServiceName>.<Slot>.<taskID>, we should only compare
|
|
|
|
// <ServiceName> and <Slot> here.
|
2016-11-04 22:23:07 -04:00
|
|
|
if prevService == task.ServiceID && prevSlot == task.Slot {
|
2016-09-08 13:11:39 -04:00
|
|
|
indentedName = fmt.Sprintf(" \\_ %s", indentedName)
|
|
|
|
}
|
2016-11-04 22:23:07 -04:00
|
|
|
prevService = task.ServiceID
|
2016-07-23 12:58:58 -04:00
|
|
|
prevSlot = task.Slot
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
// Trim and quote the error message.
|
|
|
|
taskErr := task.Status.Err
|
|
|
|
if !noTrunc && len(taskErr) > maxErrLength {
|
|
|
|
taskErr = fmt.Sprintf("%s…", taskErr[:maxErrLength-1])
|
|
|
|
}
|
|
|
|
if len(taskErr) > 0 {
|
|
|
|
taskErr = fmt.Sprintf("\"%s\"", taskErr)
|
|
|
|
}
|
|
|
|
|
2016-11-17 01:21:18 -05:00
|
|
|
image := task.Spec.ContainerSpec.Image
|
|
|
|
if !noTrunc {
|
|
|
|
ref, err := distreference.ParseNamed(image)
|
|
|
|
if err == nil {
|
|
|
|
// update image string for display
|
|
|
|
namedTagged, ok := ref.(distreference.NamedTagged)
|
|
|
|
if ok {
|
|
|
|
image = namedTagged.Name() + ":" + namedTagged.Tag()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
fmt.Fprintf(
|
2016-10-27 20:02:57 -04:00
|
|
|
out,
|
2016-09-08 13:11:39 -04:00
|
|
|
psTaskItemFmt,
|
|
|
|
indentedName,
|
2016-11-17 01:21:18 -05:00
|
|
|
image,
|
2016-09-08 13:11:39 -04:00
|
|
|
nodeValue,
|
|
|
|
command.PrettyPrint(task.DesiredState),
|
|
|
|
command.PrettyPrint(task.Status.State),
|
|
|
|
strings.ToLower(units.HumanDuration(time.Since(task.Status.Timestamp))),
|
|
|
|
taskErr,
|
2016-11-10 15:13:26 -05:00
|
|
|
portStatus(task.Status.PortStatus),
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|