forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_machines.go
118 lines (101 loc) · 2.99 KB
/
list_machines.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
package main
import (
"fmt"
"os"
"sort"
"strings"
"github.com/coreos/fleet/machine"
)
const (
defaultListMachinesFields = "machine,ip,metadata"
)
var (
listMachinesFieldsFlag string
cmdListMachines = &Command{
Name: "list-machines",
Summary: "Enumerate the current hosts in the cluster",
Usage: "[-l|--full] [--no-legend]",
Description: `Lists all active machines within the cluster. Previously active machines will not appear in this list.
For easily parsable output, you can remove the column headers:
fleetctl list-machines --no-legend
Output the list without truncation:
fleetctl list-machines --full`,
Run: runListMachines,
}
listMachinesFields = map[string]machineToField{
"machine": func(ms *machine.MachineState, full bool) string {
return machineIDLegend(*ms, full)
},
"ip": func(ms *machine.MachineState, full bool) string {
if len(ms.PublicIP) == 0 {
return "-"
}
return ms.PublicIP
},
"metadata": func(ms *machine.MachineState, full bool) string {
if len(ms.Metadata) == 0 {
return "-"
}
return formatMetadata(ms.Metadata)
},
}
)
type machineToField func(ms *machine.MachineState, full bool) string
func init() {
cmdListMachines.Flags.BoolVar(&sharedFlags.Full, "full", false, "Do not ellipsize fields on output")
cmdListMachines.Flags.BoolVar(&sharedFlags.Full, "l", false, "Shorthand for --full")
cmdListMachines.Flags.BoolVar(&sharedFlags.NoLegend, "no-legend", false, "Do not print a legend (column headers)")
cmdListMachines.Flags.StringVar(&listMachinesFieldsFlag, "fields", defaultListMachinesFields, fmt.Sprintf("Columns to print for each Machine. Valid fields are %q", strings.Join(machineToFieldKeys(listMachinesFields), ",")))
}
func runListMachines(args []string) (exit int) {
if listMachinesFieldsFlag == "" {
fmt.Fprintf(os.Stderr, "Must define output format\n")
return 1
}
cols := strings.Split(listMachinesFieldsFlag, ",")
for _, s := range cols {
if _, ok := listMachinesFields[s]; !ok {
fmt.Fprintf(os.Stderr, "Invalid key in output format: %q\n", s)
return 1
}
}
machines, err := cAPI.Machines()
if err != nil {
fmt.Fprintf(os.Stderr, "Error retrieving list of active machines: %v\n", err)
return 1
}
if !sharedFlags.NoLegend {
fmt.Fprintln(out, strings.ToUpper(strings.Join(cols, "\t")))
}
for _, ms := range machines {
ms := ms
var f []string
for _, c := range cols {
f = append(f, listMachinesFields[c](&ms, sharedFlags.Full))
}
fmt.Fprintln(out, strings.Join(f, "\t"))
}
out.Flush()
return
}
func formatMetadata(metadata map[string]string) string {
pairs := make([]string, len(metadata))
idx := 0
var sorted sort.StringSlice
for k := range metadata {
sorted = append(sorted, k)
}
sorted.Sort()
for _, key := range sorted {
value := metadata[key]
pairs[idx] = fmt.Sprintf("%s=%s", key, value)
idx++
}
return strings.Join(pairs, ",")
}
func machineToFieldKeys(m map[string]machineToField) (keys []string) {
for k, _ := range m {
keys = append(keys, k)
}
return
}