print.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. distreference "github.com/docker/distribution/reference"
  11. "github.com/docker/docker/api/types/swarm"
  12. "github.com/docker/docker/cli/command"
  13. "github.com/docker/docker/cli/command/idresolver"
  14. "github.com/docker/go-units"
  15. )
  16. const (
  17. psTaskItemFmt = "%s\t%s\t%s\t%s\t%s %s ago\t%s\t%s\n"
  18. maxErrLength = 30
  19. )
  20. type portStatus swarm.PortStatus
  21. func (ps portStatus) String() string {
  22. if len(ps.Ports) == 0 {
  23. return ""
  24. }
  25. str := fmt.Sprintf("*:%d->%d/%s", ps.Ports[0].PublishedPort, ps.Ports[0].TargetPort, ps.Ports[0].Protocol)
  26. for _, pConfig := range ps.Ports[1:] {
  27. str += fmt.Sprintf(",*:%d->%d/%s", pConfig.PublishedPort, pConfig.TargetPort, pConfig.Protocol)
  28. }
  29. return str
  30. }
  31. type tasksBySlot []swarm.Task
  32. func (t tasksBySlot) Len() int {
  33. return len(t)
  34. }
  35. func (t tasksBySlot) Swap(i, j int) {
  36. t[i], t[j] = t[j], t[i]
  37. }
  38. func (t tasksBySlot) Less(i, j int) bool {
  39. // Sort by slot.
  40. if t[i].Slot != t[j].Slot {
  41. return t[i].Slot < t[j].Slot
  42. }
  43. // If same slot, sort by most recent.
  44. return t[j].Meta.CreatedAt.Before(t[i].CreatedAt)
  45. }
  46. // Print task information in a table format.
  47. // Besides this, command `docker node ps <node>`
  48. // and `docker stack ps` will call this, too.
  49. func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
  50. sort.Stable(tasksBySlot(tasks))
  51. writer := tabwriter.NewWriter(dockerCli.Out(), 0, 4, 2, ' ', 0)
  52. // Ignore flushing errors
  53. defer writer.Flush()
  54. fmt.Fprintln(writer, strings.Join([]string{"NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR", "PORTS"}, "\t"))
  55. if err := print(writer, ctx, tasks, resolver, noTrunc); err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. // PrintQuiet shows task list in a quiet way.
  61. func PrintQuiet(dockerCli *command.DockerCli, tasks []swarm.Task) error {
  62. sort.Stable(tasksBySlot(tasks))
  63. out := dockerCli.Out()
  64. for _, task := range tasks {
  65. fmt.Fprintln(out, task.ID)
  66. }
  67. return nil
  68. }
  69. func print(out io.Writer, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
  70. prevService := ""
  71. prevSlot := 0
  72. for _, task := range tasks {
  73. name, err := resolver.Resolve(ctx, task, task.ID)
  74. nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID)
  75. if err != nil {
  76. return err
  77. }
  78. // Indent the name if necessary
  79. indentedName := name
  80. // Since the new format of the task name is <ServiceName>.<Slot>.<taskID>, we should only compare
  81. // <ServiceName> and <Slot> here.
  82. if prevService == task.ServiceID && prevSlot == task.Slot {
  83. indentedName = fmt.Sprintf(" \\_ %s", indentedName)
  84. }
  85. prevService = task.ServiceID
  86. prevSlot = task.Slot
  87. // Trim and quote the error message.
  88. taskErr := task.Status.Err
  89. if !noTrunc && len(taskErr) > maxErrLength {
  90. taskErr = fmt.Sprintf("%s…", taskErr[:maxErrLength-1])
  91. }
  92. if len(taskErr) > 0 {
  93. taskErr = fmt.Sprintf("\"%s\"", taskErr)
  94. }
  95. image := task.Spec.ContainerSpec.Image
  96. if !noTrunc {
  97. ref, err := distreference.ParseNamed(image)
  98. if err == nil {
  99. // update image string for display
  100. namedTagged, ok := ref.(distreference.NamedTagged)
  101. if ok {
  102. image = namedTagged.Name() + ":" + namedTagged.Tag()
  103. }
  104. }
  105. }
  106. fmt.Fprintf(
  107. out,
  108. psTaskItemFmt,
  109. indentedName,
  110. image,
  111. nodeValue,
  112. command.PrettyPrint(task.DesiredState),
  113. command.PrettyPrint(task.Status.State),
  114. strings.ToLower(units.HumanDuration(time.Since(task.Status.Timestamp))),
  115. taskErr,
  116. portStatus(task.Status.PortStatus),
  117. )
  118. }
  119. return nil
  120. }