list.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package node
  2. import (
  3. "fmt"
  4. "io"
  5. "text/tabwriter"
  6. "golang.org/x/net/context"
  7. "github.com/docker/docker/api/client"
  8. "github.com/docker/docker/cli"
  9. "github.com/docker/docker/opts"
  10. "github.com/docker/engine-api/types"
  11. "github.com/docker/engine-api/types/swarm"
  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 *client.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 *client.DockerCli, opts listOptions) error {
  38. client := dockerCli.Client()
  39. ctx := context.Background()
  40. nodes, err := client.NodeList(
  41. ctx,
  42. types.NodeListOptions{Filter: opts.filter.Value()})
  43. if err != nil {
  44. return err
  45. }
  46. info, err := client.Info(ctx)
  47. if err != nil {
  48. return err
  49. }
  50. out := dockerCli.Out()
  51. if opts.quiet {
  52. printQuiet(out, nodes)
  53. } else {
  54. printTable(out, nodes, info)
  55. }
  56. return nil
  57. }
  58. func printTable(out io.Writer, nodes []swarm.Node, info types.Info) {
  59. writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
  60. // Ignore flushing errors
  61. defer writer.Flush()
  62. fmt.Fprintf(writer, listItemFmt, "ID", "HOSTNAME", "STATUS", "AVAILABILITY", "MANAGER STATUS")
  63. for _, node := range nodes {
  64. name := node.Description.Hostname
  65. availability := string(node.Spec.Availability)
  66. reachability := ""
  67. if node.ManagerStatus != nil {
  68. if node.ManagerStatus.Leader {
  69. reachability = "Leader"
  70. } else {
  71. reachability = string(node.ManagerStatus.Reachability)
  72. }
  73. }
  74. ID := node.ID
  75. if node.ID == info.Swarm.NodeID {
  76. ID = ID + " *"
  77. }
  78. fmt.Fprintf(
  79. writer,
  80. listItemFmt,
  81. ID,
  82. name,
  83. client.PrettyPrint(string(node.Status.State)),
  84. client.PrettyPrint(availability),
  85. client.PrettyPrint(reachability))
  86. }
  87. }
  88. func printQuiet(out io.Writer, nodes []swarm.Node) {
  89. for _, node := range nodes {
  90. fmt.Fprintln(out, node.ID)
  91. }
  92. }