@@ -40,6 +40,7 @@ type FilterOptions struct {
4040type 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\t ACTIVE\t DRIVER\t STATE\t URL\t SWARM\t DOCKER\t ERRORS" )
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
121130func 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}
0 commit comments