-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
637 lines (600 loc) · 19.3 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
package main
import (
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/seatgeek/nomad-helper/command/attach"
"github.com/seatgeek/nomad-helper/command/gc"
"github.com/seatgeek/nomad-helper/command/job"
"github.com/seatgeek/nomad-helper/command/namespace"
"github.com/seatgeek/nomad-helper/command/node"
"github.com/seatgeek/nomad-helper/command/reevaluate"
"github.com/seatgeek/nomad-helper/command/scale"
"github.com/seatgeek/nomad-helper/command/server"
"github.com/seatgeek/nomad-helper/command/tail"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gopkg.in/workanator/go-ataman.v1"
)
var rndr = ataman.NewRenderer(ataman.BasicStyle())
var fieldHelpText = `
<bold,underline>** Arguments **<reset>
* <bold>attribute.<reset,underline>key<reset> will look up <underline>key<reset> in the "Attributes" Nomad client property
* <bold>class<reset> / <bold>nodeclass<reset> for the Nomad client "NodeClass" property
* <bold>datacenter<reset> / <bold>dc<reset> for the Nomad client "Datacenter" property
* <bold>drain<reset> for the Nomad client "Drain" property
* <bold>eligibility<reset> / <bold>schedulingeligibility<reset> for the Nomad client "SchedulingEligibility" property
* <bold>hostname<reset> is an alias for <bold>attribute.<reset,underline>unique.hostname<reset>
* <bold>id<reset> for the Nomad client "ID" property
* <bold>ip<reset> / <bold>address<reset> / <bold>ip-address<reset> is alias for <bold>attribute.<reset,underline>unique.network.ip-address<reset>
* <bold>meta.<reset,underline>key<reset> will look up <underline>key<reset> in the "Meta" Nomad client configuration
* <bold>name<reset> for the Nomad client "Name" property
* <bold>status<reset> for the Nomad client "Status" property
`
var filterHelpText = `
<bold,underline>** Filters **<reset>
--filter-attribute 'driver.docker.version=17.09.0-ce' Filter nodes by their attribute key/value like 'driver.docker.version=17.09.0-ce'. Flag can be repeated.
--filter-class batch-jobs Filter nodes by their node class batch-jobs
--filter-eligibility eligible/ineligible Filter nodes by their eligibility status eligible/ineligible
--filter-meta 'aws.instance.availability-zone=us-east-1e' Filter nodes by their meta key/value like 'aws.instance.availability-zone=us-east-1e'. Flag can be repeated.
--filter-prefix ef30d57c Filter nodes by their ID with prefix matching ef30d57c
--filter-version 0.8.4 Filter nodes by their Nomad version 0.8.4
`
var filterWebHelpText = `
<bold,underline>** Filters **<reset>
Filters are always passed as HTTP query arguments, order doesn't matter
/?filter-attribute=driver.docker.version=17.09.0-ce Filter nodes by their attribute key/value like 'driver.docker.version=17.09.0-ce'.
/?filter-class=batch-jobs Filter nodes by their node class batch-jobs
/?filter-eligibility=eligible/ineligible Filter nodes by their eligibility status eligible/ineligible
/?filter-meta=aws.instance.availability-zone=us-east-1e Filter nodes by their meta key/value like 'aws.instance.availability-zone=us-east-1e'.
/?filter-prefix=ef30d57c Filter nodes by their ID with prefix matching ef30d57c
/?filter-version=0.8.4 Filter nodes by their Nomad version 0.8.4
`
var helpExamples = `
<bold,underline>** Examples **<reset>
* nomad-helper node __COMMAND__ <bold>class status<reset>
* nomad-helper node __COMMAND__ <bold>attribute<reset,underline>.nomad.version<reset,bold> attribute.<reset,underline>driver.docker<reset>
* nomad-helper node __COMMAND__ <bold>meta.<reset,underline>aws.instance.region<reset,bold> attribute.<reset,underline>nomad.version<reset>
`
var helpWebExamples = `
<bold,underline>** Examples **<reset>
Fields are always passed as HTTP path, and processed in order
* /help
* /help/node/breakdown
* /help/node/list
* /help/node/discover
* /help/[command]/[subcommand]
* /node/[breakdown|list]/<bold>class<reset>/<bold>status<reset>
* /node/[breakdown|list]/<bold>meta.<reset,underline>aws.instance.region<reset>/<bold>attribute.<reset,underline>nomad.version<reset>
* /node/[breakdown|list]/<bold>attribute<reset,underline>.nomad.version<reset>/<bold>attribute.<reset,underline>driver.docker<reset>
`
var filterFlags = []cli.Flag{
cli.StringFlag{
Name: "filter-prefix",
Usage: "Filter nodes by their ID with prefix matching `ef30d57c`",
},
cli.StringFlag{
Name: "filter-class",
Usage: "Filter nodes by their node class `batch-jobs`",
},
cli.StringFlag{
Name: "filter-version",
Usage: "Filter nodes by their Nomad version `0.8.4`",
},
cli.StringFlag{
Name: "filter-eligibility",
Usage: "Filter nodes by their eligibility status `eligible/ineligible`",
},
cli.IntFlag{
Name: "percent",
Usage: "Filter only specific percent of nodes percent of nodes",
Value: 100,
},
cli.StringSliceFlag{
Name: "filter-meta",
Usage: "Filter nodes by their meta key/value like `'aws.instance.availability-zone=us-east-1e'`. Can be provided multiple times.",
},
cli.StringSliceFlag{
Name: "filter-attribute",
Usage: "Filter nodes by their attribute key/value like `'driver.docker.version=17.09.0-ce'`. Can be provided multiple times.",
},
cli.BoolFlag{
Name: "noop",
Usage: "Only output nodes that would be drained, don't do any modifications",
},
cli.BoolFlag{
Name: "no-progress",
Usage: "Do not show progress bar",
},
}
// Version is filled in by the compiler (git tag + changes)
var Version = "local-dev"
func main() {
app := cli.NewApp()
app.Name = "nomad-helper"
app.Usage = "Useful utilities for working with Nomad at scale"
app.Version = Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "log-level",
Value: "info",
Usage: "Debug level (debug, info, warn/warning, error, fatal, panic)",
EnvVar: "LOG_LEVEL",
},
}
app.Commands = []cli.Command{
{
Name: "attach",
Usage: "attach to a specific allocation",
Flags: []cli.Flag{
cli.StringFlag{
Name: "job",
Usage: "List allocations for the job and attach to the selected allocation",
},
cli.StringFlag{
Name: "alloc",
Usage: "Partial UUID or the full 36 char UUID to attach to",
},
cli.StringFlag{
Name: "task",
Usage: "Task name to auto-select if the allocation has multiple tasks in the allocation group",
},
cli.BoolFlag{
Name: "host",
Usage: "Connect to the host directly instead of attaching to a container",
},
cli.StringFlag{
Name: "command",
Value: "bash",
Usage: "Command to run when attaching to the container",
},
},
Action: func(c *cli.Context) error {
err := attach.Run(c)
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "tail",
Usage: "tail stdout/stderr from nomad alloc",
Flags: []cli.Flag{
cli.StringFlag{
Name: "job",
Usage: "(optional) list allocations for the job and attach to the selected allocation",
},
cli.StringFlag{
Name: "alloc",
Usage: "(optional) partial UUID or the full 36 char UUID to attach to",
},
cli.StringFlag{
Name: "task",
Usage: "(optional) the task name to auto-select if the allocation has multiple tasks in the allocation group",
},
cli.BoolTFlag{
Name: "stderr",
Usage: "(optional, default: true) tail stderr from nomad",
},
cli.BoolTFlag{
Name: "stdout",
Usage: "(optional, default: true) tail stdout from nomad",
},
cli.StringFlag{
Name: "writer",
Value: "color",
Usage: "(optional, default: color) writer type (raw, color, simple)",
},
cli.StringFlag{
Name: "theme, ct",
Value: "emacs",
Usage: "(optional, default: emacs) Chroma color scheme to use - see https://xyproto.github.io/splash/docs/",
},
},
Action: func(c *cli.Context) error {
err := tail.Run(c)
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "job",
Usage: "job specific commands with a twist (see help)",
Flags: filterFlags,
Subcommands: []cli.Command{
{
Name: "stop",
Usage: "Stop jobs in the cluster",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "purge",
Usage: "Purge job",
},
cli.BoolFlag{
Name: "dry",
Usage: "Dry run, just print actions",
},
cli.BoolFlag{
Name: "as-prefix",
Usage: "Treat the job name as a job prefix (job name 'api-' would mean all jobs 'api-*' would be stopped)",
},
},
Action: func(c *cli.Context) error {
err := job.Stop(c, log.StandardLogger())
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "hunt",
Usage: "Hunt the Jobs with discrepancy in Job version between allocations",
Action: func(c *cli.Context) error {
err := job.Hunt()
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "move",
Usage: "Move jobs in the cluster",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "dry",
Usage: "Dry run, just print actions",
},
cli.BoolFlag{
Name: "as-prefix",
Usage: "Treat the job name as a job prefix (job name 'api-' would mean all jobs 'api-*' would be stopped)",
},
cli.StringFlag{
Name: "exclude",
Usage: "Filter out jobs with substring in name",
},
cli.StringFlag{
Name: "constraint",
Usage: "Constraint attribute",
},
cli.StringFlag{
Name: "operand",
Usage: "operator",
},
cli.StringFlag{
Name: "value",
Usage: "value of constraint to check",
},
},
Action: func(c *cli.Context) error {
err := job.Move(c, log.StandardLogger())
if err != nil {
log.Fatal(err)
}
return err
},
},
},
},
{
Name: "namespace",
Usage: "namespace specific commands with a twist (see help)",
Flags: filterFlags,
Subcommands: []cli.Command{
{
Name: "gc",
Usage: "Cleans up empty namespaces",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "dry",
Usage: "Dry run, just print actions",
},
cli.StringSliceFlag{
Name: "ignore-job",
Usage: "Ignore a job ID when marking a namespace as empty. Can be provided multiple times.",
},
},
Action: func(c *cli.Context) error {
err := namespace.GC(c, log.StandardLogger())
if err != nil {
log.Fatal(err)
}
return err
},
},
},
},
{
Name: "node",
Usage: "node specific commands that act on all Nomad clients that match the filters provided, rather than a single node",
Flags: filterFlags,
Subcommands: []cli.Command{
{
Name: "drain",
Usage: "The node drain command is used to toggle drain mode on a given node. Drain mode prevents any new tasks from being allocated to the node, and begins migrating all existing allocations away",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "enable",
Usage: "Enable node drain mode",
},
cli.BoolFlag{
Name: "disable",
Usage: "Disable node drain mode",
},
cli.DurationFlag{
Name: "deadline",
Usage: "Set the deadline by which all allocations must be moved off the node. Remaining allocations after the deadline are force removed from the node. Defaults to 1 hour",
Value: time.Hour,
},
cli.BoolFlag{
Name: "no-deadline",
Usage: "No deadline allows the allocations to drain off the node without being force stopped after a certain deadline",
},
cli.BoolFlag{
Name: "monitor",
Usage: "Enter monitor mode directly without modifying the drain status",
},
cli.BoolFlag{
Name: "force",
Usage: "Force remove allocations off the node immediately",
},
cli.BoolFlag{
Name: "detach",
Usage: "Return immediately instead of entering monitor mode",
},
cli.BoolFlag{
Name: "ignore-system",
Usage: "Ignore system allows the drain to complete without stopping system job allocations. By default system jobs are stopped last.",
},
cli.BoolFlag{
Name: "keep-ineligible",
Usage: "Keep ineligible will maintain the node's scheduling ineligibility even if the drain is being disabled. This is useful when an existing drain is being canceled but additional scheduling on the node is not desired.",
},
cli.BoolFlag{
Name: "no-progress",
Usage: "Do not show progress bar",
},
cli.BoolFlag{
Name: "with-benefits",
Usage: "Instead of draining the node in a regular way move the jobs to specific constraints",
},
cli.StringFlag{
Name: "constraint",
Usage: "Constraint attribute",
},
cli.StringFlag{
Name: "operand",
Usage: "operator",
},
cli.StringFlag{
Name: "value",
Usage: "value of constraint to check",
},
cli.BoolFlag{
Name: "wait-for-pending",
Usage: "Will wait for pending allocation and blocked evaluations per job",
},
},
Action: func(c *cli.Context) error {
err := node.Drain(c, log.StandardLogger())
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "eligibility",
Aliases: []string{"eligible"},
Usage: "The eligibility command is used to toggle scheduling eligibility for a given node. By default node's are eligible for scheduling meaning they can receive placements and run new allocations. Node's that have their scheduling eligibility disabled are ineligible for new placements.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "enable",
Usage: "Enable scheduling eligibility",
},
cli.BoolFlag{
Name: "disable",
Usage: "Disable scheduling eligibility",
},
},
Action: func(c *cli.Context) error {
err := node.Eligibility(c, log.StandardLogger())
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "list",
Usage: `Output list of key properties for a Nomad client`,
UsageText: "nomad-helper node [filters...] list [command options] [keys...]",
Description: rndr.MustRender(fieldHelpText) + rndr.MustRender(filterHelpText) + rndr.MustRender(strings.ReplaceAll(helpExamples, "__COMMAND__", "list")),
ArgsUsage: "[keys...]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "output-format",
Value: "table",
Usage: "Either `table, json or json-pretty`",
},
},
Action: func(c *cli.Context) error {
err := node.ListCLI(c, log.StandardLogger())
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "breakdown",
Usage: `Break down (count) how many Nomad clients that match a list of key properties`,
UsageText: "nomad-helper node [filters...] breakdown [command options] [keys...]",
Description: rndr.MustRender(fieldHelpText) + rndr.MustRender(filterHelpText) + rndr.MustRender(strings.ReplaceAll(helpExamples, "__COMMAND__", "breakdown")),
ArgsUsage: "[keys...]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "output-format",
Value: "table",
Usage: `Either "table", "json" or "json-pretty"`,
},
},
Action: func(c *cli.Context) error {
err := node.BreakdownCLI(c, log.StandardLogger())
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "discover",
Usage: `Output the Nomad client Meta and Attribute fields present in your cluster`,
UsageText: "nomad-helper node [filters...] discover [command options]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "output-format",
Value: "table",
Usage: `Either "table", "json" or "json-pretty"`,
},
},
Action: func(c *cli.Context) error {
err := node.DiscoverCLI(c, log.StandardLogger())
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "empty",
Usage: `List nodes that only have system jobs running`,
UsageText: "nomad-helper node [filters...] empty [command options]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "output-format",
Value: "table",
Usage: `Either "table", "json" or "json-pretty"`,
},
},
Action: func(c *cli.Context) error {
err := node.Empty(c, log.StandardLogger())
if err != nil {
log.Fatal(err)
}
return err
},
},
},
},
{
Name: "scale",
Usage: "Import / Export job -> group -> count values",
Subcommands: []cli.Command{
{
Name: "export",
Usage: "Export nomad job scale config to a local file from Nomad cluster",
Action: func(c *cli.Context) error {
configFile := c.Args().Get(0)
if configFile == "" {
return fmt.Errorf("missing file name")
}
err := scale.ExportCommand(configFile)
if err != nil {
log.Fatal(err)
}
return err
},
},
{
Name: "import",
Usage: "Import nomad job scale config from a local file to Nomad cluster",
Action: func(c *cli.Context) error {
configFile := c.Args().Get(0)
if configFile == "" {
return fmt.Errorf("missing file name")
}
err := scale.ImportCommand(configFile)
if err != nil {
log.Fatal(err)
}
return err
},
},
},
},
{
Name: "stats",
Hidden: true,
Usage: "Deprecated!",
Flags: append(filterFlags,
cli.StringSliceFlag{
Name: "dimension",
},
cli.StringFlag{
Name: "output-format",
Value: "table",
Usage: "Either `table, json or json-pretty`",
},
),
Action: func(c *cli.Context) error {
err := node.Stats(c)
log.Error("")
log.Error("'nomad-helper stats' is deprecated, please use 'nomad-helper node breakdown' instead")
log.Error("")
log.Error("Below is a best-effort compatible command for the migration")
log.Error("")
log.Error(err.Error())
log.Fatal("")
return err
},
},
{
Name: "reevaluate-all",
Usage: "Force re-evaluate all jobs",
Action: func(c *cli.Context) error {
return reevaluate.App()
},
},
{
Name: "gc",
Usage: "Force a cluster GC",
Action: func(c *cli.Context) error {
return gc.App()
},
},
{
Name: "server",
Usage: "Run a web server exposing various endpoints",
Description: rndr.MustRender(fieldHelpText) + rndr.MustRender(filterWebHelpText) + rndr.MustRender(strings.ReplaceAll(helpWebExamples, "__COMMAND__", "breakdown")),
Flags: []cli.Flag{
cli.StringFlag{
Name: "listen",
Value: "0.0.0.0:8000",
EnvVar: "LISTEN",
},
},
Action: func(c *cli.Context) error {
return server.Run(app, c, log.StandardLogger())
},
},
}
app.Before = func(c *cli.Context) error {
// convert the human passed log level into logrus levels
level, err := log.ParseLevel(c.String("log-level"))
if err != nil {
log.Fatal(err)
}
log.SetLevel(level)
return nil
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Run(os.Args)
}