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