-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathkamal.ts
624 lines (604 loc) · 15.7 KB
/
kamal.ts
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
const destinationGenerator: Fig.Generator = {
script: ["bin/kamal", "destinations", "--json"],
cache: {
cacheByDirectory: true,
strategy: "stale-while-revalidate",
ttl: 30,
},
postProcess: function (out) {
try {
return JSON.parse(out).map((destination: string) => ({
name: destination,
}));
} catch (e) {
console.error(e);
return [];
}
},
};
const deployOptions: Fig.Option[] = [
{
name: ["-P", "--skip_push"],
description: "Skip image build and push",
},
];
const logOptions: Fig.Option[] = [
{
name: ["--since", "-s"],
description:
'Show lines since timestamp\\" (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)',
},
{
name: ["--lines", "-n"],
description: "Number of lines to show from each server",
},
{
name: ["--grep", "-g"],
description:
"Show lines with grep match only (use this to fetch specific requests by id)",
},
{
name: ["--follow", "-f"],
description:
"Follow log on primary server (or specific host set by --hosts)",
},
];
const baseOptions: Fig.Option[] = [
{ name: ["--verbose", "-v"], description: "Detailed logging" },
{ name: ["--quiet", "-q"], description: "Minimal logging" },
{
name: "--version",
args: { name: "VERSION" },
description: "Run commands against a specific app version",
},
{
name: ["--primary", "-p"],
description: "Run commands only on primary host instead of all",
},
{
name: ["--hosts", "-h"],
args: { name: "hosts" },
description:
"Run commands on these hosts instead of all (separate by comma)",
},
{
name: ["--roles", "-r"],
args: { name: "roles" },
description:
"Run commands on these roles instead of all (separate by comma)",
},
{
name: ["--config_file", "-c"],
args: { name: "config", default: "config/deploy.yml" },
description: "Path to config file",
},
{
name: ["-d", "--destination"],
description: "Specify destination to use",
args: {
name: "destination",
description: "Destination to use",
generators: destinationGenerator,
},
},
{ name: ["--skip_hooks", "-H"], description: "Don't run hooks" },
];
// ------------------------ COMMANDS ------------------------
const accessorySubcommand: Fig.Subcommand = {
name: "accessory",
description: "Manage accessories (db/redis/search)",
subcommands: [
{
name: "boot",
description:
"Boot new accessory service on host (use NAME=all to boot all accessories)",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
suggestions: [
{ name: "all", displayName: 'Use "all" to boot all accessories' },
],
},
},
{
name: "upload",
description: "Upload accessory files to host",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
{
name: "directories",
description: "Create accessory directories on host",
hidden: true,
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
{
name: "reboot",
description:
"Reboot existing accessory on host (stop container, remove container, start new container)",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
{
name: "start",
description: "Start existing accessory container on host",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
{
name: "stop",
description: "Stop existing accessory container on host",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
{
name: "restart",
description: "Restart existing accessory container on host",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
{
name: "details",
description:
"Show details about accessory on host (use NAME=all to show all accessories)",
args: {
name: "name",
suggestions: [
{ name: "all", displayName: 'Use "all" to boot all accessories' },
],
},
},
{
name: "exec",
description: "Execute a custom command on servers",
args: [
{
name: "name",
},
{
name: "CMD",
},
],
options: [
{
name: ["-i", "--interactive"],
description:
"Execute command over ssh for an interactive shell (use for console/bash)",
},
{
name: "--reuse",
description:
"Reuse currently running container instead of starting a new one",
},
],
},
{
name: "logs",
description: "Show log lines from accessory on host",
options: logOptions,
},
{
name: "status",
description: "Show status of accessory on host",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
{
name: "remove",
description:
"Remove accessory container, image and data directory from host",
options: [
{
name: ["-y", "--confirmed"],
description: "Proceed without confirmation question",
},
],
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
suggestions: [
{ name: "all", displayName: 'Use "all" to boot all accessories' },
],
},
},
{
name: "remove_container",
hidden: true,
description: "Remove accessory container from host",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
{
name: "remove_image",
hidden: true,
description: "Remove accessory image from host",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
{
name: "remove_service_directory",
hidden: true,
description:
"Remove accessory directory used for uploaded files and data directories from host",
args: {
name: "name", // TODO: this can be autocompleted by parsing the config
},
},
],
};
const appSubcommand: Fig.Subcommand = {
name: "app",
description: "Manage application",
subcommands: [
{
name: "boot",
description: "Boot app on servers (or reboot app if already running)",
},
{ name: "start", description: "Start existing app container on servers" },
{ name: "stop", description: "Stop app container on servers" },
{ name: "details", description: "Show details about app containers" },
{
name: "exec",
description: "Execute a custom command on servers",
args: { name: "CMD" },
options: [
{
name: ["--interactive", "-i"],
description:
"Execute command over ssh for an interactive shell (use for console/bash)",
},
{
name: "--reuse",
description:
"Reuse currently running container instead of starting a new one",
},
],
},
{ name: "containers", description: "Show app containers on servers" },
{
name: "stale_containers",
description: "Detect app stale containers",
options: [
{
name: ["--stop", "-s"],
description: "Stop the stale containers found",
},
],
},
{ name: "images", description: "Show app images on servers" },
{
name: "logs",
description:
"Show log lines from app on servers (use --help to show options)",
options: logOptions,
},
{
name: "remove",
description: "Remove app containers and images from servers",
},
{
name: "remove_container",
description: "Remove app container with given version from servers",
hidden: true,
args: { name: "VERSION" },
},
{
name: "remove_containers",
description: "Remove all app containers from servers",
hidden: true,
},
{
name: "remove_images",
description: "Remove all app images from servers",
hidden: true,
},
{
name: "version",
description: "Show app version currently running on servers",
},
],
};
const traefikCommand: Fig.Subcommand = {
name: "traefik",
description: "Manage Traefik load balancer",
icon: "🚦",
subcommands: [
{ name: "boot", description: "Boot Traefik on servers" },
{
name: "reboot",
description:
"Reboot Traefik on servers (stop container, remove container, start new container)",
options: [
{
name: "--rolling",
description: `Reboot traefik on hosts in sequence, rather than in parallel`,
},
],
},
{
name: "start",
description: "Start existing Traefik container on servers",
},
{ name: "stop", description: "Stop existing Traefik container on servers" },
{
name: "restart",
description: "Restart existing Traefik container on servers",
},
{
name: "details",
description: "Show details about Traefik container from servers",
},
{
name: "logs",
description: "Show log lines from Traefik on servers",
options: logOptions,
},
{
name: "remove",
description: "Remove Traefik container and image from servers",
},
{
name: "remove_container",
description: "Remove Traefik container from servers",
hidden: true,
},
{
name: "remove_image",
description: "Remove Traefik image from servers",
hidden: true,
},
{
name: "help",
description: "Describe subcommands or one specific subcommand",
args: {
name: "subcommand",
suggestions: [
{ name: "boot" },
{ name: "details" },
{ name: "help" },
{ name: "logs" },
{ name: "reboot" },
{ name: "remove" },
{ name: "restart" },
{ name: "start" },
{ name: "stop" },
],
},
},
],
};
const lockSubcommands: Fig.Subcommand = {
name: "lock",
description: "Manage the deploy lock",
icon: "🔒",
subcommands: [
{
name: "status",
description: "Report lock status",
},
{
name: "acquire",
description: "Acquire the deploy lock",
options: [
{
name: ["message", "m"],
args: {
name: "message",
description: "Message to set on the lock",
},
},
],
},
{
name: "release",
description: "Release the deploy lock",
},
],
};
const registrySubcommand: Fig.Subcommand = {
name: "registry",
description: "Login and -out of the image registry",
icon: "📦",
subcommands: [
{ name: "login", description: "Login to registry locally and remotely" },
{ name: "logout", description: "Log out of registry remotely" },
],
};
const pruneSubcommand: Fig.Subcommand = {
name: "prune",
icon: "🧹",
description: "Prune old application images and containers",
subcommands: [
{ name: "all", description: "Prune unused images and stopped containers" },
{ name: "images", description: "Prune unused images" },
{
name: "containers",
description: "Prune stopped containers, except last 5",
},
],
};
const buildSubcommand: Fig.Subcommand = {
name: "build",
description: "Build application image",
icon: "🏗️",
subcommands: [
{
name: "deliver",
description:
"Build app and push app image to registry then pull image on servers",
},
{
name: "push",
description: "Build and push app image to registry",
},
{
name: "pull",
description: "Pull app image from registry onto servers",
},
{
name: "create",
description: "Create a build setup",
},
{
name: "remove",
description: "Remove build setup",
},
{
name: "details",
description: "Show build setup",
},
],
};
const rootCommands: Fig.Subcommand[] = [
{
name: "setup",
icon: "🛠️",
description: "Setup all accessories and deploy app to servers",
},
{
name: "destinations",
description: "List all destinations",
options: [
{
name: ["--json", "-j"],
description: "Output as JSON",
},
],
},
{
name: "deploy",
description: "Deploy your app to a destination",
icon: "🚀",
priority: 80,
options: deployOptions,
},
{
name: "redeploy",
description:
"Deploy app to servers without bootstrapping servers, starting Traefik, pruning, and registry login",
icon: "🚀",
options: deployOptions,
},
{
name: "rollback",
description: "Rollback app to VERSION",
icon: "↩️",
args: {
name: "version",
// TODO: This can be autocompleted by doing `bin/kamal app containers` and parsing output
},
},
{
name: "details",
icon: "🔍",
description: "Show details about all containers",
},
{
name: "audit",
icon: "🔍",
description: "Show audit log from servers",
},
{
name: "config",
description: "Show combined config (including secrets!)",
},
{
name: "init",
icon: "🆕",
description: "Create config stub in config/deploy.yml and env stub in .env",
options: [
{
name: "bundle",
description: "Add Kamal to the Gemfile and create a bin/kamal binstub",
},
],
},
{
name: "envify",
description:
"Create .env by evaluating .env.erb (or .env.staging.erb -> .env.staging when using -d staging)",
options: [
{
// FIXME: REQUIRES MY FORK since it's not yet merged
hidden: true,
name: "template",
description: "Template to use",
args: { name: "template", template: "filepaths" },
},
],
},
{
name: "remove",
description:
"Remove Traefik, app, accessories, and registry session from servers",
icon: "🗑️",
options: [
{
name: ["--confirmed", "-y"],
description: "Proceed without confirmation question",
},
],
},
{
name: "version",
description: "Show Kamal version",
},
// mounted subcommands
accessorySubcommand,
appSubcommand,
buildSubcommand,
{
name: "healthcheck",
description: "Healthcheck application",
subcommands: [
{ name: "perform", description: "Health check current app version" },
],
},
lockSubcommands,
pruneSubcommand,
registrySubcommand,
{
name: "server",
description: "Bootstrap servers with curl and Docker",
subcommands: [
{ name: "bootstrap", description: "Set up Docker to run Kamal apps" },
],
},
traefikCommand,
];
const completionSpec: Fig.Spec = {
name: "kamal",
description: "Deploy web apps anywhere",
generateSpec: async (context, executeShellCommand) => {
// TODO: use logic from this comment https://github.com/withfig/autocomplete/pull/2112#discussion_r1412879317
const hasBinKamal =
(
await executeShellCommand({
command: "bash",
args: [
"-c",
`while [ ! -f "$PWD/bin/kamal" ] && [ "$PWD" != "/" ]; do cd ..; done; [ -f "$PWD/bin/kamal" ] && echo "true" || echo "false"`,
],
})
).stdout == "true";
return {
name: "kamal",
// if it has "bin/kamal" file show all commands
options: baseOptions,
subcommands: hasBinKamal
? rootCommands
: rootCommands.map((cmd) =>
cmd.name === "init" ? { ...cmd, priority: 100 } : cmd
),
};
},
};
export default completionSpec;