forked from LynnePatterson/0x4447-cli-potato
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
708 lines (570 loc) · 14.2 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
#!/usr/bin/env node
let npm = require('./package.json');
let aws = require('aws-sdk');
let term = require('terminal-kit').terminal;
let update = require('./modules/update');
let create = require('./modules/create');
let program = require('commander');
let request = require('request');
// _____ ______ _______ _______ _____ _ _ _____ _____
// / ____| | ____| |__ __| |__ __| |_ _| | \ | | / ____| / ____|
// | (___ | |__ | | | | | | | \| | | | __ | (___
// \___ \ | __| | | | | | | | . ` | | | |_ | \___ \
// ____) | | |____ | | | | _| |_ | |\ | | |__| | ____) |
// |_____/ |______| |_| |_| |_____| |_| \_| \_____| |_____/
//
//
// The CLI options for this app. At this moment we just support Version
//
program
.version(npm.version)
.option('-s, --source [type]', 'path to the folder to upload')
.option('-u, --update', 'perform an update')
.option('-c, --create', 'create a new site')
.option('-b, --bucket [type]', 'S3 bucket name')
.option('-d, --domain [type]', 'domain of the site')
.option('-a, --access_key [type]', 'The Access Key of your AWS Account')
.option('-t, --secret_key [type]', 'The Secret Access Key of your AWS Account');
//
// React when the user needs help
//
program.on('--help', function() {
//
// Just add an empty line at the end of the help to make the text more
// clear to the user
//
console.log("");
});
//
// Pass the user input to the module
//
program.parse(process.argv);
//
// Listen for key preses
//
term.on('key', function(name, matches, data ) {
//
// 1. If we detect CTR+C we kill the app
//
if(name === 'CTRL_C' )
{
//
// 1. Lets make a nice user experience and clean the terminal window
// before closing the app
//
term.clear();
//
// -> Kill the app
//
process.exit();
}
});
//
// Check if the user provided the dir source where to copy the file from
//
if(!program.source)
{
console.log('Missing source');
process.exit(0);
}
// __ __ _____ _ _
// | \/ | /\ |_ _| | \ | |
// | \ / | / \ | | | \| |
// | |\/| | / /\ \ | | | . ` |
// | | | | / ____ \ _| |_ | |\ |
// |_| |_| /_/ \_\ |_____| |_| \_|
//
//
// Before we start working, we clean the terminal window
//
term.clear();
//
// The main container that will be passed around in each chain to collect
// all the data and keep it in one place
//
let container = {
dir: process.cwd() + "/" + program.source,
region: 'us-east-1',
ask_for_credentials: true,
got_cli_credentials: false
};
//
// Start the chain
//
check_for_ec2_role(container)
.then(function(container) {
return check_for_codebuild_role(container);
}).then(function(container) {
return save_cli_data(container);
}).then(function(container) {
return display_the_welcome_message(container);
}).then(function(container) {
return ask_for_aws_key(container);
}).then(function(container) {
return ask_for_aws_secret(container);
}).then(function(container) {
return create_aws_objects(container);
}).then(function(container) {
return ask_what_to_do(container);
}).then(function(container) {
return crossroad(container);
}).then(function(container) {
term("\n\n");
term("\tDone!");
term("\n\n");
//
// -> Exit the app
//
process.exit();
}).catch(function(error) {
//
// 1. Clear the screen of necessary text
//
term.clear();
term("\n\n");
//
// 2. Show the error message
//
term.red("\t" + error);
term("\n\n");
//
// -> Exit the app
//
process.exit(-1);
});
// _____ _____ ____ __ __ _____ _____ ______ _____
// | __ \ | __ \ / __ \ | \/ | |_ _| / ____| | ____| / ____|
// | |__) | | |__) | | | | | | \ / | | | | (___ | |__ | (___
// | ___/ | _ / | | | | | |\/| | | | \___ \ | __| \___ \
// | | | | \ \ | |__| | | | | | _| |_ ____) | | |____ ____) |
// |_| |_| \_\ \____/ |_| |_| |_____| |_____/ |______| |_____/
//
//
// Query the EC2 Instance Metadata to find out if a IAM Role is attached
// to the instance, this way we can either ask the user for credentials
// or let the SDK use the Role attached to the EC2 Instance
//
function check_for_ec2_role(container)
{
return new Promise(function(resolve, reject) {
//
// 1. Prepare the request information
//
let options = {
url: 'http://169.254.169.254/latest/meta-data/iam/',
timeout: 1000
}
//
// 2. Make the request
//
request.get(options, function(error, data) {
//
// 1. We don't check for an error since when you use the
// timeout flag, request will throw an error when the time
// out happens.
//
// In this case we just don't want for this check to hang
// forever
//
//
// 2. Check to see if we got something back. If Potato
// is running on a EC2 instance we should get back the
// ROLE_NAME. And if that is the case we know that the
// AWS SDK will pick the credentials from the Role
// attached to the EC2 Instance.
//
if(data)
{
if(data.body.length > 0)
{
container.ask_for_credentials = false
}
}
//
// -> Move to the next chain
//
return resolve(container);
});
});
}
//
// Query the CodeBuild Docker Containerto find out if a IAM Role is attached
// to the container, this way we can either ask the user for credentials
// or let the SDK use the Role attached to the EC2 Instance
//
function check_for_codebuild_role(container)
{
return new Promise(function(resolve, reject) {
//
// 1. Prepare the request information
//
let options = {
url: 'http://169.254.170.2' + process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI,
timeout: 1000
}
//
// 2. Make the request
//
request.get(options, function(error, data) {
//
// 1. We don't check for an error since when you use the
// timeout flag, request will throw an error when the time
// out happens.
//
// In this case we just don't want for this check to hang
// forever
//
//
// 2. Check to see if we got something back. If Potato
// is running on a EC2 instance we should get back the
// ROLE_NAME. And if that is the case we know that the
// AWS SDK will pick the credentials from the Role
// attached to the EC2 Instance.
//
if(data)
{
if(data.body.length > 0)
{
//
// 1. Convert JSON to a JS Object
//
let body = JSON.parse(data.body);
//
// 2. Check to see if we got something back. If Potato
// is running inside a container in CodeBuild we should get
// back the RoleArn. And if that is the case we know that the
// AWS SDK will pick the credentials from the Role
// attached to CodeBuild Instance.
//
if(body.RoleArn)
{
container.ask_for_credentials = false
}
}
}
//
// -> Move to the next chain
//
return resolve(container);
});
});
}
//
// Check what type of information was passed and save it so we can skip
// some menu actions and run the command programmatically
//
function save_cli_data(container)
{
return new Promise(function(resolve, reject) {
//
// 1. Check if one of the options is enabled
//
if(program.create || program.update)
{
//
// 1. By default assume always that we are dealing with
// an update.
//
container.selection = 'Update';
//
// 2. Check if we need to change opinion.
//
if(program.create)
{
container.selection = 'Create';
}
}
//
// 2. Check if the bucket name was passed.
//
if(program.bucket)
{
container.bucket = program.bucket;
}
//
// 3. Check if the user passed the credentials in the CLI
//
if(program.access_key && program.secret_key)
{
//
// 1. Save the credentials passed in the CLI
//
container.aws_access_key_id = program.access_key;
container.aws_secret_access_key = program.secret_key;
//
// 2. Mark this run not to ask the user for the credentials
// since the information was passed in the CLI.
//
container.ask_for_credentials = false;
//
// 3. Let the rest of the chain know that we got the credentials
// from the CLI itself.
//
container.got_cli_credentials = true;
}
//
// -> Move to the next promise
//
return resolve(container);
});
}
//
// Draw on the screen a nice welcome message to show our user how
// cool we are :)
//
function display_the_welcome_message(container)
{
return new Promise(function(resolve, reject) {
//
// 1. Don't display the welcome message if we are dealing with the
// CLI
//
if(program.create || program.update)
{
//
// -> Move to the next promise
//
return resolve(container);
}
term("\n");
//
// 2. Set the options that will draw the banner
//
let options = {
flashStyle: term.brightWhite,
style: term.brightYellow,
delay: 20
}
//
// 3. The text to be displayed on the screen
//
let text = "\tStarting Potato";
//
// 4. Draw the text
//
term.slowTyping(text, options, function() {
//
// -> Move to the next step once the animation finishes drawing
//
return resolve(container);
});
});
}
//
// Make sure the Configuration file is actually available in the system
//
function ask_for_aws_key(container)
{
return new Promise(function(resolve, reject) {
//
// 1. Check if we have to ask the for credentials
//
if(!container.ask_for_credentials)
{
//
// -> Move to the next chain
//
return resolve(container);
}
term.clear();
term("\n");
//
// 2. Ask input from the user
//
term.yellow("\tPlease paste your AWS Access Key ID: ");
//
// 3. Listen for the user input
//
term.inputField({}, function(error, aws_access_key_id) {
term("\n");
term.yellow("\tLoading...");
//
// 1. Save the URL
//
container.aws_access_key_id = aws_access_key_id;
//
// -> Move to the next chain
//
return resolve(container);
});
});
}
//
// Make sure the Credentials file is actually available in the system
//
function ask_for_aws_secret(container)
{
return new Promise(function(resolve, reject) {
//
// 1. Check if we have to ask the for credentials
//
if(!container.ask_for_credentials)
{
//
// -> Move to the next chain
//
return resolve(container);
}
term.clear();
term("\n");
//
// 1. Ask input from the user
//
term.yellow("\tPlease paste your AWS Secret Access Key: ");
//
// 2. Listen for the user input
//
term.inputField({}, function(error, aws_secret_access_key) {
term("\n");
term.yellow("\tLoading...");
//
// 1. Save the URL
//
container.aws_secret_access_key = aws_secret_access_key;
//
// -> Move to the next chain
//
return resolve(container);
});
});
}
//
// After we get all the necessary credentials we use them to create
// all the AWS object used to programmatically make all the work
//
function create_aws_objects(container)
{
return new Promise(function(resolve, reject) {
//
// 1. Create basic settings for the constructor
//
let s3 = {
region: container.region
}
let cloudfront = {
region: container.region
}
let route53 = {
region: container.region
}
let acm = {
region: container.region
}
//
// 2. Update constructor settings if the user had to past the
// credentials
//
if(container.ask_for_credentials || container.got_cli_credentials)
{
s3.accessKeyId = container.aws_access_key_id
s3.secretAccessKey = container.aws_secret_access_key
cloudfront.accessKeyId = container.aws_access_key_id
cloudfront.secretAccessKey = container.aws_secret_access_key
route53.accessKeyId = container.aws_access_key_id
route53.secretAccessKey = container.aws_secret_access_key
acm.accessKeyId = container.aws_access_key_id
acm.secretAccessKey = container.aws_secret_access_key
}
//
// 3. Create the AWS S3 object
//
container.s3 = new aws.S3(s3);
//
// 4. Create the AWS CloudFront object
//
container.cloudfront = new aws.CloudFront(cloudfront);
//
// 5. Create the AWS Route 53 object
//
container.route53 = new aws.Route53(route53);
//
// 6. Create the AWS Certificate Manager object
//
container.acm = new aws.ACM(acm);
//
// -> Move to the next chain
//
return resolve(container);
});
}
//
// Ask the user what to do, since this app can create or update a project
//
function ask_what_to_do(container)
{
return new Promise(function(resolve, reject) {
//
// 1. Skip this view if the information was already passed in the
// CLI
//
if(container.selection)
{
//
// -> Move to the next chain
//
return resolve(container);
}
term.clear();
term("\n");
term.yellow("\tUpdate or create a new website?");
term('\n');
//
// 2. Default settings how to draw the ASCII menu
//
let options = {
leftPadding: "\t"
};
//
// 3. The two options to show the user
//
let question = [
'Update',
'Create'
];
//
// 4. Draw the drop down menu
//
term.singleColumnMenu(question, options, function(error, res) {
term("\n");
term("\n");
term.yellow("\tLoading...");
term("\n");
term("\n");
//
// 1. Get the Property name based on the user selection
//
let selection = question[res.selectedIndex];
//
// 2. Save the user selection
//
container.selection = selection;
//
// -> Move to the next chain
//
return resolve(container);
});
});
}
// ______ _ _ _ _ _____ _______ _____ ____ _ _ _____
// | ____|| | | || \ | | / ____||__ __||_ _|/ __ \ | \ | | / ____|
// | |__ | | | || \| || | | | | | | | | || \| || (___
// | __| | | | || . ` || | | | | | | | | || . ` | \___ \
// | | | |__| || |\ || |____ | | _| |_| |__| || |\ | ____) |
// |_| \____/ |_| \_| \_____| |_| |_____|\____/ |_| \_||_____/
//
//
// This is a function that depending on the option selected by the user
// returns just one specific promises that will then perform the
// action selected by the user.
//
function crossroad(container)
{
if(container.selection == 'Update')
{
return update(container);
}
if(container.selection == 'Create')
{
return create(container);
}
}