-
Notifications
You must be signed in to change notification settings - Fork 22
/
getenv.js
382 lines (371 loc) · 19.1 KB
/
getenv.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
'use strict';
var questions = require('./questions/qustions').getQustions();
var inquirer = require('inquirer');
var environment = process.env.npm_config_e ? process.env.npm_config_e : 'terraform';
var fs = require('fs');
var obj = {};
var text;
function writeToFile(destPath, messageId) {
fs.open(destPath, 'w+', function (err, fd) {
if (err) {
console.log('Error opening file', err);
return;
}
fs.write(fd, messageId, function (err) {
if (err) {
console.log('Could not write to file', err);
}
else {
console.log('written to file', destPath);
}
});
});
}
function getSqsVariables() {
var temp = '';
var sqsVals;
try {
sqsVals = require('./terraform.json');
} catch (e) {
console.log('------------------------------------');
console.log('Oops ! Seems like you haven\'t generated the SQS configuration yet. Do that & try again.');
console.log('------------------------------------');
}
if (sqsVals) {
for (var tkey in sqsVals) {
switch (tkey) {
case 'AWS_SQS_URL':
temp += '\n\n AWS_SQS_URL = ' + sqsVals[tkey];
break;
case 'AWS_SQS_REGION':
temp += '\n AWS_SQS_REGION = ' + sqsVals[tkey];
break;
case 'AWS_SQS_ACCESS_KEY_ID':
temp += '\n AWS_SQS_ACCESS_KEY_ID = ' + sqsVals[tkey];
break;
case 'AWS_SQS_SECRET_ACCESS_KEY':
temp += '\n AWS_SQS_SECRET_ACCESS_KEY = ' + sqsVals[tkey];
break;
default:
break;
}
}
}
return temp;
}
function generateEnv(data) {
switch (environment) {
case 'web':
text = '';
for (var key in data) {
if (data.hasOwnProperty(key)) {
switch (key) {
case 'general':
for (var gKey in data[key]) {
if (data[key].hasOwnProperty(gKey)) {
switch (gKey) {
case 'env':
text += '\n NODE_ENV = ' + data[key][gKey];
break;
case 'scheme':
text += '\n SCHEME = ' + data[key][gKey];
break;
case 'external_ip':
text += '\n\n ##\
\n # To accommodate redirects, the code needs to know the external facing URL (FQDN or IP)\
\n ##';
text += '\n VM_EXTERNAL_IP = ' + data[key][gKey];
break;
case 'worker_type':
text += '\n WORKER_TYPE = ' + data[key][gKey];
break;
}
}
}
break;
case 'aws':
text += '\n\n # AWS account credentials\n'
for (var aKey in data[key]) {
if (data[key].hasOwnProperty(aKey)) {
switch (aKey) {
case 'aws_access_key_id':
text += '\n AWS_ACCESS_KEY_ID = ' + data[key][aKey];
break;
case 'aws_secret_access_key':
text += '\n AWS_SECRET_ACCESS_KEY = ' + data[key][aKey];
break;
case 'aws_bucket':
text += '\n\n ## \n # A seed file contains information to populate the database. It contains: \n # - roles, usernames, passwords \n # - integration tokens gained via a successful oauth handshake, \n # used to communicate with a point-of-sale (Vend) on behalf of retailers \n # - suppliers, outlets \n # - anything and everything which we cannot add via a UI for onboarding warehouse users. \n ##\n';
text += '\n AWS_BUCKET = ' + data[key][aKey];
break;
case 'aws_default_region':
text += '\n AWS_DEFAULT_REGION = ' + data[key][aKey];
break;
case 'aws_key':
text += '\n AWS_KEY = ' + data[key][aKey];
break;
}
}
}
break;
case 'vend':
text += '\n\n ##\
\n # Whomever is running warehouse, needs to be identified as a unique player by the point-of-sale (Vend)\
\n # To get these, you can:\
\n # - Register as a developer or Sign In: https://developers.vendhq.com/\
\n # - View or Add Application\
\n # - https://developers.vendhq.com/developer/applications\
\n ##\
\n\n # your identification credentials given by Vend\n';
for (var vKey in data[key]) {
if (data[key].hasOwnProperty(vKey)) {
switch (vKey) {
case 'vend_client_id':
text += '\n VEND_CLIENT_ID = ' + data[key][vKey];
break;
case 'vend_client_secret':
text += '\n VEND_CLIENT_SECRET = ' + data[key][vKey];
break;
}
}
}
break;
case 'oauth':
for (var oKey in data[key]) {
if (data[key].hasOwnProperty(oKey)) {
switch (oKey) {
case 'oauth_secret_key':
text += '\n\n OAUTH SECRET KEY = ' + data[key][oKey];
break;
}
}
}
break;
case 'workers':
text += '\n\n ## \n # Each worker takes care of a different task. \n # Sometimes workers are renamed, so in order to avoid code changes, \n # those worker names can be configured here, instead.\n # DO NOT TOUCH - unless you are a developer.\n ## \n';
for (var zKey in data[key]) {
if (data[key].hasOwnProperty(zKey)) {
switch (zKey) {
case 'GENERATE_STOCK_ORDER_WORKER':
text += '\n GENERATE_STOCK_ORDER_WORKER = ' + data[key][zKey];
break;
case 'IMPORT_STOCK_ORDER_TO_POS':
text += '\n IMPORT_STOCK_ORDER_TO_POS = ' + data[key][zKey];
break;
case 'IMPORT_STOCK_ORDER_TO_WAREHOUSE_WITHOUT_SUPPLIER':
text += '\n IMPORT_STOCK_ORDER_TO_WAREHOUSE_WITHOUT_SUPPLIER = ' + data[key][zKey];
break;
case 'IMPORT_STOCK_ORDER_TO_WAREHOUSE':
text += '\n IMPORT_STOCK_ORDER_TO_WAREHOUSE = ' + data[key][zKey];
break;
case 'REMOVE_UNFULFILLED_PRODUCTS_WORKER':
text += '\n REMOVE_UNFULFILLED_PRODUCTS_WORKER = ' + data[key][zKey];
break;
case 'REMOVE_UNRECEIVED_PRODUCTS_WORKER':
text += '\n REMOVE_UNRECEIVED_PRODUCTS_WORKER = ' + data[key][zKey];
break;
case 'STOCK_ORDER_WORKER':
text += '\n STOCK_ORDER_WORKER = ' + data[key][zKey];
break;
}
}
}
break;
}
}
}
if (data.end.completion) {
text += getSqsVariables();
writeToFile('.env', text);
console.log('------------------------------------');
console.log('Congratulations ! you have successfully generated environment configuration for Web. You can delete the \'aws.json\' from your project.');
console.log('------------------------------------');
}
break;
case 'terraform':
text = '';
fs.writeFile('aws.json', JSON.stringify(data.answers, null, '\t'), function () {
console.log('------------------------------------');
console.log('we have saved these credentials for future purpose in aws.json');
console.log('------------------------------------');
});
for (var tKey in data) {
if (data.hasOwnProperty(tKey)) {
switch (tKey) {
case 'answers':
for (var sKey in data[tKey]) {
if (data[tKey].hasOwnProperty(sKey)) {
switch (sKey) {
case 'aws_iam_access_key':
text += '\n aws_iam_access_key = \"' + data[tKey][sKey] + '\"';
break;
case 'aws_iam_secret_key':
text += '\n aws_iam_secret_key = \"' + data[tKey][sKey] + '\"';
break;
case 'aws_region':
text += '\n aws_region = \"' + data[tKey][sKey] + '\"';
break;
case 'Q':
text += '\n Q = \"' + data[tKey][sKey] + '\"';
break;
case 'DLQ':
text += '\n DLQ = \"' + data[tKey][sKey] + '\"';
break;
}
}
}
break;
default:
break;
}
}
}
if (data.end.completion) {
writeToFile('terraform/terraform.tfvars', text);
console.log('------------------------------------');
console.log('Congratulations ! You have successfully configured the SQS infrastructure. Now you can generate the environment configuration again for web,worker & worker2 respectively.');
console.log('------------------------------------');
}
break;
case 'worker':
text = '';
for (var wkey in data) {
if (data.hasOwnProperty(wkey)) {
switch (wkey) {
case 'answers':
for (var gKey1 in data[wkey]) {
if (data[wkey].hasOwnProperty(gKey1)) {
switch (gKey1) {
case 'db_url':
text += '\n DbUrl = ' + data[wkey][gKey1];
break;
case 'cache_url':
text += '\n # cache is used when we want to:\n # - limit the memory consumed by workers\n # - reuse common data across multiple worker runs\n # DO NOT TOUCH - unless you are a developer/devops';
text += '\n cacheUrl = ' + data[wkey][gKey1];
break;
case 'AWS_SQS_URL_2':
text += '\n AWS_SQS_URL_2 = ' + data[wkey][gKey1];
break;
case 'WORKERS_VERSION':
text += '\n # Important to determine which set of workers are being called\n # to route the operations properly. This is for the time-being\n # until all workers are transitioned to v2';
text += '\n WORKERS_VERSION = ' + data[wkey][gKey1];
break;
}
}
}
break;
}
}
}
if (data.end.completion) {
text += getSqsVariables();
writeToFile('worker.env', text);
console.log('------------------------------------');
console.log('Congratulations ! you have successfully generated environment configuration for Worker.');
console.log('------------------------------------');
}
break;
case 'worker2':
text = '';
for (var wkey2 in data) {
if (data.hasOwnProperty(wkey2)) {
switch (wkey2) {
case 'answers':
for (var gKey2 in data[wkey2]) {
if (data[wkey2].hasOwnProperty(gKey2)) {
switch (gKey2) {
case 'db_url':
text += '\n DbUrl = ' + data[wkey2][gKey2];
break;
case 'cache_url':
text += '\n # cache is used when we want to:\n # - limit the memory consumed by workers\n # - reuse common data across multiple worker runs\n # DO NOT TOUCH - unless you are a developer/devops';
text += '\n cacheUrl = ' + data[wkey2][gKey2];
break;
case 'WORKERS_VERSION':
text += '\n # Important to determine which set of workers are being called\n # to route the operations properly. This is for the time-being\n # until all workers are transitioned to v2';
text += '\n WORKERS_VERSION = ' + data[wkey2][gKey2];
break;
}
}
}
break;
}
}
}
if (data.end.completion) {
text += getSqsVariables();
writeToFile('worker2.env', text);
console.log('------------------------------------');
console.log('Congratulations ! you have successfully generated environment configuration for Worker2.');
console.log('------------------------------------');
}
break;
default:
break;
}
}
switch (environment) {
case 'web':
inquirer.prompt(questions.generalQuestions).then(answers => {
obj.general = answers;
}).then(() => {
inquirer.prompt(questions.awsqs).then(awsvals => {
obj.aws = awsvals;
}).then(() => {
inquirer.prompt(questions.vendqs).then(vendvals => {
obj.vend = vendvals;
}).then(() => {
inquirer.prompt(questions.oauthqs).then(oavals => {
obj.oauth = oavals;
}).then(() => {
inquirer.prompt(questions.workersqs).then(workers => {
obj.workers = workers;
}).then(() => {
inquirer.prompt(questions.endqs).then(endval => {
obj.end = endval;
generateEnv(obj);
});
});
});
});
});
});
break;
case 'terraform':
inquirer.prompt(questions.terraformqs).then(answers => {
obj.answers = answers;
}).then(() => {
inquirer.prompt(questions.endqs).then(endval => {
obj.end = endval;
generateEnv(obj);
});
});
break;
case 'worker':
inquirer.prompt(questions.workerqs).then(answers => {
obj.answers = answers;
//obj.worker=answers;
}).then(() => {
inquirer.prompt(questions.endqs).then(endval => {
obj.end = endval;
generateEnv(obj);
});
});
break;
case 'worker2':
inquirer.prompt(questions.worker2qs).then(answers => {
obj.answers = answers;
}).then(() => {
inquirer.prompt(questions.endqs).then(endval => {
obj.end = endval;
generateEnv(obj);
});
});
break;
default:
console.log('------------------------------------');
console.log('Oops ! An Invalid Option Entered, Please choose correct option among : ');
console.log(' 1.terraform \n 2.web \n 3.worker \n 4.worker2');
console.log('------------------------------------');
break;
}