forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.go
506 lines (435 loc) · 12.3 KB
/
ls.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package commands
import (
"errors"
"fmt"
"os"
"regexp"
"sort"
"strings"
"text/tabwriter"
"text/template"
"time"
"io"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcndockerclient"
"github.com/docker/machine/libmachine/persist"
"github.com/docker/machine/libmachine/state"
"github.com/docker/machine/libmachine/swarm"
"github.com/skarademir/naturalsort"
)
const (
lsDefaultTimeout = 10
tableFormatKey = "table"
lsDefaultFormat = "table {{ .Name }}\t{{ .Active }}\t{{ .DriverName}}\t{{ .State }}\t{{ .URL }}\t{{ .Swarm }}\t{{ .DockerVersion }}\t{{ .Error}}"
)
var (
headers = map[string]string{
"Name": "NAME",
"Active": "ACTIVE",
"ActiveHost": "ACTIVE_HOST",
"ActiveSwarm": "ACTIVE_SWARM",
"DriverName": "DRIVER",
"State": "STATE",
"URL": "URL",
"SwarmOptions": "SWARM_OPTIONS",
"Swarm": "SWARM",
"EngineOptions": "ENGINE_OPTIONS",
"Error": "ERRORS",
"DockerVersion": "DOCKER",
"ResponseTime": "RESPONSE",
}
)
type HostListItem struct {
Name string
Active string
ActiveHost bool
ActiveSwarm bool
DriverName string
State state.State
URL string
SwarmOptions *swarm.Options
Swarm string
EngineOptions *engine.Options
Error string
DockerVersion string
ResponseTime time.Duration
}
// FilterOptions -
type FilterOptions struct {
SwarmName []string
DriverName []string
State []string
Name []string
Labels []string
}
func cmdLs(c CommandLine, api libmachine.API) error {
filters, err := parseFilters(c.StringSlice("filter"))
if err != nil {
return err
}
hostList, hostInError, err := persist.LoadAllHosts(api)
if err != nil {
return err
}
hostList = filterHosts(hostList, filters)
// Just print out the names if we're being quiet
if c.Bool("quiet") {
for _, host := range hostList {
fmt.Println(host.Name)
}
return nil
}
template, table, err := parseFormat(c.String("format"))
if err != nil {
return err
}
var w io.Writer
if table {
tabWriter := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
defer tabWriter.Flush()
w = tabWriter
if err := template.Execute(w, headers); err != nil {
return err
}
} else {
w = os.Stdout
}
timeout := time.Duration(c.Int("timeout")) * time.Second
items := getHostListItems(hostList, hostInError, timeout)
swarmMasters := make(map[string]string)
swarmInfo := make(map[string]string)
for _, host := range hostList {
if host.HostOptions != nil {
swarmOptions := host.HostOptions.SwarmOptions
if swarmOptions.Master {
swarmMasters[swarmOptions.Discovery] = host.Name
}
if swarmOptions.Discovery != "" {
swarmInfo[host.Name] = swarmOptions.Discovery
}
}
}
for _, item := range items {
swarmColumn := ""
if item.SwarmOptions != nil && item.SwarmOptions.Discovery != "" {
swarmColumn = swarmMasters[item.SwarmOptions.Discovery]
if item.SwarmOptions.Master {
swarmColumn = fmt.Sprintf("%s (master)", swarmColumn)
}
}
item.Swarm = swarmColumn
if err := template.Execute(w, item); err != nil {
return err
}
}
return nil
}
func parseFormat(format string) (*template.Template, bool, error) {
table := false
finalFormat := format
if finalFormat == "" {
finalFormat = lsDefaultFormat
}
if strings.HasPrefix(finalFormat, tableFormatKey) {
table = true
finalFormat = finalFormat[len(tableFormatKey):]
}
finalFormat = strings.Trim(finalFormat, " ")
r := strings.NewReplacer(`\t`, "\t", `\n`, "\n")
finalFormat = r.Replace(finalFormat)
template, err := template.New("").Parse(finalFormat + "\n")
if err != nil {
return nil, false, err
}
return template, table, nil
}
func parseFilters(filters []string) (FilterOptions, error) {
options := FilterOptions{}
for _, f := range filters {
kv := strings.SplitN(f, "=", 2)
if len(kv) != 2 {
return options, errors.New("Unsupported filter syntax")
}
key, value := strings.ToLower(kv[0]), kv[1]
switch key {
case "swarm":
options.SwarmName = append(options.SwarmName, value)
case "driver":
options.DriverName = append(options.DriverName, value)
case "state":
options.State = append(options.State, value)
case "name":
options.Name = append(options.Name, value)
case "label":
options.Labels = append(options.Labels, value)
default:
return options, fmt.Errorf("Unsupported filter key '%s'", key)
}
}
return options, nil
}
func filterHosts(hosts []*host.Host, filters FilterOptions) []*host.Host {
if len(filters.SwarmName) == 0 &&
len(filters.DriverName) == 0 &&
len(filters.State) == 0 &&
len(filters.Name) == 0 &&
len(filters.Labels) == 0 {
return hosts
}
filteredHosts := []*host.Host{}
swarmMasters := getSwarmMasters(hosts)
for _, h := range hosts {
if filterHost(h, filters, swarmMasters) {
filteredHosts = append(filteredHosts, h)
}
}
return filteredHosts
}
func getSwarmMasters(hosts []*host.Host) map[string]string {
swarmMasters := make(map[string]string)
for _, h := range hosts {
if h.HostOptions != nil {
swarmOptions := h.HostOptions.SwarmOptions
if swarmOptions != nil && swarmOptions.Master {
swarmMasters[swarmOptions.Discovery] = h.Name
}
}
}
return swarmMasters
}
func filterHost(host *host.Host, filters FilterOptions, swarmMasters map[string]string) bool {
swarmMatches := matchesSwarmName(host, filters.SwarmName, swarmMasters)
driverMatches := matchesDriverName(host, filters.DriverName)
stateMatches := matchesState(host, filters.State)
nameMatches := matchesName(host, filters.Name)
labelMatches := matchesLabel(host, filters.Labels)
return swarmMatches && driverMatches && stateMatches && nameMatches && labelMatches
}
func matchesSwarmName(host *host.Host, swarmNames []string, swarmMasters map[string]string) bool {
if len(swarmNames) == 0 {
return true
}
for _, n := range swarmNames {
if host.HostOptions != nil && host.HostOptions.SwarmOptions != nil {
if strings.EqualFold(n, swarmMasters[host.HostOptions.SwarmOptions.Discovery]) {
return true
}
}
}
return false
}
func matchesDriverName(host *host.Host, driverNames []string) bool {
if len(driverNames) == 0 {
return true
}
for _, n := range driverNames {
if strings.EqualFold(host.DriverName, n) {
return true
}
}
return false
}
func matchesState(host *host.Host, states []string) bool {
if len(states) == 0 {
return true
}
for _, n := range states {
s, err := host.Driver.GetState()
if err != nil {
log.Warn(err)
}
if strings.EqualFold(n, s.String()) {
return true
}
}
return false
}
func matchesName(host *host.Host, names []string) bool {
if len(names) == 0 {
return true
}
for _, n := range names {
r, err := regexp.Compile(n)
if err != nil {
log.Error(err)
os.Exit(1) // TODO: Can we get rid of this call, and exit 'properly' ?
}
if r.MatchString(host.Driver.GetMachineName()) {
return true
}
}
return false
}
func matchesLabel(host *host.Host, labels []string) bool {
if len(labels) == 0 {
return true
}
var englabels = make(map[string]string, len(host.HostOptions.EngineOptions.Labels))
if host.HostOptions != nil && host.HostOptions.EngineOptions.Labels != nil {
for _, s := range host.HostOptions.EngineOptions.Labels {
kv := strings.SplitN(s, "=", 2)
englabels[kv[0]] = kv[1]
}
}
for _, l := range labels {
kv := strings.SplitN(l, "=", 2)
if val, exists := englabels[kv[0]]; exists && strings.EqualFold(val, kv[1]) {
return true
}
}
return false
}
// PERFORMANCE: The code of this function is complicated because we try
// to call the underlying drivers as less as possible to get the information
// we need.
func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) {
requestBeginning := time.Now()
url := ""
currentState := state.None
dockerVersion := "Unknown"
hostError := ""
url, err := h.URL()
// PERFORMANCE: if we have the url, it's ok to assume the host is running
// This reduces the number of calls to the drivers
if err == nil {
if url != "" {
currentState = state.Running
} else {
currentState, err = h.Driver.GetState()
}
} else {
currentState, _ = h.Driver.GetState()
}
if err == nil && url != "" {
// PERFORMANCE: Reuse the url instead of asking the host again.
// This reduces the number of calls to the drivers
dockerHost := &mcndockerclient.RemoteDocker{
HostURL: url,
AuthOption: h.AuthOptions(),
}
dockerVersion, err = mcndockerclient.DockerVersion(dockerHost)
if err != nil {
dockerVersion = "Unknown"
} else {
dockerVersion = fmt.Sprintf("v%s", dockerVersion)
}
}
if err != nil {
hostError = err.Error()
}
if hostError == drivers.ErrHostIsNotRunning.Error() {
hostError = ""
}
var swarmOptions *swarm.Options
var engineOptions *engine.Options
if h.HostOptions != nil {
swarmOptions = h.HostOptions.SwarmOptions
engineOptions = h.HostOptions.EngineOptions
}
isMaster := false
swarmHost := ""
if swarmOptions != nil {
isMaster = swarmOptions.Master
swarmHost = swarmOptions.Host
}
activeHost := isActive(currentState, url)
activeSwarm := isSwarmActive(currentState, url, isMaster, swarmHost)
active := "-"
if activeHost {
active = "*"
}
if activeSwarm {
active = "* (swarm)"
}
stateQueryChan <- HostListItem{
Name: h.Name,
Active: active,
ActiveHost: activeHost,
ActiveSwarm: activeSwarm,
DriverName: h.Driver.DriverName(),
State: currentState,
URL: url,
SwarmOptions: swarmOptions,
EngineOptions: engineOptions,
DockerVersion: dockerVersion,
Error: hostError,
ResponseTime: time.Now().Round(time.Millisecond).Sub(requestBeginning.Round(time.Millisecond)),
}
}
func getHostState(h *host.Host, hostListItemsChan chan<- HostListItem, timeout time.Duration) {
// This channel is used to communicate the properties we are querying
// about the host in the case of a successful read.
stateQueryChan := make(chan HostListItem)
go attemptGetHostState(h, stateQueryChan)
select {
// If we get back useful information, great. Forward it straight to
// the original parent channel.
case hli := <-stateQueryChan:
hostListItemsChan <- hli
// Otherwise, give up after a predetermined duration.
case <-time.After(timeout):
hostListItemsChan <- HostListItem{
Name: h.Name,
DriverName: h.Driver.DriverName(),
State: state.Timeout,
ResponseTime: timeout,
}
}
}
func getHostListItems(hostList []*host.Host, hostsInError map[string]error, timeout time.Duration) []HostListItem {
log.Debugf("timeout set to %s", timeout)
hostListItems := []HostListItem{}
hostListItemsChan := make(chan HostListItem)
for _, h := range hostList {
go getHostState(h, hostListItemsChan, timeout)
}
for range hostList {
hostListItems = append(hostListItems, <-hostListItemsChan)
}
close(hostListItemsChan)
for name, err := range hostsInError {
hostListItems = append(hostListItems, newHostListItemInError(name, err))
}
sortHostListItemsByName(hostListItems)
return hostListItems
}
func newHostListItemInError(name string, err error) HostListItem {
return HostListItem{
Name: name,
DriverName: "not found",
State: state.Error,
Error: strings.Replace(err.Error(), "\n", "", -1),
}
}
func sortHostListItemsByName(items []HostListItem) {
m := make(map[string]HostListItem, len(items))
s := make([]string, len(items))
for i, v := range items {
name := strings.ToLower(v.Name)
m[name] = v
s[i] = name
}
sort.Sort(naturalsort.NaturalSort(s))
for i, v := range s {
items[i] = m[v]
}
}
func isActive(currentState state.State, hostURL string) bool {
return currentState == state.Running && hostURL == os.Getenv("DOCKER_HOST")
}
func isSwarmActive(currentState state.State, hostURL string, isMaster bool, swarmHost string) bool {
return isMaster && currentState == state.Running && toSwarmURL(hostURL, swarmHost) == os.Getenv("DOCKER_HOST")
}
func urlPort(urlWithPort string) string {
parts := strings.Split(urlWithPort, ":")
return parts[len(parts)-1]
}
func toSwarmURL(hostURL string, swarmHost string) string {
hostPort := urlPort(hostURL)
swarmPort := urlPort(swarmHost)
return strings.Replace(hostURL, ":"+hostPort, ":"+swarmPort, 1)
}