forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.go
130 lines (106 loc) · 3.2 KB
/
workers.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
package commands
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/concourse/atc"
"github.com/concourse/fly/rc"
"github.com/concourse/fly/ui"
"github.com/fatih/color"
)
type WorkersCommand struct {
Details bool `short:"d" long:"details" description:"Print additional information for each worker"`
}
func (command *WorkersCommand) Execute([]string) error {
target, err := rc.LoadTarget(Fly.Target)
if err != nil {
return err
}
err = target.Validate()
if err != nil {
return err
}
workers, err := target.Client().ListWorkers()
if err != nil {
return err
}
sort.Sort(byWorkerName(workers))
var runningWorkers []atc.Worker
var stalledWorkers []atc.Worker
for _, w := range workers {
if w.State == "stalled" {
stalledWorkers = append(stalledWorkers, w)
} else {
runningWorkers = append(runningWorkers, w)
}
}
dst, isTTY := ui.ForTTY(os.Stdout)
if !isTTY {
return command.tableFor(append(runningWorkers, stalledWorkers...)).Render(os.Stdout)
}
err = command.tableFor(runningWorkers).Render(os.Stdout)
if err != nil {
return err
}
if len(stalledWorkers) > 0 {
fmt.Fprintln(dst, "")
fmt.Fprintln(dst, "")
fmt.Fprintln(dst, "the following workers have not checked in recently:")
fmt.Fprintln(dst, "")
err = command.tableFor(stalledWorkers).Render(os.Stdout)
if err != nil {
return err
}
fmt.Fprintln(dst, "")
fmt.Fprintln(dst, "these stalled workers can be cleaned up by running:")
fmt.Fprintln(dst, "")
fmt.Fprintln(dst, " "+ui.Embolden("fly -t %s prune-worker -w (name)", Fly.Target))
fmt.Fprintln(dst, "")
}
return nil
}
func (command *WorkersCommand) tableFor(workers []atc.Worker) ui.Table {
headers := ui.TableRow{
{Contents: "name", Color: color.New(color.Bold)},
{Contents: "containers", Color: color.New(color.Bold)},
{Contents: "platform", Color: color.New(color.Bold)},
{Contents: "tags", Color: color.New(color.Bold)},
{Contents: "team", Color: color.New(color.Bold)},
{Contents: "state", Color: color.New(color.Bold)},
}
if command.Details {
headers = append(headers,
ui.TableCell{Contents: "garden address", Color: color.New(color.Bold)},
ui.TableCell{Contents: "baggageclaim url", Color: color.New(color.Bold)},
ui.TableCell{Contents: "resource types", Color: color.New(color.Bold)},
)
}
table := ui.Table{Headers: headers}
for _, w := range workers {
row := ui.TableRow{
{Contents: w.Name},
{Contents: strconv.Itoa(w.ActiveContainers)},
{Contents: w.Platform},
stringOrDefault(strings.Join(w.Tags, ", ")),
stringOrDefault(w.Team),
{Contents: w.State},
}
if command.Details {
var resourceTypes []string
for _, t := range w.ResourceTypes {
resourceTypes = append(resourceTypes, t.Type)
}
row = append(row, stringOrDefault(w.GardenAddr))
row = append(row, stringOrDefault(w.BaggageclaimURL))
row = append(row, stringOrDefault(strings.Join(resourceTypes, ", ")))
}
table.Data = append(table.Data, row)
}
return table
}
type byWorkerName []atc.Worker
func (ws byWorkerName) Len() int { return len(ws) }
func (ws byWorkerName) Swap(i int, j int) { ws[i], ws[j] = ws[j], ws[i] }
func (ws byWorkerName) Less(i int, j int) bool { return ws[i].Name < ws[j].Name }