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

Commit 02cbb7e

Browse files
author
Dongsu Park
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 Originally written by wuqixuan <wuqixuan@huawei.com> Supersedes #1272 Fixes: #1072
1 parent da56590 commit 02cbb7e

1 file changed

Lines changed: 46 additions & 6 deletions

File tree

fleetctl/list_unit_files.go

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,51 @@ func mapTargetField(u schema.Unit, full bool) string {
4343
return machineFullLegend(*ms, full)
4444
}
4545

46+
func mapStateField(u schema.Unit, full bool) string {
47+
if suToGlobal(u) {
48+
states, err := cAPI.UnitStates()
49+
if err != nil {
50+
return "-"
51+
}
52+
var (
53+
inactive int
54+
loaded int
55+
launched int
56+
currentStates []string
57+
)
58+
for _, state := range states {
59+
if state.Name == u.Name {
60+
if (state.SystemdActiveState == "active") || (state.SystemdSubState == "running") {
61+
launched++
62+
continue
63+
}
64+
if state.SystemdLoadState == "loaded" {
65+
loaded++
66+
continue
67+
}
68+
inactive++
69+
}
70+
}
71+
if launched != 0 {
72+
s := fmt.Sprintf("launched(%d)", launched)
73+
currentStates = append(currentStates, s)
74+
}
75+
if loaded != 0 {
76+
s := fmt.Sprintf("loaded(%d)", loaded)
77+
currentStates = append(currentStates, s)
78+
}
79+
if inactive != 0 {
80+
s := fmt.Sprintf("inactive(%d)", inactive)
81+
currentStates = append(currentStates, s)
82+
}
83+
return strings.Join(currentStates, " ")
84+
}
85+
if u.CurrentState == "" {
86+
return "-"
87+
}
88+
return u.CurrentState
89+
}
90+
4691
var (
4792
listUnitFilesFieldsFlag string
4893
cmdListUnitFiles = &Command{
@@ -67,12 +112,7 @@ var (
67112
},
68113
"target": mapTargetField,
69114
"tmachine": mapTargetField,
70-
"state": func(u schema.Unit, full bool) string {
71-
if suToGlobal(u) || u.CurrentState == "" {
72-
return "-"
73-
}
74-
return u.CurrentState
75-
},
115+
"state": mapStateField,
76116
"hash": func(u schema.Unit, full bool) string {
77117
uf := schema.MapSchemaUnitOptionsToUnitFile(u.Options)
78118
if !full {

0 commit comments

Comments
 (0)