From 37e59a20c5384cfcf75f304c32513d2727019855 Mon Sep 17 00:00:00 2001 From: Dongsu Park Date: Mon, 2 May 2016 14:13:24 +0200 Subject: [PATCH] fleetctl: list-unit-files fully populated for global units Make "fleetctl list-unit-files" able to 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 Supersedes https://github.com/coreos/fleet/pull/1272 Fixes: https://github.com/coreos/fleet/issues/1072 --- fleetctl/list_unit_files.go | 52 ++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/fleetctl/list_unit_files.go b/fleetctl/list_unit_files.go index 8599d0c22..fc1ed8e9b 100644 --- a/fleetctl/list_unit_files.go +++ b/fleetctl/list_unit_files.go @@ -43,6 +43,51 @@ func mapTargetField(u schema.Unit, full bool) string { return machineFullLegend(*ms, full) } +func mapStateField(u schema.Unit, full bool) string { + if suToGlobal(u) { + states, err := cAPI.UnitStates() + if err != nil { + return "-" + } + var ( + inactive int + loaded int + launched int + currentStates []string + ) + for _, state := range states { + if state.Name == u.Name { + if (state.SystemdActiveState == "active") || (state.SystemdSubState == "running") { + launched++ + continue + } + if state.SystemdLoadState == "loaded" { + loaded++ + continue + } + inactive++ + } + } + if launched != 0 { + s := fmt.Sprintf("launched(%d)", launched) + currentStates = append(currentStates, s) + } + if loaded != 0 { + s := fmt.Sprintf("loaded(%d)", loaded) + currentStates = append(currentStates, s) + } + if inactive != 0 { + s := fmt.Sprintf("inactive(%d)", inactive) + currentStates = append(currentStates, s) + } + return strings.Join(currentStates, " ") + } + if u.CurrentState == "" { + return "-" + } + return u.CurrentState +} + var ( listUnitFilesFieldsFlag string cmdListUnitFiles = &Command{ @@ -67,12 +112,7 @@ var ( }, "target": mapTargetField, "tmachine": mapTargetField, - "state": func(u schema.Unit, full bool) string { - if suToGlobal(u) || u.CurrentState == "" { - return "-" - } - return u.CurrentState - }, + "state": mapStateField, "hash": func(u schema.Unit, full bool) string { uf := schema.MapSchemaUnitOptionsToUnitFile(u.Options) if !full {