print.go 2.8 KB

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