forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_machines.go
69 lines (55 loc) · 1.44 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
package main
import (
"fmt"
"strings"
"github.com/coreos/fleet/third_party/github.com/codegangsta/cli"
)
func newListMachinesCommand() cli.Command {
return cli.Command{
Name: "list-machines",
Usage: "Enumerate the current hosts in the cluster",
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`,
Action: listMachinesAction,
Flags: []cli.Flag{
cli.BoolFlag{"full, l", "Do not ellipsize fields on output"},
cli.BoolFlag{"no-legend", "Do not print a legend (column headers)"},
},
}
}
func listMachinesAction(c *cli.Context) {
r := getRegistry(c)
if !c.Bool("no-legend") {
fmt.Fprintln(out, "MACHINE\tIP\tMETADATA")
}
full := c.Bool("full")
for _, m := range r.GetActiveMachines() {
mach := m.BootId
if !full {
mach = ellipsize(mach, 8)
}
ip := m.PublicIP
if len(ip) == 0 {
ip = "-"
}
metadata := "-"
if len(m.Metadata) != 0 {
metadata = formatMetadata(m.Metadata)
}
fmt.Fprintf(out, "%s\t%s\t%s\n", mach, ip, metadata)
}
out.Flush()
}
func formatMetadata(metadata map[string]string) string {
pairs := make([]string, len(metadata))
idx := 0
for key, value := range metadata {
pairs[idx] = fmt.Sprintf("%s=%s", key, value)
idx++
}
return strings.Join(pairs, ", ")
}