forked from coreos/fleet
/
list_units.go
162 lines (139 loc) · 3.69 KB
/
list_units.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"os"
"sort"
"strings"
"github.com/coreos/fleet/job"
)
const (
defaultListUnitsFields = "unit,state,load,active,sub,desc,machine"
)
var (
cmdListUnits = &Command{
Name: "list-units",
Summary: "Enumerate units loaded in the cluster",
Usage: "[--no-legend] [-l|--full]",
Description: `Lists all units submitted or started on the cluster.
For easily parsable output, you can remove the column headers:
fleetctl list-units --no-legend
Output the list without ellipses:
fleetctl list-units --full
Or, choose the columns to display:
fleetctl list-units --fields=unit,machine`,
Run: runListUnits,
}
fieldsToOutput = map[string]jobToField{
"unit": func(j *job.Job, full bool) string {
return j.Name
},
"state": func(j *job.Job, full bool) string {
js := j.State
if js != nil {
return string(*js)
}
return "-"
},
"load": func(j *job.Job, full bool) string {
us := j.UnitState
if us == nil {
return "-"
}
return us.LoadState
},
"active": func(j *job.Job, full bool) string {
us := j.UnitState
if us == nil {
return "-"
}
return us.ActiveState
},
"sub": func(j *job.Job, full bool) string {
us := j.UnitState
if us == nil {
return "-"
}
return us.SubState
},
"desc": func(j *job.Job, full bool) string {
d := j.Unit.Description()
if d == "" {
return "-"
}
return d
},
"machine": func(j *job.Job, full bool) string {
us := j.UnitState
if us == nil || us.MachineState == nil {
return "-"
}
return machineFullLegend(*us.MachineState, full)
},
"hash": func(j *job.Job, full bool) string {
if !full {
return j.UnitHash.Short()
}
return j.UnitHash.String()
},
}
)
type jobToField func(j *job.Job, full bool) string
func init() {
cmdListUnits.Flags.BoolVar(&sharedFlags.Full, "full", false, "Do not ellipsize fields on output")
cmdListUnits.Flags.BoolVar(&sharedFlags.Full, "l", false, "Shorthand for --full")
cmdListUnits.Flags.BoolVar(&sharedFlags.NoLegend, "no-legend", false, "Do not print a legend (column headers)")
cmdListUnits.Flags.StringVar(&sharedFlags.Fields, "fields", defaultListUnitsFields, fmt.Sprintf("Columns to print for each Unit. Valid fields are %q", strings.Join(mapKeys(fieldsToOutput), ",")))
}
func runListUnits(args []string) (exit int) {
if sharedFlags.Fields == "" {
fmt.Fprintf(os.Stderr, "Must define output format\n")
return 1
}
cols := strings.Split(sharedFlags.Fields, ",")
for _, s := range cols {
if _, ok := fieldsToOutput[s]; !ok {
fmt.Fprintf(os.Stderr, "Invalid key in output format: %q\n", s)
return 1
}
}
jobs, sortable, err := findAllUnits()
if err != nil {
fmt.Fprintf(os.Stderr, "Error retrieving list of units from repository: %v\n", err)
return 1
}
if !sharedFlags.NoLegend {
fmt.Fprintln(out, strings.ToUpper(strings.Join(cols, "\t")))
}
for _, name := range sortable {
var f []string
j := jobs[name]
for _, c := range cols {
f = append(f, fieldsToOutput[c](&j, sharedFlags.Full))
}
fmt.Fprintln(out, strings.Join(f, "\t"))
}
out.Flush()
return
}
// findAllUnits returns a map describing all the Jobs in the Registry, and a
// sort.StringSlice containing their names in sorted order.
// It returns any error encountered in communicating with the Registry.
func findAllUnits() (jobs map[string]job.Job, sortable sort.StringSlice, err error) {
jobs = make(map[string]job.Job, 0)
jj, err := registryCtl.GetAllJobs()
if err != nil {
return
}
for _, j := range jj {
jobs[j.Name] = j
sortable = append(sortable, j.Name)
}
sortable.Sort()
return
}
func mapKeys(m map[string]jobToField) (keys []string) {
for k, _ := range m {
keys = append(keys, k)
}
return
}