Skip to content

Commit 2439338

Browse files
author
Matthew Fisher
committed
Merge pull request #4577 from michelleN/deisctl-list-nodes
deisctl list-machines
2 parents dfa98e9 + a0c30bc commit 2439338

8 files changed

Lines changed: 162 additions & 3 deletions

File tree

deisctl/backend/backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Backend interface {
1616
SSH(string) error
1717
SSHExec(string, string) error
1818
Dock(string, []string) error
19+
ListMachines() error
1920
ListUnits() error
2021
ListUnitFiles() error
2122
Status(string) error
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package fleet
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"strings"
7+
8+
"github.com/coreos/fleet/machine"
9+
)
10+
11+
const (
12+
defaultListMachinesFields = "machine,ip,metadata"
13+
)
14+
15+
var (
16+
listMachinesFields = map[string]machineToField{
17+
"machine": func(ms *machine.MachineState, full bool) string {
18+
return machineIDLegend(*ms, full)
19+
},
20+
"ip": func(ms *machine.MachineState, full bool) string {
21+
if len(ms.PublicIP) == 0 {
22+
return "-"
23+
}
24+
return ms.PublicIP
25+
},
26+
"metadata": func(ms *machine.MachineState, full bool) string {
27+
if len(ms.Metadata) == 0 {
28+
return "-"
29+
}
30+
return formatMetadata(ms.Metadata)
31+
},
32+
}
33+
)
34+
35+
type machineToField func(ms *machine.MachineState, full bool) string
36+
37+
// ListMachines prints all nodes to Stdout
38+
func (c *FleetClient) ListMachines() (err error) {
39+
machines, err := c.Fleet.Machines()
40+
if err != nil {
41+
return err
42+
}
43+
44+
c.printMachines(machines)
45+
return
46+
}
47+
48+
// printUnits writes units to stdout using a tabwriter
49+
func (c *FleetClient) printMachines(states []machine.MachineState) {
50+
cols := strings.Split(defaultListMachinesFields, ",")
51+
fmt.Fprintln(c.out, strings.ToUpper(strings.Join(cols, "\t")))
52+
for _, ms := range states {
53+
var f []string
54+
for _, c := range cols {
55+
f = append(f, listMachinesFields[c](&ms, false))
56+
}
57+
fmt.Fprintln(c.out, strings.Join(f, "\t"))
58+
}
59+
c.out.Flush()
60+
}
61+
62+
func formatMetadata(metadata map[string]string) string {
63+
pairs := make([]string, len(metadata))
64+
idx := 0
65+
var sorted sort.StringSlice
66+
for k := range metadata {
67+
sorted = append(sorted, k)
68+
}
69+
sorted.Sort()
70+
for _, key := range sorted {
71+
value := metadata[key]
72+
pairs[idx] = fmt.Sprintf("%s=%s", key, value)
73+
idx++
74+
}
75+
return strings.Join(pairs, ",")
76+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package fleet
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
"text/tabwriter"
7+
8+
"github.com/coreos/fleet/machine"
9+
)
10+
11+
func TestListMachines(t *testing.T) {
12+
testMachines := []machine.MachineState{
13+
machine.MachineState{
14+
ID: "123456",
15+
PublicIP: "1.1.1.1",
16+
Metadata: map[string]string{
17+
"foo": "bar",
18+
"ping": "pong",
19+
},
20+
Version: "",
21+
},
22+
machine.MachineState{
23+
ID: "654321",
24+
PublicIP: "2.2.2.2",
25+
Metadata: nil,
26+
Version: "",
27+
},
28+
}
29+
30+
testWriter := bytes.Buffer{}
31+
testTabWriter := new(tabwriter.Writer)
32+
testTabWriter.Init(&testWriter, 0, 8, 1, '\t', 0)
33+
34+
c := &FleetClient{
35+
Fleet: &stubFleetClient{
36+
testMachineStates: testMachines,
37+
},
38+
out: testTabWriter,
39+
}
40+
41+
err := c.ListMachines()
42+
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
47+
expected := `MACHINE IP METADATA
48+
123456... 1.1.1.1 foo=bar,ping=pong
49+
654321... 2.2.2.2 -
50+
`
51+
52+
actual := testWriter.String()
53+
54+
if expected != actual {
55+
t.Errorf("Expected '%s', Got '%s'", expected, actual)
56+
}
57+
}

deisctl/client/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type DeisCtlClient interface {
2222
Install(argv []string) error
2323
Journal(argv []string) error
2424
List(argv []string) error
25+
Machines(argv []string) error
2526
RefreshUnits(argv []string) error
2627
Restart(argv []string) error
2728
Scale(argv []string) error
@@ -240,6 +241,20 @@ Usage:
240241
return cmd.ListUnits(c.Backend)
241242
}
242243

244+
func (c *Client) Machines(argv []string) error {
245+
usage := `List the current hosts in the cluster
246+
247+
248+
Usage:
249+
deisctl machines
250+
`
251+
// parse command-line arguments
252+
if _, err := docopt.Parse(usage, argv, true, "", false); err != nil {
253+
return err
254+
}
255+
return cmd.ListMachines(c.Backend)
256+
}
257+
243258
// RefreshUnits overwrites local unit files with those requested.
244259
func (c *Client) RefreshUnits(argv []string) error {
245260
usage := `Overwrites local unit files with those requested.

deisctl/cmd/cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ func ListUnits(b backend.Backend) error {
3535
return b.ListUnits()
3636
}
3737

38+
// ListMachines prints a list of current hosts.
39+
func ListMachines(b backend.Backend) error {
40+
return b.ListMachines()
41+
}
42+
3843
// ListUnitFiles prints the contents of all defined unit files.
3944
func ListUnitFiles(b backend.Backend) error {
4045
return b.ListUnitFiles()

deisctl/cmd/cmd_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func (backend *backendStub) RollingRestart(target string, wg *sync.WaitGroup, ou
5454
backend.restartedUnits = append(backend.restartedUnits, target)
5555
}
5656

57+
func (backend *backendStub) ListMachines() error {
58+
return nil
59+
}
60+
5761
func (backend *backendStub) ListUnits() error {
5862
return nil
5963
}

deisctl/deisctl.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Commands, use "deisctl help <command>" to learn more:
3232
install install components, or the entire platform
3333
uninstall uninstall components
3434
list list installed components
35+
machines list the current hosts in the cluster
3536
start start components
3637
stop stop components
3738
restart stop, then start components
@@ -75,7 +76,7 @@ Options:
7576
return 0
7677
}
7778

78-
command := args["<command>"]
79+
command := args["<command>"].(string)
7980
setTunnel := true
8081
// "--help" and "refresh-units" doesn't need SSH tunneling
8182
if helpFlag || command == "refresh-units" {
@@ -95,6 +96,8 @@ Options:
9596
switch command {
9697
case "list":
9798
err = c.List(argv)
99+
case "machines":
100+
err = c.Machines(argv)
98101
case "scale":
99102
err = c.Scale(argv)
100103
case "start":

deisctl/test/mock/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,3 @@ func (cb ConfigBackend) SetWithTTL(key string, value string, ttl uint64) (string
8383
}
8484
return "", fmt.Errorf("%s does not exist", cb.Expected)
8585
}
86-
87-

0 commit comments

Comments
 (0)