forked from jippi/hashi-ui
/
connection.go
503 lines (438 loc) · 11.1 KB
/
connection.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
package main
import (
"fmt"
"io"
"time"
"gopkg.in/fatih/set.v0"
"github.com/gorilla/websocket"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/command"
)
const (
// bytesToLines is an estimation of how many bytes are in each log line.
// This is used to set the offset to read from when a user specifies how
// many lines to tail from.
bytesToLines int64 = 120
// defaultTailLines is the number of lines to tail by default.
defaultTailLines int64 = 250
// If a file exceeds an estimate of 250 loglines we start tailing it
// from the end, otherwise the whole file is retrieved and followed.
maxFileSize int64 = defaultTailLines * bytesToLines
)
// Connection monitors the websocket connection. It processes any action
// received on the websocket and sends out actions on Nomad state changes. It
// maintains a set to keep track of the running watches.
type Connection struct {
socket *websocket.Conn
hub *Hub
receive chan *Action
send chan *Action
destroyCh chan struct{}
watches *set.Set
}
// NewConnection creates a new connection.
func NewConnection(hub *Hub, socket *websocket.Conn) *Connection {
return &Connection{
watches: set.New(),
hub: hub,
socket: socket,
receive: make(chan *Action),
send: make(chan *Action),
destroyCh: make(chan struct{}),
}
}
func (c *Connection) writePump() {
defer func() {
c.socket.Close()
}()
for {
action, ok := <-c.send
if !ok {
if err := c.socket.WriteMessage(websocket.CloseMessage, []byte{}); err != nil {
logger.Errorf("Could not write close message to websocket: %s", err)
}
return
}
if err := c.socket.WriteJSON(action); err != nil {
logger.Errorf("Could not write action to websocket: %s", err)
}
}
}
func (c *Connection) readPump() {
defer func() {
c.watches.Clear()
c.hub.unregister <- c
c.socket.Close()
}()
// Register this connection with the hub for broadcast updates
c.hub.register <- c
// Flush all current state to the websocket
c.hub.nomad.FlushAll(c)
var action Action
for {
err := c.socket.ReadJSON(&action)
if err != nil {
break
}
c.process(action)
}
}
func (c *Connection) process(action Action) {
switch action.Type {
case fetchMember:
c.fetchMember(action)
case fetchNode:
c.fetchNode(action)
case fetchDir:
c.fetchDir(action)
case watchJob:
go c.watchJob(action)
case watchAlloc:
go c.watchAlloc(action)
case watchEval:
go c.watchEval(action)
case watchMember:
go c.watchMember(action)
case watchNode:
go c.watchNode(action)
case watchFile:
go c.watchFile(action)
case unwatchEval:
fallthrough
case unwatchMember:
fallthrough
case unwatchNode:
fallthrough
case unwatchJob:
fallthrough
case unwatchAlloc:
c.watches.Remove(action.Payload.(string))
case unwatchFile:
c.watches.Remove(action.Payload.(string))
}
}
// Handle monitors the websocket connection for incoming actions. It sends
// out actions on state changes.
func (c *Connection) Handle() {
go c.writePump()
c.readPump()
// Kill any remaining watcher routines
close(c.destroyCh)
}
func (c *Connection) watchAlloc(action Action) {
allocID := action.Payload.(string)
defer func() {
c.watches.Remove(allocID)
logger.Infof("Stopped watching alloc with id: %s", allocID)
}()
c.watches.Add(allocID)
logger.Infof("Started watching alloc with id: %s", allocID)
q := &api.QueryOptions{WaitIndex: 1}
for {
select {
case <-c.destroyCh:
return
default:
alloc, meta, err := c.hub.nomad.Client.Allocations().Info(allocID, q)
if err != nil {
logger.Errorf("connection: unable to fetch alloc info: %s", err)
time.Sleep(10 * time.Second)
continue
}
if !c.watches.Has(allocID) {
return
}
c.send <- &Action{Type: fetchedAlloc, Payload: alloc}
waitIndex := meta.LastIndex
if q.WaitIndex > meta.LastIndex {
waitIndex = q.WaitIndex
}
q = &api.QueryOptions{WaitIndex: waitIndex, WaitTime: 10 * time.Second}
}
}
}
func (c *Connection) watchEval(action Action) {
evalID := action.Payload.(string)
defer func() {
c.watches.Remove(evalID)
logger.Infof("Stopped watching eval with id: %s", evalID)
}()
c.watches.Add(evalID)
logger.Infof("Started watching eval with id: %s", evalID)
q := &api.QueryOptions{WaitIndex: 1}
for {
select {
case <-c.destroyCh:
return
default:
eval, meta, err := c.hub.nomad.Client.Evaluations().Info(evalID, q)
if err != nil {
logger.Errorf("connection: unable to fetch eval info: %s", err)
time.Sleep(10 * time.Second)
continue
}
if !c.watches.Has(evalID) {
return
}
c.send <- &Action{Type: fetchedEval, Payload: eval}
waitIndex := meta.LastIndex
if q.WaitIndex > meta.LastIndex {
waitIndex = q.WaitIndex
}
q = &api.QueryOptions{WaitIndex: waitIndex, WaitTime: 10 * time.Second}
}
}
}
func (c *Connection) fetchMember(action Action) {
memberID := action.Payload.(string)
member, err := c.hub.nomad.MemberWithID(memberID)
if err != nil {
logger.Errorf("websocket: unable to fetch member %q: %s", memberID, err)
return
}
c.send <- &Action{Type: fetchedMember, Payload: member}
}
func (c *Connection) watchMember(action Action) {
memberID := action.Payload.(string)
defer func() {
c.watches.Remove(memberID)
logger.Infof("Stopped watching member with id: %s", memberID)
}()
c.watches.Add(memberID)
logger.Infof("Started watching member with id: %s", memberID)
for {
select {
case <-c.destroyCh:
return
default:
member, err := c.hub.nomad.MemberWithID(memberID)
if err != nil {
logger.Errorf("connection: unable to fetch member info: %s", err)
time.Sleep(10 * time.Second)
continue
}
if !c.watches.Has(memberID) {
return
}
c.send <- &Action{Type: fetchedMember, Payload: member}
time.Sleep(10 * time.Second)
}
}
}
func (c *Connection) fetchNode(action Action) {
nodeID := action.Payload.(string)
node, _, err := c.hub.nomad.Client.Nodes().Info(nodeID, nil)
if err != nil {
logger.Errorf("websocket: unable to fetch node %q: %s", nodeID, err)
}
c.send <- &Action{Type: fetchedNode, Payload: node}
}
func (c *Connection) watchNode(action Action) {
nodeID := action.Payload.(string)
defer func() {
c.watches.Remove(nodeID)
logger.Infof("Stopped watching node with id: %s", nodeID)
}()
c.watches.Add(nodeID)
logger.Infof("Started watching node with id: %s", nodeID)
q := &api.QueryOptions{WaitIndex: 1}
for {
select {
case <-c.destroyCh:
return
default:
node, meta, err := c.hub.nomad.Client.Nodes().Info(nodeID, q)
if err != nil {
logger.Errorf("connection: unable to fetch node info: %s", err)
time.Sleep(10 * time.Second)
continue
}
if !c.watches.Has(nodeID) {
return
}
c.send <- &Action{Type: fetchedNode, Payload: node}
waitIndex := meta.LastIndex
if q.WaitIndex > meta.LastIndex {
waitIndex = q.WaitIndex
}
q = &api.QueryOptions{WaitIndex: waitIndex, WaitTime: 10 * time.Second}
}
}
}
func (c *Connection) watchJob(action Action) {
jobID := action.Payload.(string)
defer func() {
c.watches.Remove(jobID)
logger.Infof("Stopped watching job with id: %s", jobID)
}()
c.watches.Add(jobID)
logger.Infof("Started watching job with id: %s", jobID)
q := &api.QueryOptions{WaitIndex: 1}
for {
select {
case <-c.destroyCh:
return
default:
job, meta, err := c.hub.nomad.Client.Jobs().Info(jobID, q)
if err != nil {
logger.Errorf("connection: unable to fetch job info: %s", err)
time.Sleep(10 * time.Second)
continue
}
if !c.watches.Has(jobID) {
return
}
c.send <- &Action{Type: fetchedJob, Payload: job}
waitIndex := meta.LastIndex
if q.WaitIndex > meta.LastIndex {
waitIndex = q.WaitIndex
}
q = &api.QueryOptions{WaitIndex: waitIndex, WaitTime: 10 * time.Second}
}
}
}
func (c *Connection) fetchDir(action Action) {
params, ok := action.Payload.(map[string]interface{})
if !ok {
logger.Errorf("Could not decode payload")
return
}
addr := params["addr"].(string)
path := params["path"].(string)
allocID := params["allocID"].(string)
config := api.DefaultConfig()
config.Address = fmt.Sprintf("http://%s", addr)
client, err := api.NewClient(config)
if err != nil {
logger.Fatalf("Could not create client: %s", err)
return
}
alloc, _, err := client.Allocations().Info(allocID, nil)
if err != nil {
logger.Errorf("Unable to fetch alloc: %s", err)
return
}
dir, _, err := client.AllocFS().List(alloc, path, nil)
if err != nil {
logger.Errorf("Unable to fetch directory: %s", err)
}
c.send <- &Action{Type: fetchedDir, Payload: dir}
}
type Line struct {
Data string
File string
}
func (c *Connection) watchFile(action Action) {
params, ok := action.Payload.(map[string]interface{})
if !ok {
logger.Error("Could not decode payload")
return
}
addr := params["addr"].(string)
path := params["path"].(string)
allocID := params["allocID"].(string)
config := api.DefaultConfig()
config.Address = fmt.Sprintf("http://%s", addr)
client, err := api.NewClient(config)
if err != nil {
logger.Errorf("Could not create client: %s", err)
return
}
alloc, _, err := client.Allocations().Info(allocID, nil)
if err != nil {
logger.Errorf("Unable to fetch alloc: %s", err)
return
}
// Get file stat info
file, _, err := client.AllocFS().Stat(alloc, path, nil)
if err != nil {
logger.Errorf("Unable to stat file: %s", err)
return
}
var origin string = api.OriginStart
var offset int64 = 0
var oversized bool
if file.Size > maxFileSize {
origin = api.OriginEnd
offset = maxFileSize
oversized = true
}
cancel := make(chan struct{})
frames, err := client.AllocFS().Stream(alloc, path, origin, offset, cancel, nil)
if err != nil {
c.send <- &Action{
Type: fileStreamFailed,
Payload: struct {
path string
}{
path: path,
},
}
logger.Errorf("Unable to stream file: %s", err)
return
}
var r io.ReadCloser
frameReader := api.NewFrameReader(frames, cancel)
frameReader.SetUnblockTime(500 * time.Millisecond)
r = command.NewLineLimitReader(frameReader, int(defaultTailLines), int(defaultTailLines*bytesToLines), 1*time.Second)
// Turn the reader into a channel
lines := make(chan []byte)
b := make([]byte, defaultTailLines*bytesToLines)
go func() {
for {
n, err := r.Read(b[:cap(b)])
if err != nil {
return
}
if n > 0 {
lines <- b[0:n]
}
}
}()
c.watches.Add(path)
defer func() {
logger.Infof("Stopped watching file with path: %s", path)
c.watches.Remove(path)
r.Close()
}()
logger.Infof("Started watching file with path: %s", path)
c.send <- &Action{
Type: fetchedFile,
Payload: struct {
File string
Data string
Oversized bool
}{
File: path,
Data: "",
Oversized: oversized,
},
}
ticker := time.NewTicker(10 * time.Second)
for {
select {
case <-c.destroyCh:
return
case line := <-lines:
if !c.watches.Has(path) {
return
}
c.send <- &Action{
Type: fetchedFile,
Payload: struct {
File string
Data string
Oversized bool
}{
File: path,
Data: string(line),
Oversized: oversized,
},
}
case <-ticker.C:
if !c.watches.Has(path) {
return
}
}
}
}