inspect.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package node
  2. import (
  3. "fmt"
  4. "io"
  5. "sort"
  6. "strings"
  7. "github.com/docker/docker/api/client"
  8. "github.com/docker/docker/api/client/inspect"
  9. "github.com/docker/docker/cli"
  10. "github.com/docker/docker/pkg/ioutils"
  11. "github.com/docker/engine-api/types/swarm"
  12. "github.com/docker/go-units"
  13. "github.com/spf13/cobra"
  14. "golang.org/x/net/context"
  15. )
  16. type inspectOptions struct {
  17. nodeIds []string
  18. format string
  19. pretty bool
  20. }
  21. func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
  22. var opts inspectOptions
  23. cmd := &cobra.Command{
  24. Use: "inspect [OPTIONS] self|NODE [NODE...]",
  25. Short: "Display detailed information on one or more nodes",
  26. Args: cli.RequiresMinArgs(1),
  27. RunE: func(cmd *cobra.Command, args []string) error {
  28. opts.nodeIds = args
  29. return runInspect(dockerCli, opts)
  30. },
  31. }
  32. flags := cmd.Flags()
  33. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
  34. flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format.")
  35. return cmd
  36. }
  37. func runInspect(dockerCli *client.DockerCli, opts inspectOptions) error {
  38. client := dockerCli.Client()
  39. ctx := context.Background()
  40. getRef := func(ref string) (interface{}, []byte, error) {
  41. nodeRef, err := Reference(client, ctx, ref)
  42. if err != nil {
  43. return nil, nil, err
  44. }
  45. node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
  46. return node, nil, err
  47. }
  48. if !opts.pretty {
  49. return inspect.Inspect(dockerCli.Out(), opts.nodeIds, opts.format, getRef)
  50. }
  51. return printHumanFriendly(dockerCli.Out(), opts.nodeIds, getRef)
  52. }
  53. func printHumanFriendly(out io.Writer, refs []string, getRef inspect.GetRefFunc) error {
  54. for idx, ref := range refs {
  55. obj, _, err := getRef(ref)
  56. if err != nil {
  57. return err
  58. }
  59. printNode(out, obj.(swarm.Node))
  60. // TODO: better way to do this?
  61. // print extra space between objects, but not after the last one
  62. if idx+1 != len(refs) {
  63. fmt.Fprintf(out, "\n\n")
  64. }
  65. }
  66. return nil
  67. }
  68. // TODO: use a template
  69. func printNode(out io.Writer, node swarm.Node) {
  70. fmt.Fprintf(out, "ID:\t\t\t%s\n", node.ID)
  71. ioutils.FprintfIfNotEmpty(out, "Name:\t\t\t%s\n", node.Spec.Name)
  72. if node.Spec.Labels != nil {
  73. fmt.Fprintln(out, "Labels:")
  74. for k, v := range node.Spec.Labels {
  75. fmt.Fprintf(out, " - %s = %s\n", k, v)
  76. }
  77. }
  78. ioutils.FprintfIfNotEmpty(out, "Hostname:\t\t%s\n", node.Description.Hostname)
  79. fmt.Fprintln(out, "Status:")
  80. fmt.Fprintf(out, " State:\t\t\t%s\n", client.PrettyPrint(node.Status.State))
  81. ioutils.FprintfIfNotEmpty(out, " Message:\t\t%s\n", client.PrettyPrint(node.Status.Message))
  82. fmt.Fprintf(out, " Availability:\t\t%s\n", client.PrettyPrint(node.Spec.Availability))
  83. if node.ManagerStatus != nil {
  84. fmt.Fprintln(out, "Manager Status:")
  85. fmt.Fprintf(out, " Address:\t\t%s\n", node.ManagerStatus.Addr)
  86. fmt.Fprintf(out, " Raft Status:\t\t%s\n", client.PrettyPrint(node.ManagerStatus.Reachability))
  87. leader := "No"
  88. if node.ManagerStatus.Leader {
  89. leader = "Yes"
  90. }
  91. fmt.Fprintf(out, " Leader:\t\t%s\n", leader)
  92. }
  93. fmt.Fprintln(out, "Platform:")
  94. fmt.Fprintf(out, " Operating System:\t%s\n", node.Description.Platform.OS)
  95. fmt.Fprintf(out, " Architecture:\t\t%s\n", node.Description.Platform.Architecture)
  96. fmt.Fprintln(out, "Resources:")
  97. fmt.Fprintf(out, " CPUs:\t\t\t%d\n", node.Description.Resources.NanoCPUs/1e9)
  98. fmt.Fprintf(out, " Memory:\t\t%s\n", units.BytesSize(float64(node.Description.Resources.MemoryBytes)))
  99. var pluginTypes []string
  100. pluginNamesByType := map[string][]string{}
  101. for _, p := range node.Description.Engine.Plugins {
  102. // append to pluginTypes only if not done previously
  103. if _, ok := pluginNamesByType[p.Type]; !ok {
  104. pluginTypes = append(pluginTypes, p.Type)
  105. }
  106. pluginNamesByType[p.Type] = append(pluginNamesByType[p.Type], p.Name)
  107. }
  108. if len(pluginTypes) > 0 {
  109. fmt.Fprintln(out, "Plugins:")
  110. sort.Strings(pluginTypes) // ensure stable output
  111. for _, pluginType := range pluginTypes {
  112. fmt.Fprintf(out, " %s:\t\t%s\n", pluginType, strings.Join(pluginNamesByType[pluginType], ", "))
  113. }
  114. }
  115. fmt.Fprintf(out, "Engine Version:\t\t%s\n", node.Description.Engine.EngineVersion)
  116. if len(node.Description.Engine.Labels) != 0 {
  117. fmt.Fprintln(out, "Engine Labels:")
  118. for k, v := range node.Description.Engine.Labels {
  119. fmt.Fprintf(out, " - %s = %s", k, v)
  120. }
  121. }
  122. }