-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.js
168 lines (144 loc) · 5.35 KB
/
cmd.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
#!/usr/bin/env node
const cdkutils = require('./utils/cdkutils');
let args = process.argv.slice(2);
const fs = require('fs');
const path = require('path');
const term = require('terminal-kit').terminal;
term.on('key', function (name, matches, data) {
if (name === 'CTRL_C') { terminate(); }
});
var AWS = require('aws-sdk');
let ec2 = null;
function terminate() {
term.grabInput(false);
setTimeout(function () { process.exit() }, 100);
}
const KEYPAIR_NAME = 'h0ck-ec2-keypair'
const KEYPAIR_PATH = './' + KEYPAIR_NAME + '.keypair';
function createKeyPair() {
ec2.createKeyPair({ KeyName: KEYPAIR_NAME }, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log('Storing keypair on', KEYPAIR_PATH)
fs.writeFileSync(KEYPAIR_PATH, data.KeyMaterial)
}
});
}
function deleteKeyPairIfExists() {
ec2.describeKeyPairs({ KeyNames: [KEYPAIR_NAME] }, function (err, data) {
if (!err) {
if (fs.existsSync(KEYPAIR_PATH)) {
fs.unlinkSync(KEYPAIR_PATH)
}
ec2.deleteKeyPair({ KeyName: KEYPAIR_NAME }, function (err, data) {
if (err) {
console.error("Error deleting keypair")
}
})
}
});
}
function createKeyPairIfNotExists() {
ec2.describeKeyPairs({ KeyNames: [KEYPAIR_NAME] }, function (err, data) {
if (err) {
if (err.code === 'InvalidKeyPair.NotFound') {
createKeyPair();
}
else {
console.log("Error", err);
}
} else {
if (fs.existsSync(KEYPAIR_PATH)) {
console.log('Keypair file exists and keypair exists on amazon. Not creating it');
} else {
ec2.deleteKeyPair({ KeyName: KEYPAIR_NAME }, function (err, data) {
if (err) {
console.error("Error deleting keypair")
}
else {
createKeyPair();
}
})
}
}
});
}
function checkEnvVars() {
let listToCheck = ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'CDK_DEFAULT_ACCOUNT', 'CDK_DEFAULT_REGION', 'EXTERNAL_IP'];
return new Promise(async (resolve, reject) => {
let allValid = true;
for (let variable of listToCheck) {
if (!process.env[variable]) {
allValid = false;
term.yellow(variable + " env var not setted, please set and restart or write the value: \n");
let input = await term.inputField({}).promise;
term("\n")
process.env[variable] = input;
await checkEnvVars();
resolve()
}
}
if (allValid) {
resolve()
}
})
}
async function preconfig() {
await checkEnvVars();
AWS.config.update({ region: process.env.CDK_DEFAULT_REGION, credentials: { accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY } });
ec2 = new AWS.EC2({ apiVersion: '2016-11-15' });
}
async function deploy() {
await preconfig();
createKeyPairIfNotExists();
await cdkutils.deploy('/bin/infrastructure.js', false, ['EC2Stack', 'LambdasStack'], false).then(async function (outputs) {
let infrastructureJson = { "h0ck_core": "http://" + outputs?.EC2Stack?.h0ckec2publicip + ":7001" };
fs.writeFileSync(path.join(__dirname, "/frontfiles/infrastructure.json"), JSON.stringify(infrastructureJson));
let data = fs.readFileSync(path.join(__dirname, "/frontfiles/infrastructure.json"), 'utf8');
await cdkutils.deploy('/bin/infrastructurefront.js', true, ['FrontStack'], { force: true }, true).then(outputs => {
console.log("Deployment finished")
})
});
}
async function destroy() {
await preconfig();
deleteKeyPairIfExists();
await cdkutils.destroy('/bin/infrastructurefront.js', false);
await cdkutils.destroy('/bin/infrastructure.js', true);
}
if (args.length === 0) {
//Dynamic execution
const TERM_OPTIONS_TEXT = {
DEPLOY: 'Deploy',
DESTROY: 'Destroy'
}
const TERM_OPTIONS_ACTIONS = {
'Deploy': deploy,
'Destroy': destroy,
}
let package = require('./package.json');
term.blue("==================================")("\n")
term.blue("== ").cyan("Welcome to H0ck Framework. ").blue("==")("\n")
term.blue("==================================")("\n")
term.green("Infrastructure Version: " + package.version)("\n");
term.cyan("More info of this Open Source Framework in ").yellow("https://github.com/H0ck")("\n")
term.blue("==================================")("\n")
term("\n")
term.bold("Select what action do you want to do with the H0ck Framework:")("\n")
term.singleColumnMenu(Object.values(TERM_OPTIONS_TEXT), {}, async function (err, response) {
await TERM_OPTIONS_ACTIONS[response.selectedText]()
term.processExit()
})
} else {
//Call the corresponding converter removing the converter name parameter.
process.env.EXTERNAL_IP = "78.30.20.108";
if (args[0] === 'deploy') {
deploy();
} else if (args[0] === 'destroy') {
destroy();
}
else {
console.log("Command", args[0], "not found");
}
}