print.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package task
  2. import (
  3. "fmt"
  4. "io"
  5. "sort"
  6. "strings"
  7. "text/tabwriter"
  8. "time"
  9. "golang.org/x/net/context"
  10. "github.com/docker/docker/api/types/swarm"
  11. "github.com/docker/docker/cli/command"
  12. "github.com/docker/docker/cli/command/idresolver"
  13. "github.com/docker/go-units"
  14. )
  15. const (
  16. psTaskItemFmt = "%s\t%s\t%s\t%s\t%s %s ago\t%s\n"
  17. maxErrLength = 30
  18. )
  19. type tasksBySlot []swarm.Task
  20. func (t tasksBySlot) Len() int {
  21. return len(t)
  22. }
  23. func (t tasksBySlot) Swap(i, j int) {
  24. t[i], t[j] = t[j], t[i]
  25. }
  26. func (t tasksBySlot) Less(i, j int) bool {
  27. // Sort by slot.
  28. if t[i].Slot != t[j].Slot {
  29. return t[i].Slot < t[j].Slot
  30. }
  31. // If same slot, sort by most recent.
  32. return t[j].Meta.CreatedAt.Before(t[i].CreatedAt)
  33. }
  34. // Print task information in a table format.
  35. // Besides this, command `docker node ps <node>`
  36. // and `docker stack ps` will call this, too.
  37. func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
  38. sort.Stable(tasksBySlot(tasks))
  39. writer := tabwriter.NewWriter(dockerCli.Out(), 0, 4, 2, ' ', 0)
  40. // Ignore flushing errors
  41. defer writer.Flush()
  42. fmt.Fprintln(writer, strings.Join([]string{"NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR"}, "\t"))
  43. if err := print(writer, ctx, tasks, resolver, noTrunc); err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. // PrintQuiet shows task list in a quiet way.
  49. func PrintQuiet(dockerCli *command.DockerCli, tasks []swarm.Task) error {
  50. sort.Stable(tasksBySlot(tasks))
  51. out := dockerCli.Out()
  52. for _, task := range tasks {
  53. fmt.Fprintln(out, task.ID)
  54. }
  55. return nil
  56. }
  57. func print(out io.Writer, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
  58. prevService := ""
  59. prevSlot := 0
  60. for _, task := range tasks {
  61. name, err := resolver.Resolve(ctx, task, task.ID)
  62. nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID)
  63. if err != nil {
  64. return err
  65. }
  66. // Indent the name if necessary
  67. indentedName := name
  68. // Since the new format of the task name is <ServiceName>.<Slot>.<taskID>, we should only compare
  69. // <ServiceName> and <Slot> here.
  70. if prevService == task.ServiceID && prevSlot == task.Slot {
  71. indentedName = fmt.Sprintf(" \\_ %s", indentedName)
  72. }
  73. prevService = task.ServiceID
  74. prevSlot = task.Slot
  75. // Trim and quote the error message.
  76. taskErr := task.Status.Err
  77. if !noTrunc && len(taskErr) > maxErrLength {
  78. taskErr = fmt.Sprintf("%s…", taskErr[:maxErrLength-1])
  79. }
  80. if len(taskErr) > 0 {
  81. taskErr = fmt.Sprintf("\"%s\"", taskErr)
  82. }
  83. fmt.Fprintf(
  84. out,
  85. psTaskItemFmt,
  86. indentedName,
  87. task.Spec.ContainerSpec.Image,
  88. nodeValue,
  89. command.PrettyPrint(task.DesiredState),
  90. command.PrettyPrint(task.Status.State),
  91. strings.ToLower(units.HumanDuration(time.Since(task.Status.Timestamp))),
  92. taskErr,
  93. )
  94. }
  95. return nil
  96. }