-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·780 lines (707 loc) · 21.8 KB
/
index.js
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#!/usr/bin/env node
import { ECS } from "@aws-sdk/client-ecs";
import { fromIni } from "@aws-sdk/credential-providers";
import { Command } from "commander";
import inquirer from "inquirer";
import pino from "pino";
import dotenv from "dotenv";
import chalk from "chalk";
import figlet from "figlet";
import ora from "ora";
import Conf from "conf";
import { pastel } from "gradient-string";
import { spawn, execSync } from "child_process";
import fs from "fs";
import path from "path";
import os from "os";
import ini from "ini";
import { AWS_REGIONS } from "./regions.js";
// Load environment variables
dotenv.config();
// ---------------------------------------------------------------------------
// Configuration Management using Conf
// ---------------------------------------------------------------------------
const config = new Conf({
projectName: "taskonaut",
schema: {
awsProfile: {
type: "string",
default: "default",
},
awsRegion: {
type: "string",
default: "us-east-1",
},
lastUsedCluster: {
type: "string",
default: "",
},
awsProfiles: {
type: "array",
default: [],
},
lastProfileSync: {
type: "number",
default: 0,
},
},
});
const SYNC_INTERVAL = 1000 * 60 * 60; // 1 hour
// ---------------------------------------------------------------------------
// Banner & Logger Setup
// ---------------------------------------------------------------------------
console.log(
pastel.multiline(
figlet.textSync("taskonaut", {
font: "ANSI Shadow",
horizontalLayout: "full",
}),
),
);
const logger = pino({
transport: {
target: "pino-pretty",
options: {
colorize: true,
levelFirst: true,
translateTime: "HH:MM:ss",
ignore: "pid,hostname",
},
},
});
// ---------------------------------------------------------------------------
// AWS Profiles Management
// ---------------------------------------------------------------------------
/**
* Parses AWS profiles from credentials and config files.
* @returns {string[]} Array of AWS profile names.
*/
function parseAwsProfiles() {
const profiles = new Set();
const credentialsPath = path.join(os.homedir(), ".aws", "credentials");
if (fs.existsSync(credentialsPath)) {
try {
const content = fs.readFileSync(credentialsPath, "utf-8");
const parsed = ini.parse(content);
Object.keys(parsed).forEach((profile) => profiles.add(profile));
} catch (err) {
logger.error(chalk.red(`Error parsing credentials file: ${err.message}`));
}
}
const configPath = path.join(os.homedir(), ".aws", "config");
if (fs.existsSync(configPath)) {
try {
const content = fs.readFileSync(configPath, "utf-8");
const parsed = ini.parse(content);
Object.keys(parsed).forEach((profile) => {
const profileName = profile.replace("profile ", "");
profiles.add(profileName);
});
} catch (err) {
logger.error(chalk.red(`Error parsing config file: ${err.message}`));
}
}
return Array.from(profiles);
}
/**
* Synchronizes AWS profiles and updates the configuration.
* @returns {Promise<string[]>} Array of AWS profile names.
*/
async function syncAwsProfiles() {
const spinner = ora("Syncing AWS profiles...").start();
try {
const profilesList = parseAwsProfiles();
config.set("awsProfiles", profilesList);
config.set("lastProfileSync", Date.now());
spinner.succeed(`Found ${profilesList.length} AWS profiles`);
return profilesList;
} catch (err) {
spinner.fail("Failed to sync AWS profiles");
logger.error(err, chalk.red("Error syncing profiles"));
throw err;
}
}
/**
* Retrieves AWS profiles from configuration or syncs if necessary.
* @returns {Promise<string[]>} Array of AWS profile names.
*/
async function getAwsProfiles() {
const lastSync = config.get("lastProfileSync");
if (Date.now() - lastSync > SYNC_INTERVAL) {
return await syncAwsProfiles();
}
return config.get("awsProfiles");
}
// ---------------------------------------------------------------------------
// AWS ECS Client Initialization
// ---------------------------------------------------------------------------
/**
* Initializes the AWS ECS client with the selected profile and region.
* @returns {Promise<ECS>} An instance of the AWS ECS client.
*/
const initAWS = async () => {
try {
const profiles = await getAwsProfiles();
const currentProfile = config.get("awsProfile");
if (!profiles.includes(currentProfile)) {
logger.warn(
chalk.yellow(`Profile ${currentProfile} not found, please reconfigure`),
);
throw new Error("Invalid AWS profile");
}
const region = config.get("awsRegion");
logger.info(
chalk.dim(`Using AWS Profile: ${currentProfile} and Region: ${region}`),
);
const credentials = await fromIni({ profile: currentProfile })();
return new ECS({ region, credentials });
} catch (err) {
logger.error(chalk.red(err.message));
throw err;
}
};
// ---------------------------------------------------------------------------
// AWS ECS Cluster, Task, and Container Management
// ---------------------------------------------------------------------------
/**
* Lists ECS clusters along with their service, task, and container instance counts.
* @param {ECS} ecs - AWS ECS client.
* @returns {Promise<Array<{clusterName: string, servicesCount: number, tasksCount: number, containerInstancesCount: number}>>} Clusters with details.
*/
async function listClusters(ecs) {
try {
const spinner = ora("Fetching clusters...").start();
const { clusterArns } = await ecs.listClusters({});
spinner.succeed("Clusters fetched");
if (!clusterArns || clusterArns.length === 0) {
logger.warn(chalk.yellow("No clusters found."));
return [];
}
const clusters = await Promise.all(
clusterArns.map(async (arn) => {
const clusterName = arn.split("/").pop();
let servicesCount = 0;
let tasksCount = 0;
let containerInstancesCount = 0;
try {
const servicesResult = await ecs.listServices({
cluster: clusterName,
});
servicesCount = servicesResult.serviceArns
? servicesResult.serviceArns.length
: 0;
} catch (err) {
logger.error(
chalk.red(
`Error fetching services for cluster ${clusterName}: ${err.message}`,
),
);
}
try {
const tasksResult = await ecs.listTasks({ cluster: clusterName });
tasksCount = tasksResult.taskArns ? tasksResult.taskArns.length : 0;
} catch (err) {
logger.error(
chalk.red(
`Error fetching tasks for cluster ${clusterName}: ${err.message}`,
),
);
}
try {
const containerInstancesResult = await ecs.listContainerInstances({
cluster: clusterName,
});
containerInstancesCount =
containerInstancesResult.containerInstanceArns
? containerInstancesResult.containerInstanceArns.length
: 0;
} catch (err) {
logger.error(
chalk.red(
`Error fetching container instances for cluster ${clusterName}: ${err.message}`,
),
);
}
return {
clusterName,
servicesCount,
tasksCount,
containerInstancesCount,
};
}),
);
return clusters;
} catch (err) {
logger.error(chalk.red(err.message));
throw err;
}
}
/**
* Prompts user to select an ECS cluster.
* @param {ECS} ecs - AWS ECS client.
* @returns {Promise<string>} Selected cluster name.
*/
async function selectCluster(ecs) {
const clusters = await listClusters(ecs);
if (!clusters || clusters.length === 0) {
logger.warn(chalk.yellow("No clusters found."));
throw new Error("No clusters available");
}
const { cluster } = await inquirer.prompt([
{
type: "list",
name: "cluster",
message: chalk.blue("Select ECS cluster:"),
prefix: "🚀",
choices: clusters.map((c) => ({
name:
chalk.green(c.clusterName) +
chalk.yellow(
` (Services: ${c.servicesCount}, Tasks: ${c.tasksCount}, Container Instances: ${c.containerInstancesCount})`,
),
value: c.clusterName,
})),
},
]);
return cluster;
}
/**
* Prompts user to select a task within a cluster, optionally allowing going back.
* @param {ECS} ecs - AWS ECS client.
* @param {string} cluster - Cluster name.
* @param {boolean} allowBack - Whether to allow going back.
* @returns {Promise<string>} Selected task ARN or '__BACK__'.
*/
async function selectTask(ecs, cluster, allowBack = false) {
try {
const spinner = ora("Fetching tasks...").start();
const { taskArns } = await ecs.listTasks({ cluster });
if (!taskArns || taskArns.length === 0) {
spinner.fail("No tasks found");
logger.warn(chalk.yellow("No tasks found in cluster."));
throw new Error("No tasks available in cluster");
}
const { tasks } = await ecs.describeTasks({
cluster,
tasks: taskArns,
});
spinner.succeed("Tasks fetched");
// Sort tasks alphabetically by task definition name.
tasks.sort((a, b) => {
const aName = (a.taskDefinitionArn.split("/").pop() || "").toLowerCase();
const bName = (b.taskDefinitionArn.split("/").pop() || "").toLowerCase();
return aName.localeCompare(bName);
});
const choices = tasks.map((task) => {
const taskDefName = task.taskDefinitionArn.split("/").pop();
const taskId = task.taskArn.split("/").pop();
const shortTaskId = taskId.slice(-6);
const startedAt = task.startedAt
? new Date(task.startedAt).toLocaleString()
: "N/A";
return {
name: `${chalk.green(taskDefName)} ${chalk.yellow(
`(ID: ${shortTaskId}, ${task.lastStatus}, started at: ${startedAt})`,
)}`,
value: task.taskArn,
};
});
if (allowBack) {
choices.unshift({
name: chalk.blue("← Go Back"),
value: "__BACK__",
});
}
const { taskArn } = await inquirer.prompt([
{
type: "list",
name: "taskArn",
message: chalk.blue("Select task:"),
prefix: "📦",
choices,
loop: false,
pageSize: choices.length,
},
]);
return taskArn;
} catch (err) {
logger.error(chalk.red(err.message));
throw err;
}
}
/**
* Retrieves task details.
* @param {ECS} ecs - AWS ECS client.
* @param {string} cluster - Cluster name.
* @param {string} taskArn - Task ARN.
* @returns {Promise<object>} Task details.
*/
async function getTaskDetails(ecs, cluster, taskArn) {
try {
const { tasks } = await ecs.describeTasks({
cluster,
tasks: [taskArn],
});
if (!tasks || tasks.length === 0) {
throw new Error("Task not found");
}
return tasks[0];
} catch (err) {
logger.error(chalk.red(err.message));
throw err;
}
}
/**
* Prompts user to select a container within a task, optionally allowing to go back.
* @param {ECS} ecs - AWS ECS client.
* @param {string} cluster - Cluster name.
* @param {string} taskArn - Task ARN.
* @param {boolean} allowBack - Whether to allow going back.
* @returns {Promise<string>} Selected container name or '__BACK__'.
*/
async function selectContainer(ecs, cluster, taskArn, allowBack = false) {
const spinner = ora("Fetching container details...").start();
const task = await getTaskDetails(ecs, cluster, taskArn);
const containers = task.containers;
spinner.succeed("Container details fetched");
let choices = [];
if (containers.length === 1 && !allowBack) {
logger.info(chalk.dim("Single container detected, auto-selecting..."));
return containers[0].name;
} else {
choices = containers.map((container) => ({
name: `${chalk.green(container.name)} ${chalk.yellow(
`(${container.lastStatus})`,
)}`,
value: container.name,
}));
if (allowBack) {
choices.unshift({
name: chalk.blue("← Go Back"),
value: "__BACK__",
});
}
}
const { containerName } = await inquirer.prompt([
{
type: "list",
name: "containerName",
message: chalk.blue("Select container:"),
prefix: "🐳",
choices,
},
]);
return containerName;
}
/**
* Executes a command on the selected container.
* @param {string} cluster - Cluster name.
* @param {string} taskArn - Task ARN.
* @param {string} containerName - Container name.
* @returns {Promise<number>} Exit code.
*/
async function executeCommand(cluster, taskArn, containerName) {
return new Promise((resolve, reject) => {
logger.info(chalk.dim("Starting shell session..."));
const childProcess = spawn(
"aws",
[
"ecs",
"execute-command",
"--profile",
config.get("awsProfile"),
"--region",
config.get("awsRegion"),
"--cluster",
cluster,
"--task",
taskArn,
"--container",
containerName,
"--command",
"/bin/sh",
"--interactive",
],
{
stdio: "inherit",
},
);
const signals = ["SIGINT", "SIGTERM", "SIGQUIT"];
const signalHandlers = {};
const cleanup = () => {
logger.info(chalk.yellow("📤 Cleaning up ECS session..."));
childProcess.kill("SIGTERM");
signals.forEach((signal) => {
process.removeListener(signal, signalHandlers[signal]);
});
};
signals.forEach((signal) => {
signalHandlers[signal] = () => cleanup();
process.on(signal, signalHandlers[signal]);
});
childProcess.on("error", (err) => {
logger.error(chalk.red(err.message));
cleanup();
reject(err);
});
childProcess.on("exit", (code) => {
signals.forEach((signal) => {
process.removeListener(signal, signalHandlers[signal]);
});
logger.info(
chalk.green(`✨ Session ended with exit code ${chalk.bold(code)}`),
);
resolve(code);
});
});
}
// ---------------------------------------------------------------------------
// Diagnostic Functions
// ---------------------------------------------------------------------------
/**
* Checks if AWS CLI is installed.
* @returns {boolean} True if installed, false otherwise.
*/
function checkAwsCliInstalled() {
try {
execSync("aws --version", { stdio: "ignore" });
return true;
} catch (err) {
logger.error(chalk.red(err.message));
return false;
}
}
/**
* Checks if Session Manager Plugin is installed.
* @returns {boolean} True if installed, false otherwise.
*/
function checkSessionManagerPluginInstalled() {
try {
execSync("session-manager-plugin --version", { stdio: "ignore" });
return true;
} catch (err) {
logger.error(chalk.red(err.message));
return false;
}
}
/**
* Checks if AWS credentials are configured.
* @returns {boolean} True if configured, false otherwise.
*/
function checkAwsCredentials() {
const credentialsPath = path.join(os.homedir(), ".aws", "credentials");
const configPath = path.join(os.homedir(), ".aws", "config");
return fs.existsSync(credentialsPath) || fs.existsSync(configPath);
}
/**
* Checks if the configured AWS profile is valid.
* @returns {boolean} True if valid, false otherwise.
*/
function checkAwsProfileConfigured() {
const profiles = config.get("awsProfiles");
const currentProfile = config.get("awsProfile");
return profiles.includes(currentProfile);
}
/**
* Performs diagnostics to check environment setup.
*/
async function performDiagnostics() {
let allGood = true;
logger.info(chalk.blue.bold("🏃 Running Diagnostics..."));
if (!checkAwsCliInstalled()) {
logger.error(chalk.red("❌ AWS CLI is not installed."));
allGood = false;
} else {
logger.info(chalk.green("✅ AWS CLI is installed."));
}
if (!checkSessionManagerPluginInstalled()) {
logger.error(chalk.red("❌ Session Manager Plugin is not installed."));
allGood = false;
} else {
logger.info(chalk.green("✅ Session Manager Plugin is installed."));
}
if (!checkAwsCredentials()) {
logger.error(chalk.red("❌ AWS credentials are not configured."));
allGood = false;
} else {
logger.info(chalk.green("✅ AWS credentials are configured."));
}
if (!checkAwsProfileConfigured()) {
logger.error(
chalk.red(
`❌ AWS profile '${config.get("awsProfile")}' is not configured.`,
),
);
allGood = false;
} else {
logger.info(
chalk.green(
`✅ AWS profile '${config.get("awsProfile")}' is configured.`,
),
);
}
if (allGood) {
logger.info(
chalk.green(
"💯 All checks passed! Your environment is set up correctly.",
),
);
} else {
logger.warn(
chalk.yellow(
"😭 Errors were detected. Please address them and try again.",
),
);
}
}
// ---------------------------------------------------------------------------
// CLI Program Setup using Commander
// ---------------------------------------------------------------------------
const program = new Command();
program
.name(chalk.cyan("taskonaut"))
.description(chalk.yellow("✨ Interactive ECS task executor"))
.addHelpText("after", chalk.dim("Example: taskonaut "))
.action(async () => {
try {
const ecs = await initAWS();
let cluster = await selectCluster(ecs);
let taskArn, containerName;
// Allow backward navigation on task and container selection.
while (true) {
taskArn = await selectTask(ecs, cluster, true);
if (taskArn === "__BACK__") {
cluster = await selectCluster(ecs);
continue;
}
while (true) {
containerName = await selectContainer(ecs, cluster, taskArn, true);
if (containerName === "__BACK__") {
break; // Go back to task selection.
}
logger.info(
chalk.green(
`🚀 Connecting to container ${chalk.bold(containerName)}...`,
),
);
await executeCommand(cluster, taskArn, containerName);
return; // End after session completes.
}
}
} catch (err) {
logger.error(chalk.red(err.message));
process.exit(1);
}
});
program
.command("config")
.description("Manage configuration settings")
.addCommand(
new Command("set")
.description("Set AWS profile and region")
.addHelpText("after", chalk.dim("Example: taskonaut config set"))
.action(async () => {
try {
const spinner = ora("Loading AWS profiles...").start();
const profiles = await getAwsProfiles();
spinner.succeed("AWS profiles loaded");
const { profile } = await inquirer.prompt([
{
type: "list",
name: "profile",
message: chalk.blue("Select AWS Profile:"),
prefix: "🔑",
choices: profiles.map((p) => ({
name: chalk.green(p),
value: p,
})),
default: config.get("awsProfile"),
},
]);
const { region } = await inquirer.prompt([
{
type: "list",
name: "region",
message: chalk.blue("Select AWS Region:"),
prefix: "🌎",
choices: AWS_REGIONS.map((r) => ({
name: chalk.green(r),
value: r,
})),
default: config.get("awsRegion"),
},
]);
config.set("awsProfile", profile);
config.set("awsRegion", region);
logger.info(chalk.green("✨ Configuration saved successfully!"));
} catch (err) {
logger.error(chalk.red(err.message));
process.exit(1);
}
}),
)
.addCommand(
new Command("show")
.alias("path")
.description("Show configuration path and current values")
.action(async () => {
try {
const spinner = ora("Reading configuration...").start();
const configDetails = {
path: config.path,
values: config.store,
};
spinner.succeed("Configuration loaded");
console.log("\n" + chalk.blue.bold("Configuration Details:"));
console.log(chalk.dim("Path:"), chalk.green(configDetails.path));
console.log(chalk.dim("Values:"));
console.log(
chalk.green(JSON.stringify(configDetails.values, null, 2)),
);
} catch (err) {
logger.error(chalk.red(err.message));
process.exit(1);
}
}),
)
.addCommand(
new Command("cleanup")
.alias("clear")
.description("Remove all stored configuration")
.action(async () => {
try {
const { confirm } = await inquirer.prompt([
{
type: "confirm",
name: "confirm",
message: chalk.yellow(
"⚠️ Are you sure you want to remove all stored configuration?",
),
default: false,
},
]);
if (confirm) {
const spinner = ora("Cleaning up configuration...").start();
config.clear();
spinner.succeed("Configuration cleared successfully");
} else {
logger.info(chalk.dim("Cleanup cancelled"));
}
} catch (err) {
logger.error(chalk.red(err.message));
process.exit(1);
}
}),
);
program
.command("doctor")
.description("Run diagnostics to check your environment setup")
.action(async () => {
try {
await performDiagnostics();
} catch (err) {
logger.error(chalk.red("Diagnostics failed: " + err.message));
process.exit(1);
}
});
program.parse();