-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
docker_info.go
113 lines (96 loc) · 2.98 KB
/
docker_info.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
package appui
import (
"bytes"
"strconv"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/go-units"
termui "github.com/gizak/termui"
"github.com/moncho/dry/docker"
"github.com/moncho/dry/ui"
drytermui "github.com/moncho/dry/ui/termui"
"github.com/olekukonko/tablewriter"
)
// DockerInfo is a widget to show Docker info
type DockerInfo struct {
drytermui.SizableBufferer
}
// NewDockerInfo creates a DockerInfo widget
func NewDockerInfo(daemon docker.ContainerDaemon) *DockerInfo {
di := drytermui.NewParFromMarkupText(DryTheme, dockerInfo(daemon))
di.BorderTop = false
di.BorderBottom = true
di.BorderLeft = false
di.BorderRight = false
di.BorderFg = termui.Attribute(DryTheme.Footer)
di.BorderBg = termui.Attribute(DryTheme.Bg)
di.Height = 4
di.Bg = termui.Attribute(DryTheme.Bg)
di.TextBgColor = termui.Attribute(DryTheme.Bg)
di.Display = false
return &DockerInfo{di}
}
func dockerInfo(daemon docker.ContainerDaemon) string {
version, _ := daemon.Version()
info, _ := daemon.Info()
swarmInfo := info.Swarm
buffer := new(bytes.Buffer)
rows := [][]string{
{
ui.Blue("Docker Host:"), ui.Yellow(daemon.DockerEnv().DockerHost), "",
ui.Blue("Docker Version:"), ui.Yellow(version.Version)},
{
ui.Blue("Cert Path:"), ui.Yellow(daemon.DockerEnv().DockerCertPath), "",
ui.Blue("APIVersion:"), ui.Yellow(version.APIVersion)},
{
ui.Blue("Verify Certificate:"), ui.Yellow(strconv.FormatBool(daemon.DockerEnv().DockerTLSVerify)), "",
ui.Blue("OS/Arch/Kernel:"), ui.Yellow(version.Os + "/" + version.Arch + "/" + version.KernelVersion)},
}
rows = addHostInfo(rows, info)
rows = addSwarmInfo(rows, swarmInfo)
table := tablewriter.NewWriter(buffer)
table.SetBorder(false)
table.SetColumnSeparator("")
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.AppendBulk(rows)
table.Render()
return buffer.String()
}
func addSwarmInfo(rows [][]string, info swarm.Info) [][]string {
firstRow := rows[0]
secondRow := rows[1]
thirdRow := rows[2]
firstRow = append(firstRow,
ui.Blue("Swarm:"),
ui.Yellow(string(info.LocalNodeState)))
if info.LocalNodeState != swarm.LocalNodeStateInactive {
if info.ControlAvailable {
secondRow = append(secondRow,
ui.Blue("Node role:"),
ui.Yellow(string(swarm.NodeRoleManager)))
} else {
secondRow = append(secondRow,
ui.Blue("Node role:"),
ui.Yellow(string(swarm.NodeRoleWorker)))
}
thirdRow = append(thirdRow,
ui.Blue("Nodes:"),
ui.Yellow(strconv.Itoa(info.Nodes)))
}
return [][]string{firstRow, secondRow, thirdRow}
}
func addHostInfo(rows [][]string, info types.Info) [][]string {
firstRow := rows[0]
secondRow := rows[1]
thirdRow := rows[2]
firstRow = append(firstRow,
ui.Blue("Hostname:"),
ui.Yellow(info.Name))
secondRow = append(secondRow,
ui.Blue("CPU:"),
ui.Yellow(strconv.Itoa(info.NCPU)))
thirdRow = append(thirdRow,
ui.Blue("Memory:"),
ui.Yellow(units.BytesSize(float64(info.MemTotal))))
return [][]string{firstRow, secondRow, thirdRow}
}