forked from moby/swarmkit
/
inspect.go
145 lines (126 loc) · 3.82 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
144
145
package node
import (
"errors"
"fmt"
"os"
"sort"
"text/tabwriter"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/cmd/swarmctl/common"
"github.com/docker/swarmkit/cmd/swarmctl/task"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)
func printNodeSummary(node *api.Node) {
w := tabwriter.NewWriter(os.Stdout, 8, 8, 8, ' ', 0)
defer func() {
// Ignore flushing errors - there's nothing we can do.
_ = w.Flush()
}()
spec := &node.Spec
desc := node.Description
if desc == nil {
desc = &api.NodeDescription{}
}
common.FprintfIfNotEmpty(w, "ID\t: %s\n", node.ID)
common.FprintfIfNotEmpty(w, "Name\t: %s\n", spec.Annotations.Name)
if node.Description != nil {
common.FprintfIfNotEmpty(w, "Hostname\t: %s\n", node.Description.Hostname)
}
fmt.Fprintln(w, "Status:\t")
common.FprintfIfNotEmpty(w, " State\t: %s\n", node.Status.State.String())
common.FprintfIfNotEmpty(w, " Message\t: %s\n", node.Status.Message)
common.FprintfIfNotEmpty(w, " Availability\t: %s\n", spec.Availability.String())
if node.Manager != nil {
fmt.Fprintln(w, "Manager:\t")
common.FprintfIfNotEmpty(w, " Address\t: %s\n", node.Manager.Raft.Addr)
common.FprintfIfNotEmpty(w, " Raft status\t: %s\n", node.Manager.Raft.Status.Reachability.String())
leader := "no"
if node.Manager.Raft.Status.Leader {
leader = "yes"
}
common.FprintfIfNotEmpty(w, " Leader\t: %s\n", leader)
}
if desc.Platform != nil {
fmt.Fprintln(w, "Platform:\t")
common.FprintfIfNotEmpty(w, " Operating System\t: %s\n", desc.Platform.OS)
common.FprintfIfNotEmpty(w, " Architecture\t: %s\n", desc.Platform.Architecture)
}
if desc.Resources != nil {
fmt.Fprintln(w, "Resources:\t")
fmt.Fprintf(w, " CPUs\t: %d\n", desc.Resources.NanoCPUs/1e9)
fmt.Fprintf(w, " Memory\t: %s\n", humanize.IBytes(uint64(desc.Resources.MemoryBytes)))
if node.Description != nil {
common.FprintfIfNotEmpty(w, "Hostname\t: %s\n", node.Description.Hostname)
}
}
if desc.Engine != nil {
fmt.Fprintln(w, "Plugins:\t")
var pluginTypes []string
pluginNamesByType := map[string][]string{}
for _, p := range desc.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)
}
sort.Strings(pluginTypes) // ensure stable output
for _, pluginType := range pluginTypes {
fmt.Fprintf(w, " %s\t: %v\n", pluginType, pluginNamesByType[pluginType])
}
}
if desc.Engine != nil {
common.FprintfIfNotEmpty(w, "Engine Version\t: %s\n", desc.Engine.EngineVersion)
if len(desc.Engine.Labels) != 0 {
fmt.Fprintln(w, "Engine Labels:\t")
for k, v := range desc.Engine.Labels {
fmt.Fprintf(w, " %s = %s", k, v)
}
}
}
}
var (
inspectCmd = &cobra.Command{
Use: "inspect <node ID>",
Short: "Inspect a node",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("node ID missing")
}
flags := cmd.Flags()
all, err := flags.GetBool("all")
if err != nil {
return err
}
c, err := common.Dial(cmd)
if err != nil {
return err
}
node, err := getNode(common.Context(cmd), c, args[0])
if err != nil {
return err
}
// TODO(aluzzardi): This should be implemented as a ListOptions filter.
r, err := c.ListTasks(common.Context(cmd), &api.ListTasksRequest{})
if err != nil {
return err
}
tasks := []*api.Task{}
for _, t := range r.Tasks {
if t.NodeID == node.ID {
tasks = append(tasks, t)
}
}
printNodeSummary(node)
if len(tasks) > 0 {
fmt.Printf("\n")
task.Print(tasks, all, common.NewResolver(cmd, c))
}
return nil
},
}
)
func init() {
inspectCmd.Flags().BoolP("all", "a", false, "Show all tasks (default shows just running)")
}