Skip to content

Commit 24a13af

Browse files
committed
FIX docker-archive-public#2653 docker-machine ls need to identify machine or cluster
Signed-off-by: David Gageot <david@gageot.net>
1 parent 5bb3d90 commit 24a13af

File tree

3 files changed

+89
-26
lines changed

3 files changed

+89
-26
lines changed

commands/ls.go

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type FilterOptions struct {
4040
type HostListItem struct {
4141
Name string
4242
Active bool
43+
SwarmActive bool
4344
DriverName string
4445
State state.State
4546
URL string
@@ -78,6 +79,8 @@ func cmdLs(c CommandLine, api libmachine.API) error {
7879
swarmInfo := make(map[string]string)
7980

8081
w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
82+
defer w.Flush()
83+
8184
fmt.Fprintln(w, "NAME\tACTIVE\tDRIVER\tSTATE\tURL\tSWARM\tDOCKER\tERRORS")
8285

8386
for _, host := range hostList {
@@ -96,26 +99,32 @@ func cmdLs(c CommandLine, api libmachine.API) error {
9699
items := getHostListItems(hostList, hostInError)
97100

98101
for _, item := range items {
99-
activeString := "-"
100-
if item.Active {
101-
activeString = "*"
102-
}
102+
printItemToTabWriter(item, swarmInfo, swarmMasters, w)
103+
}
103104

104-
swarmInfo := ""
105+
return nil
106+
}
105107

106-
if item.SwarmOptions != nil && item.SwarmOptions.Discovery != "" {
107-
swarmInfo = swarmMasters[item.SwarmOptions.Discovery]
108-
if item.SwarmOptions.Master {
109-
swarmInfo = fmt.Sprintf("%s (master)", swarmInfo)
110-
}
111-
}
112-
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
113-
item.Name, activeString, item.DriverName, item.State, item.URL, swarmInfo, item.DockerVersion, item.Error)
108+
func printItemToTabWriter(item HostListItem, swarmInfo map[string]string, swarmMasters map[string]string, w *tabwriter.Writer) {
109+
activeColumn := "-"
110+
if item.Active {
111+
activeColumn = "*"
112+
}
113+
if item.SwarmActive {
114+
activeColumn = "* (swarm)"
114115
}
115116

116-
w.Flush()
117+
swarmColumn := ""
117118

118-
return nil
119+
if item.SwarmOptions != nil && item.SwarmOptions.Discovery != "" {
120+
swarmColumn = swarmMasters[item.SwarmOptions.Discovery]
121+
if item.SwarmOptions.Master {
122+
swarmColumn = fmt.Sprintf("%s (master)", swarmColumn)
123+
}
124+
}
125+
126+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
127+
item.Name, activeColumn, item.DriverName, item.State, item.URL, swarmColumn, item.DockerVersion, item.Error)
119128
}
120129

121130
func parseFilters(filters []string) (FilterOptions, error) {
@@ -320,9 +329,17 @@ func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) {
320329
engineOptions = h.HostOptions.EngineOptions
321330
}
322331

332+
isMaster := false
333+
swarmHost := ""
334+
if swarmOptions != nil {
335+
isMaster = swarmOptions.Master
336+
swarmHost = swarmOptions.Host
337+
}
338+
323339
stateQueryChan <- HostListItem{
324340
Name: h.Name,
325341
Active: isActive(currentState, url),
342+
SwarmActive: isSwarmActive(currentState, url, isMaster, swarmHost),
326343
DriverName: h.Driver.DriverName(),
327344
State: currentState,
328345
URL: url,
@@ -397,16 +414,21 @@ func sortHostListItemsByName(items []HostListItem) {
397414
}
398415
}
399416

400-
// IsActive provides a single function for determining if a host is active
401-
// based on both the url and if the host is stopped.
402-
func isActive(currentState state.State, url string) bool {
403-
dockerHost := os.Getenv("DOCKER_HOST")
417+
func isActive(currentState state.State, hostURL string) bool {
418+
return currentState == state.Running && hostURL == os.Getenv("DOCKER_HOST")
419+
}
420+
421+
func isSwarmActive(currentState state.State, hostURL string, isMaster bool, swarmHost string) bool {
422+
return isMaster && currentState == state.Running && toSwarmURL(hostURL, swarmHost) == os.Getenv("DOCKER_HOST")
423+
}
404424

405-
// TODO: hard-coding the swarm port is a travesty...
406-
deSwarmedHost := strings.Replace(dockerHost, ":3376", ":2376", 1)
407-
if dockerHost == url || deSwarmedHost == url {
408-
return currentState == state.Running
409-
}
425+
func urlPort(urlWithPort string) string {
426+
parts := strings.Split(urlWithPort, ":")
427+
return parts[len(parts)-1]
428+
}
410429

411-
return false
430+
func toSwarmURL(hostURL string, swarmHost string) string {
431+
hostPort := urlPort(hostURL)
432+
swarmPort := urlPort(swarmHost)
433+
return strings.Replace(hostURL, ":"+hostPort, ":"+swarmPort, 1)
412434
}

commands/ls_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func TestIsActive(t *testing.T) {
415415
{"tcp://5.6.7.8:2376", state.Running, false},
416416
{"tcp://1.2.3.4:2376", state.Stopped, false},
417417
{"tcp://1.2.3.4:2376", state.Running, true},
418-
{"tcp://1.2.3.4:3376", state.Running, true},
418+
{"tcp://1.2.3.4:3376", state.Running, false},
419419
}
420420

421421
for _, c := range cases {
@@ -430,6 +430,33 @@ func TestIsActive(t *testing.T) {
430430
}
431431
}
432432

433+
func TestIsSwarmActive(t *testing.T) {
434+
cases := []struct {
435+
dockerHost string
436+
state state.State
437+
isMaster bool
438+
expected bool
439+
}{
440+
{"", state.Running, false, false},
441+
{"tcp://5.6.7.8:3376", state.Running, true, false},
442+
{"tcp://1.2.3.4:3376", state.Stopped, true, false},
443+
{"tcp://1.2.3.4:3376", state.Running, true, true},
444+
{"tcp://1.2.3.4:3376", state.Running, false, false},
445+
{"tcp://1.2.3.4:2376", state.Running, true, false},
446+
}
447+
448+
for _, c := range cases {
449+
os.Unsetenv("DOCKER_HOST")
450+
if c.dockerHost != "" {
451+
os.Setenv("DOCKER_HOST", c.dockerHost)
452+
}
453+
454+
actual := isSwarmActive(c.state, "tcp://1.2.3.4:2376", c.isMaster, "tcp://0.0.0.0:3376")
455+
456+
assert.Equal(t, c.expected, actual)
457+
}
458+
}
459+
433460
func TestGetHostStateTimeout(t *testing.T) {
434461
defer func(timeout time.Duration) { stateTimeoutDuration = timeout }(stateTimeoutDuration)
435462
stateTimeoutDuration = 1 * time.Millisecond

test/integration/core/swarm-options.bats

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ export TOKEN=$(curl -sS -X POST "https://discovery.hub.docker.com/v1/clusters")
2626
echo ${heartbeat_arg}
2727
[[ "$heartbeat_arg" =~ "--heartbeat=5s" ]]
2828
}
29+
30+
@test "should not show as swarm active if normal active" {
31+
eval $(machine env queenbee)
32+
run machine ls
33+
echo ${output}
34+
[[ ${lines[1]} != *"* (swarm)"* ]]
35+
}
36+
37+
@test "should show as swarm active" {
38+
eval $(machine env --swarm queenbee)
39+
run machine ls
40+
echo ${output}
41+
[[ ${lines[1]} == *"* (swarm)"* ]]
42+
}

0 commit comments

Comments
 (0)