list.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package node
  2. import (
  3. "fmt"
  4. "io"
  5. "text/tabwriter"
  6. "golang.org/x/net/context"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/swarm"
  9. "github.com/docker/docker/cli"
  10. "github.com/docker/docker/cli/command"
  11. "github.com/docker/docker/opts"
  12. "github.com/spf13/cobra"
  13. )
  14. const (
  15. listItemFmt = "%s\t%s\t%s\t%s\t%s\n"
  16. )
  17. type listOptions struct {
  18. quiet bool
  19. filter opts.FilterOpt
  20. }
  21. func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
  22. opts := listOptions{filter: opts.NewFilterOpt()}
  23. cmd := &cobra.Command{
  24. Use: "ls [OPTIONS]",
  25. Aliases: []string{"list"},
  26. Short: "List nodes in the swarm",
  27. Args: cli.NoArgs,
  28. RunE: func(cmd *cobra.Command, args []string) error {
  29. return runList(dockerCli, opts)
  30. },
  31. }
  32. flags := cmd.Flags()
  33. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
  34. flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
  35. return cmd
  36. }
  37. func runList(dockerCli *command.DockerCli, opts listOptions) error {
  38. client := dockerCli.Client()
  39. out := dockerCli.Out()
  40. ctx := context.Background()
  41. nodes, err := client.NodeList(
  42. ctx,
  43. types.NodeListOptions{Filters: opts.filter.Value()})
  44. if err != nil {
  45. return err
  46. }
  47. if len(nodes) > 0 && !opts.quiet {
  48. // only non-empty nodes and not quiet, should we call /info api
  49. info, err := client.Info(ctx)
  50. if err != nil {
  51. return err
  52. }
  53. printTable(out, nodes, info)
  54. } else if !opts.quiet {
  55. // no nodes and not quiet, print only one line with columns ID, HOSTNAME, ...
  56. printTable(out, nodes, types.Info{})
  57. } else {
  58. printQuiet(out, nodes)
  59. }
  60. return nil
  61. }
  62. func printTable(out io.Writer, nodes []swarm.Node, info types.Info) {
  63. writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
  64. // Ignore flushing errors
  65. defer writer.Flush()
  66. fmt.Fprintf(writer, listItemFmt, "ID", "HOSTNAME", "STATUS", "AVAILABILITY", "MANAGER STATUS")
  67. for _, node := range nodes {
  68. name := node.Description.Hostname
  69. availability := string(node.Spec.Availability)
  70. reachability := ""
  71. if node.ManagerStatus != nil {
  72. if node.ManagerStatus.Leader {
  73. reachability = "Leader"
  74. } else {
  75. reachability = string(node.ManagerStatus.Reachability)
  76. }
  77. }
  78. ID := node.ID
  79. if node.ID == info.Swarm.NodeID {
  80. ID = ID + " *"
  81. }
  82. fmt.Fprintf(
  83. writer,
  84. listItemFmt,
  85. ID,
  86. name,
  87. command.PrettyPrint(string(node.Status.State)),
  88. command.PrettyPrint(availability),
  89. command.PrettyPrint(reachability))
  90. }
  91. }
  92. func printQuiet(out io.Writer, nodes []swarm.Node) {
  93. for _, node := range nodes {
  94. fmt.Fprintln(out, node.ID)
  95. }
  96. }