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"
|
2016-12-06 21:52:47 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/go-units"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-12-06 21:52:47 -05:00
|
|
|
psTaskItemFmt = "%s\t%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-12-25 16:23:35 -05:00
|
|
|
func Print(dockerCli command.Cli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
sort.Stable(tasksBySlot(tasks))
|
|
|
|
|
|
|
|
writer := tabwriter.NewWriter(dockerCli.Out(), 0, 4, 2, ' ', 0)
|
|
|
|
|
|
|
|
// Ignore flushing errors
|
|
|
|
defer writer.Flush()
|
2016-12-06 21:52:47 -05:00
|
|
|
fmt.Fprintln(writer, strings.Join([]string{"ID", "NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR", "PORTS"}, "\t"))
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-12-14 16:16:12 -05:00
|
|
|
return print(writer, ctx, tasks, resolver, noTrunc)
|
2016-10-27 20:02:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrintQuiet shows task list in a quiet way.
|
2016-12-25 16:23:35 -05:00
|
|
|
func PrintQuiet(dockerCli command.Cli, tasks []swarm.Task) error {
|
2016-10-27 20:02:57 -04:00
|
|
|
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-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
|
|
|
id := task.ID
|
|
|
|
if !noTrunc {
|
|
|
|
id = stringid.TruncateID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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,
|
2016-12-06 21:52:47 -05:00
|
|
|
id,
|
2016-09-08 13:11:39 -04:00
|
|
|
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
|
|
|
|
}
|