inspect.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package node
  2. import (
  3. "fmt"
  4. "io"
  5. "sort"
  6. "strings"
  7. "github.com/docker/docker/api/types/swarm"
  8. "github.com/docker/docker/cli"
  9. "github.com/docker/docker/cli/command"
  10. "github.com/docker/docker/cli/command/inspect"
  11. "github.com/docker/docker/pkg/ioutils"
  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 *command.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 *command.DockerCli, opts inspectOptions) error {
  38. client := dockerCli.Client()
  39. ctx := context.Background()
  40. getRef := func(ref string) (interface{}, []byte, error) {
  41. nodeRef, err := Reference(ctx, client, 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. } else {
  65. fmt.Fprintf(out, "\n")
  66. }
  67. }
  68. return nil
  69. }
  70. // TODO: use a template
  71. func printNode(out io.Writer, node swarm.Node) {
  72. fmt.Fprintf(out, "ID:\t\t\t%s\n", node.ID)
  73. ioutils.FprintfIfNotEmpty(out, "Name:\t\t\t%s\n", node.Spec.Name)
  74. if node.Spec.Labels != nil {
  75. fmt.Fprintln(out, "Labels:")
  76. for k, v := range node.Spec.Labels {
  77. fmt.Fprintf(out, " - %s = %s\n", k, v)
  78. }
  79. }
  80. ioutils.FprintfIfNotEmpty(out, "Hostname:\t\t%s\n", node.Description.Hostname)
  81. fmt.Fprintf(out, "Joined at:\t\t%s\n", command.PrettyPrint(node.CreatedAt))
  82. fmt.Fprintln(out, "Status:")
  83. fmt.Fprintf(out, " State:\t\t\t%s\n", command.PrettyPrint(node.Status.State))
  84. ioutils.FprintfIfNotEmpty(out, " Message:\t\t%s\n", command.PrettyPrint(node.Status.Message))
  85. fmt.Fprintf(out, " Availability:\t\t%s\n", command.PrettyPrint(node.Spec.Availability))
  86. if node.ManagerStatus != nil {
  87. fmt.Fprintln(out, "Manager Status:")
  88. fmt.Fprintf(out, " Address:\t\t%s\n", node.ManagerStatus.Addr)
  89. fmt.Fprintf(out, " Raft Status:\t\t%s\n", command.PrettyPrint(node.ManagerStatus.Reachability))
  90. leader := "No"
  91. if node.ManagerStatus.Leader {
  92. leader = "Yes"
  93. }
  94. fmt.Fprintf(out, " Leader:\t\t%s\n", leader)
  95. }
  96. fmt.Fprintln(out, "Platform:")
  97. fmt.Fprintf(out, " Operating System:\t%s\n", node.Description.Platform.OS)
  98. fmt.Fprintf(out, " Architecture:\t\t%s\n", node.Description.Platform.Architecture)
  99. fmt.Fprintln(out, "Resources:")
  100. fmt.Fprintf(out, " CPUs:\t\t\t%d\n", node.Description.Resources.NanoCPUs/1e9)
  101. fmt.Fprintf(out, " Memory:\t\t%s\n", units.BytesSize(float64(node.Description.Resources.MemoryBytes)))
  102. var pluginTypes []string
  103. pluginNamesByType := map[string][]string{}
  104. for _, p := range node.Description.Engine.Plugins {
  105. // append to pluginTypes only if not done previously
  106. if _, ok := pluginNamesByType[p.Type]; !ok {
  107. pluginTypes = append(pluginTypes, p.Type)
  108. }
  109. pluginNamesByType[p.Type] = append(pluginNamesByType[p.Type], p.Name)
  110. }
  111. if len(pluginTypes) > 0 {
  112. fmt.Fprintln(out, "Plugins:")
  113. sort.Strings(pluginTypes) // ensure stable output
  114. for _, pluginType := range pluginTypes {
  115. fmt.Fprintf(out, " %s:\t\t%s\n", pluginType, strings.Join(pluginNamesByType[pluginType], ", "))
  116. }
  117. }
  118. fmt.Fprintf(out, "Engine Version:\t\t%s\n", node.Description.Engine.EngineVersion)
  119. if len(node.Description.Engine.Labels) != 0 {
  120. fmt.Fprintln(out, "Engine Labels:")
  121. for k, v := range node.Description.Engine.Labels {
  122. fmt.Fprintf(out, " - %s = %s\n", k, v)
  123. }
  124. }
  125. }