-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
725 lines (645 loc) · 15.6 KB
/
main.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"time"
"github.com/atotto/clipboard"
ui "github.com/jroimartin/gocui"
chef "github.com/marpaia/chef-golang"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
forward = iota
backward = iota
elipsis = "..."
)
var (
g *ui.Gui
query = kingpin.Arg("query", "turns the arg into a knife search of 'hostname:<ARG>'").String()
knife = kingpin.Flag("knife", "uses the passed in value as a raw knife search").Short('k').String()
filterStr = kingpin.Flag("filter", "filter string").Short('f').String()
fake = kingpin.Flag("mock", "fake nodes").Short('m').Bool()
role = kingpin.Flag("role", "chef role").Short('r').String()
scp = kingpin.Flag("scp", "file to scp to targets").Short('s').String()
username string
info bool
current string
hosts []node
visibleNodes []node
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.-,"
colors map[string]func(io.Writer, string)
hostLabel string
filterLabel string
window int
f *os.File
msg chan string
)
type node struct {
node chef.Node
selected bool
index int
}
type byHost []node
func (b byHost) Len() int { return len(b) }
func (b byHost) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byHost) Less(i, j int) bool { return b[i].node.Name < b[j].node.Name }
func init() {
msg = make(chan string)
f, _ = os.Create("/tmp/ussh.log")
log.SetOutput(f)
username = os.Getenv("USSH_USER")
if username == "" {
fmt.Println("please set the $USSH_USER env var to your ldap username.")
os.Exit(1)
}
window = 20
w := os.Getenv("USSH_WINDOW")
if w != "" {
x, err := strconv.ParseInt(w, 10, 32)
if err == nil {
window = int(x)
}
}
setupColors()
}
func main() {
kingpin.Parse()
if *fake {
getFakeNodes()
} else {
getNodes()
}
if *filterStr != "" {
search(*filterStr)
}
targets := getTargets()
f.Close()
login(targets)
}
func getTargets() []string {
g = ui.NewGui()
if err := g.Init(); err != nil {
log.Fatal("could not init", err)
}
go message()
current = "hosts-cursor"
g.Editor = ui.EditorFunc(edit)
defer g.Close()
if err := keybindings(g); err != nil {
log.Panicln(err)
}
g.SetLayout(layout)
g.Cursor = true
if err := g.MainLoop(); err != nil {
if err != ui.ErrQuit {
log.Fatal(err)
}
}
var out []string
for _, n := range visibleNodes {
if n.selected {
out = append(out, n.node.Name)
}
}
return out
}
func search(pred string) {
my := len(hosts)
if g != nil {
_, my = g.Size()
my -= 5
}
pred = strings.TrimSpace(pred)
preds := strings.Split(pred, ",")
var i int
visibleNodes = []node{}
for _, n := range hosts {
if i < window && inAll(n.node.Name, preds) {
visibleNodes = append(visibleNodes, n)
i++
}
if i >= window {
break
}
}
}
func inAll(h string, preds []string) bool {
for _, p := range preds {
if strings.Index(h, p) == -1 {
return false
}
}
return true
}
func getWidth() int {
var w int
for _, n := range hosts {
if len(n.node.Name) > w {
w = len(n.node.Name)
}
}
return w
}
func showHelp(g *ui.Gui, v *ui.View) error {
x, y := g.Size()
if v, err := g.SetView("help", -1, -1, x, y); err != nil {
if err != ui.ErrUnknownView {
return err
}
f := colors["color2"]
f(v, " help\n")
f(v, " C-f: Enter filter mode (hit enter to exit filter mode)")
f(v, " C-a: Csshx to all visible hosts")
f(v, " Enter: Ssh to the highlighted host(s)")
f(v, " Space: Toggle select the host on the current line")
f(v, " C-i: Show info for the current host")
f(v, " C-d: Quit without sshing")
f(v, " n: Move cursor to the next host (down arrow does same thing)")
f(v, " p: Move cursor to the previous host (up arrow does same thing)")
f(v, " c: Copy the current host to the clipboard")
f(v, " C: Copy the current host to the clipboard with 'USSH_USER@' prepended to the host")
f(v, " q: Exit the help screen")
current = "help"
v.Editable = false
}
return g.SetCurrentView("help")
}
func exitHelp(g *ui.Gui, v *ui.View) error {
if current != "help" {
return nil
}
current = "hosts-cursor"
return g.DeleteView("help")
}
func layout(g *ui.Gui) error {
x, y := g.Size()
size := len(visibleNodes)
width := getWidth()
if v, err := g.SetView("hosts-label", -1, -1, len("hosts"), 1); err != nil {
if err != ui.ErrUnknownView {
return err
}
v.Frame = false
fmt.Fprintln(v, hostLabel)
}
if v, err := g.SetView("msg", len("hosts")+1, -1, x, 1); err != nil {
if err != ui.ErrUnknownView {
return err
}
v.Highlight = false
v.Frame = false
v.Editable = false
colors["color3"](v, "")
}
if v, err := g.SetView("hosts-cursor", 4, 0, 6, size+1); err != nil {
if err != ui.ErrUnknownView {
return err
}
v.Highlight = true
v.Frame = false
}
if v, err := g.SetView("hosts", 6, 0, width+11, size+1); err != nil {
if err != ui.ErrUnknownView {
return err
}
v.Frame = false
printNodes()
}
if v, err := g.SetView("filter-label", -1, size, width+13, size+2); err != nil {
if err != ui.ErrUnknownView {
return err
}
v.FgColor = ui.ColorGreen
v.Highlight = false
v.Frame = false
v.Editable = false
fmt.Fprintln(v, filterLabel)
}
if v, err := g.SetView("filter", 4, size+1, width, size+3); err != nil {
if err != ui.ErrUnknownView {
return err
}
v.FgColor = ui.ColorGreen
v.Highlight = false
v.Frame = false
v.Editable = true
search(*filterStr)
fmt.Fprintln(v, *filterStr)
*filterStr = ""
}
if v, err := g.SetView("info", width+13, 0, x, y-1); err != nil {
if err != ui.ErrUnknownView {
return err
}
v.FgColor = ui.ColorGreen
v.Highlight = false
v.Frame = false
v.Editable = false
printInfo(v)
}
return g.SetCurrentView(current)
}
func printNodes() {
hv, _ := g.View("hosts")
cv, _ := g.View("hosts-cursor")
hv.Clear()
_, cur := cv.Cursor()
for i, n := range visibleNodes {
prefix, postfix := getElipsis(i)
f := colors["color1"]
if n.selected && i == cur {
f = colors["color3"]
} else if n.selected || i == cur {
f = colors["color2"]
}
f(hv, fmt.Sprintf("%s%s%s", prefix, n.node.Name, postfix))
}
}
func getElipsis(i int) (string, string) {
if len(visibleNodes) < window {
return "", ""
}
if i == 0 && visibleNodes[0].index > 0 {
return elipsis, ""
}
if i == window-1 && (visibleNodes[window-1].index != hosts[len(hosts)-1].index) {
return "", elipsis
}
return "", ""
}
func edit(v *ui.View, key ui.Key, ch rune, mod ui.Modifier) {
if key == ui.KeyEnter {
cv, _ := g.View("hosts-cursor")
cv.SetCursor(0, 0)
current = "hosts-cursor"
printNodes()
return
}
s := strings.TrimSpace(v.Buffer())
if key == 127 && len(s) > 0 {
v.Clear()
s = s[:len(s)-1]
v.Write([]byte(s))
search(s)
v.SetCursor(len(s), 0)
} else if key == 127 && len(s) == 0 {
unhideAll()
} else if acceptable(string(ch)) {
fmt.Fprint(v, string(ch))
s = v.Buffer()
search(s)
v.SetCursor(len(s)-1, 0)
}
printNodes()
}
func unhideAll() {
visibleNodes = make([]node, len(hosts))
copy(visibleNodes, hosts)
}
func acceptable(s string) bool {
return strings.Contains(chars, s)
}
type key struct {
name string
key interface{}
mod ui.Modifier
f ui.KeybindingHandler
}
var keys = []key{
{"", ui.KeyCtrlC, ui.ModNone, quit},
{"", ui.KeyCtrlD, ui.ModNone, quit},
{"", ui.KeyCtrlA, ui.ModNone, sshAll},
{"", 'q', ui.ModNone, exitHelp},
{"filter", ui.KeyEnter, ui.ModNone, exitFilter},
{"hosts-cursor", ui.KeyCtrlN, ui.ModNone, next},
{"hosts-cursor", 'n', ui.ModNone, next},
{"hosts-cursor", ui.KeyArrowDown, ui.ModNone, next},
{"hosts-cursor", ui.KeyCtrlP, ui.ModNone, prev},
{"hosts-cursor", 'p', ui.ModNone, prev},
{"hosts-cursor", ui.KeyArrowUp, ui.ModNone, prev},
{"hosts-cursor", ui.KeySpace, ui.ModNone, sel},
{"hosts-cursor", ui.KeyEnter, ui.ModNone, ssh},
{"hosts-cursor", ui.KeyCtrlI, ui.ModNone, showInfo},
{"hosts-cursor", 'i', ui.ModNone, showInfo},
{"hosts-cursor", ui.KeyCtrlF, ui.ModNone, filter},
{"hosts-cursor", 'f', ui.ModNone, filter},
{"hosts-cursor", ui.KeyCtrlH, ui.ModNone, showHelp},
{"hosts-cursor", 'h', ui.ModNone, showHelp},
{"hosts-cursor", ui.KeyCtrlC, ui.ModNone, copyToClipboard},
{"hosts-cursor", 'c', ui.ModNone, copyToClipboard},
{"hosts-cursor", 'C', ui.ModNone, copyToClipboardWithUsername},
}
func keybindings(g *ui.Gui) error {
for _, k := range keys {
if err := g.SetKeybinding(k.name, k.key, k.mod, k.f); err != nil {
return err
}
}
return nil
}
func quit(g *ui.Gui, v *ui.View) error {
return ui.ErrQuit
}
func copyToClipboard(g *ui.Gui, v *ui.View) error {
cv, _ := g.View("hosts-cursor")
_, cur := cv.Cursor()
n := visibleNodes[cur]
msg <- fmt.Sprintf("copied %s to clipboard", n.node.Name)
return clipboard.WriteAll(n.node.Name)
}
func copyToClipboardWithUsername(g *ui.Gui, v *ui.View) error {
cv, _ := g.View("hosts-cursor")
_, cur := cv.Cursor()
n := visibleNodes[cur]
s := fmt.Sprintf("%s@%s", username, n.node.Name)
msg <- fmt.Sprintf("copied %s to clipboard", s)
return clipboard.WriteAll(s)
}
func message() {
dur := time.Second * 2
for {
select {
case m := <-msg:
dur = time.Second * 2
writeMsg(m)
case <-time.After(dur):
dur = time.Second * 1000
writeMsg("")
}
}
}
func writeMsg(msg string) {
g.Execute(func(g *ui.Gui) error {
v, _ := g.View("msg")
v.Clear()
fmt.Fprint(v, msg)
return nil
})
}
func filter(g *ui.Gui, v *ui.View) error {
var err error
if v, err = g.View("filter"); err != nil {
return err
}
buf := strings.TrimSpace(v.Buffer())
v.Clear()
v.Write([]byte(buf))
current = "filter"
l := len(buf)
return v.SetCursor(l, 0)
}
func exitFilter(g *ui.Gui, v *ui.View) error {
current = "hosts-cursor"
return nil
}
func sshAll(g *ui.Gui, v *ui.View) error {
for i := range visibleNodes {
visibleNodes[i].selected = true
}
return ui.ErrQuit
}
func scroll(dir int) {
if len(visibleNodes) < window {
return
}
if dir == forward {
if visibleNodes[window-1].index == len(hosts)-1 {
return
}
start := visibleNodes[0].index + 1
end := start + window
visibleNodes = hosts[start:end]
} else {
if visibleNodes[0].index == 0 {
return
}
start := visibleNodes[0].index - 1
end := start + window
visibleNodes = hosts[start:end]
}
printNodes()
}
func next(g *ui.Gui, v *ui.View) error {
cx, cy := v.Cursor()
if cy+1 >= len(visibleNodes) {
scroll(forward)
return nil
}
err := v.SetCursor(cx, cy+1)
printNodes()
iv, _ := g.View("info")
printInfo(iv)
return err
}
func prev(g *ui.Gui, v *ui.View) error {
cx, cy := v.Cursor()
if cy-1 < 0 {
scroll(backward)
return nil
}
err := v.SetCursor(cx, cy-1)
printNodes()
iv, _ := g.View("info")
printInfo(iv)
return err
}
func ssh(g *ui.Gui, v *ui.View) error {
_, cy := v.Cursor()
visibleNodes[cy].selected = true
return ui.ErrQuit
}
func sel(g *ui.Gui, v *ui.View) error {
_, cy := v.Cursor()
if visibleNodes[cy].selected {
visibleNodes[cy].selected = false
} else {
visibleNodes[cy].selected = true
}
printNodes()
return nil
}
func showInfo(g *ui.Gui, v *ui.View) error {
v, _ = g.View("info")
if info {
v.Clear()
info = false
} else {
info = true
printInfo(v)
}
return nil
}
func printInfo(v *ui.View) {
if info {
v.Clear()
cv, _ := g.View("hosts-cursor")
_, cur := cv.Cursor()
n := visibleNodes[cur]
fmt.Fprintf(v, "Name: %s\n", n.node.Name)
fmt.Fprintf(v, "Roles: %v\n", n.node.Info.Roles)
fmt.Fprintf(v, "Environment: %s\n", n.node.Environment)
fmt.Fprintf(v, "IP: %s\n", n.node.Info.IPAddress)
fmt.Fprintf(v, "MAC: %s\n", n.node.Info.MACAddress)
fmt.Fprintf(v, "Uptime: %s\n", n.node.Info.Uptime)
printMemory(v, n.node)
printFilesystem(v, n.node)
printCPU(v, n.node)
}
}
func printMemory(v *ui.View, n chef.Node) {
fmt.Fprint(v, "Memory:\n")
fmt.Fprintf(v, " active: %s\n", n.Info.Memory["active"])
fmt.Fprintf(v, " free: %s\n", n.Info.Memory["free"])
fmt.Fprintf(v, " inactive: %s\n", n.Info.Memory["inactive"])
}
func printFilesystem(v *ui.View, n chef.Node) {
fmt.Fprint(v, "Filesystem:\n")
for k, val := range n.Info.Filesystem {
if strings.Index(k, "/dev") > -1 {
fmt.Fprintf(v, " %s\n", k)
fmt.Fprintf(v, " Size: %v\n", val.KBSize)
fmt.Fprintf(v, " Used: %v\n", val.KBUsed)
fmt.Fprintf(v, " Available: %v\n", val.KBavailable)
fmt.Fprintf(v, " PercentUsed: %v\n", val.PercentUsed)
}
}
}
func printCPU(v *ui.View, n chef.Node) {
fmt.Fprint(v, "CPU: \n")
for k, val := range n.Info.CPU {
if k == "cores" {
fmt.Fprintf(v, " %s\n", k)
fmt.Fprintf(v, " Cores: %v\n", val)
}
if k == "cores" {
fmt.Fprintf(v, " Cores: %v\n", val)
}
}
}
func login(targets []string) {
if len(targets) == 1 && targets[0] != "" && len(*scp) == 0 {
cmd := exec.Command("ssh", fmt.Sprintf("%s@%s", username, targets[0]))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal("couldn't ssh", err)
}
} else if len(targets) >= 1 && targets[0] != "" && len(*scp) > 0 {
for _, x := range targets {
cmd := exec.Command("scp", *scp, fmt.Sprintf("%s@%s:", username, x))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal("couldn't scp", err)
}
}
} else if len(targets) > 1 {
for i, x := range targets {
targets[i] = fmt.Sprintf("%s@%s", username, x)
}
cmd := exec.Command("csshx", targets...)
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
}
func getFakeNodes() {
f := []string{"com", "net"}
e := []string{"prod", "staging"}
hosts = make([]node, 30)
for i := 0; i < 30; i++ {
n := node{node: chef.Node{Name: fmt.Sprintf("server%d.%s", i, f[i%2]), Environment: e[i%2]}, index: i}
hosts[i] = n
if i < window {
visibleNodes = append(visibleNodes, n)
}
}
}
func getNodes() {
c, err := chef.Connect()
if err != nil {
log.Fatal("Error:", err)
}
c.SSLNoVerify = true
var q string
if *knife != "" {
q = *knife
} else {
q = fmt.Sprintf("hostname:*%s*", *query)
if *role != "" {
q = fmt.Sprintf("%s AND role:*%s*", q, *role)
}
}
resp, err := c.Search("node", q)
if err != nil {
log.Fatal("search", err)
}
if len(resp.Rows) == 0 {
fmt.Printf("No nodes found with query %s, please try again with a different search\n", *query)
os.Exit(0)
}
for i, x := range resp.Rows {
var cn chef.Node
json.Unmarshal(x, &cn)
n := node{node: cn, index: i}
hosts = append(hosts, n)
}
sort.Sort(byHost(hosts))
for i, _ := range hosts {
hosts[i].index = i
}
end := window
if len(hosts) < window {
end = len(hosts)
}
visibleNodes = hosts[0:end]
}
func setupColors() {
m := map[string]string{
"black": "30",
"red": "31",
"green": "32",
"yellow": "33",
"blue": "34",
"magenta": "35",
"cyan": "36",
"white": "37",
}
color1 := os.Getenv("USSH_COLOR_1")
if color1 == "" {
color1 = "green"
}
color2 := os.Getenv("USSH_COLOR_2")
if color2 == "" {
color2 = "white"
}
color3 := os.Getenv("USSH_COLOR_3")
if color3 == "" {
color3 = "yellow"
}
colors = map[string]func(io.Writer, string){
"color1": func(w io.Writer, s string) {
fmt.Fprintf(w, fmt.Sprintf("\033[%sm%%s\033[%sm\n", m[color1], m[color1]), s)
},
"color2": func(w io.Writer, s string) {
fmt.Fprintf(w, fmt.Sprintf("\033[%sm%%s\033[%sm\n", m[color2], m[color1]), s)
},
"color3": func(w io.Writer, s string) {
fmt.Fprintf(w, fmt.Sprintf("\033[%sm%%s\033[%sm\n", m[color3], m[color1]), s)
},
}
hostLabel = fmt.Sprintf("\033[%smhosts\033[%sm\n", m[color2], m[color1])
filterLabel = fmt.Sprintf("\033[%smfilter\033[%sm\n", m[color2], m[color1])
}