Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit fe4d0f2

Browse files
author
wuqixuan
committed
fleetctl: list-unit-files fully populated for global units
Now, fleetctl list-unit-files command can show exact number of the machines running the unit in STATE column as below: localhost # fleetctl list-unit-files UNIT HASH DSTATE STATE TARGET world_glob.service 66439c2 launched launched(2) global Fixes #1072
1 parent d3a2a6d commit fe4d0f2

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

fleetctl/list_unit_files.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,45 @@ var (
6868
"target": mapTargetField,
6969
"tmachine": mapTargetField,
7070
"state": func(u schema.Unit, full bool) string {
71-
if suToGlobal(u) || u.CurrentState == "" {
71+
if suToGlobal(u) {
72+
states, err := cAPI.UnitStates()
73+
if err != nil {
74+
return "-"
75+
}
76+
var (
77+
inactive int
78+
loaded int
79+
launched int
80+
currentStates []string
81+
)
82+
for _, state := range states {
83+
if state.Name == u.Name {
84+
if (state.SystemdActiveState == "active") || (state.SystemdSubState == "running") {
85+
launched++
86+
continue
87+
}
88+
if state.SystemdLoadState == "loaded" {
89+
loaded++
90+
continue
91+
}
92+
inactive++
93+
}
94+
}
95+
if launched != 0 {
96+
s := fmt.Sprintf("launched(%d)", launched)
97+
currentStates = append(currentStates, s)
98+
}
99+
if loaded != 0 {
100+
s := fmt.Sprintf("loaded(%d)", loaded)
101+
currentStates = append(currentStates, s)
102+
}
103+
if inactive != 0 {
104+
s := fmt.Sprintf("inactive(%d)", inactive)
105+
currentStates = append(currentStates, s)
106+
}
107+
return strings.Join(currentStates, " ")
108+
}
109+
if u.CurrentState == "" {
72110
return "-"
73111
}
74112
return u.CurrentState

0 commit comments

Comments
 (0)