-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfigure.js
executable file
·258 lines (242 loc) · 8 KB
/
configure.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
const fs = require('fs');
const prompt = require('prompt');
const processes = require('./processes.json');
const dataStringify = JSON.stringify(processes);
const dataParsed = JSON.parse(dataStringify);
const {exec, execSync, execFileSync} = require('child_process');
prompt.colors = false;
process.stdin.resume(); //so the program will not close instantly
function exitHandler(options, err) {
try {
if (err) {
process.kill(process.pid);
process.exit();
}
if (options.exit) {
process.exit();
} else if (options.pid) {
process.kill(process.pid);
}
} catch (err) {
console.log(err);
}
}
//catches ctrl+c and stop.sh events
process.on('SIGINT', exitHandler.bind(null, {exit: true}));
//catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, {pid: true}));
process.on('SIGUSR2', exitHandler.bind(null, {pid: true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {
exit: true,
reason: 'uncaughtException'
}));
main();
async function main() {
if (processConfigured()) {
try {
var cp = execSync('cp processes.json processes_backup.json');
if (dataParsed.apps[0].env.tokens.WICKRIO_BOT_NAME.value !== undefined) {
var newName = "WickrIO-Monitor-Bot_" + dataParsed.apps[0].env.tokens.WICKRIO_BOT_NAME.value;
} else {
var newName = "WickrIO-Monitor-Bot";
}
//var assign = Object.assign(dataParsed.apps[0].name, newName);
dataParsed.apps[0].name = newName;
var ps = fs.writeFileSync('./processes.json', JSON.stringify(dataParsed, null, 2));
} catch (err) {
console.log(err);
}
console.log("Already configured");
process.exit();
} else {
try {
var it = await inputTokens();
process.exit();
} catch (err) {
console.log(err);
}
}
}
async function inputTokens() {
var tokens = ['WICKRIO_BOT_NAME','PAGERDUTY_API_KEY', 'NEIGHBOR_BOTS_LIST']; //Add any tokens(as strings separated by commas) you want to prompt for in the configuration process here
var config = [];
var i = 0;
var inputResult = await readFileInput();
let objectKeyArray = [];
let objectValueArray = [];
if (inputResult !== "" || inputResult !== undefined) {
for (var x = 0; x < inputResult.length; x++) {
let locationEqual = inputResult[x].indexOf("=");
let objectKey = inputResult[x].slice(0, locationEqual);
let objectValue = inputResult[x].slice(locationEqual + 1, inputResult[x].length); //Input value
objectKeyArray.push(objectKey);
objectValueArray.push(objectValue);
}
var newObjectResult = {};
for (var j = 0; j < inputResult.length; j++) {
newObjectResult[objectKeyArray[j]] = objectValueArray[j];
}
}
return new Promise((resolve, reject) => {
var recursivePrompt = function() {
var token = tokens[i];
var type;
if (i === tokens.length) {
return resolve("Configuration complete!");
}
if (token === 'WICKRIO_BOT_NAME' && process.env.WICKRIO_BOT_NAME !== undefined){
var input = token + '=' + process.env.WICKRIO_BOT_NAME;
config.push(input);
i++;
return recursivePrompt();
} else if (token === 'PAGERDUTY_API_KEY' && process.env.PAGERDUTY_API_KEY !== undefined){
var input = token + '=' + process.env.PAGERDUTY_API_KEY;
config.push(input);
i++;
return recursivePrompt();
} else if(token === 'NEIGHBOR_BOTS_LIST' && process.env.NEIGHBOR_BOTS_LIST !== undefined){
var input = token + '=' + process.env.NEIGHBOR_BOTS_LIST;
config.push(input);
i++;
return recursivePrompt();
}
var dflt = newObjectResult[token];
var emptyChoice = false;
if (dflt === "undefined" || dflt === undefined) {
dflt = "N/A";
emptyChoice = true;
}
var schema = {
properties: {
[token]: {
pattern: type,
type: 'string',
description: 'Enter your ' + token.replace(/_/g, " ").replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}) + ' (Default: ' + dflt + ')',
message: 'Cannot leave ' + token + ' empty! Please enter a value',
required: emptyChoice
}
}
};
prompt.get(schema, function(err, answer) {
if (answer[token] === "")
answer[token] = newObjectResult[token];
var input = token + '=' + answer[token];
config.push(input);
i++;
recursivePrompt();
});
}
recursivePrompt();
}).then(function(answer) {
let objectKeyArray = [];
let objectValueArray = [];
for (var i = 0; i < config.length; i++) {
let locationEqual = config[i].indexOf("=");
let objectKey = config[i].slice(0, locationEqual);
let objectValue = config[i].slice(locationEqual + 1, config[i].length); //Input value
objectKeyArray.push(objectKey);
objectValueArray.push(objectValue);
}
var newObjectResult = {};
for (var j = 0; j < config.length; j++) {
newObjectResult[objectKeyArray[j]] = objectValueArray[j];
}
for (var key in newObjectResult) {
if (key === 'WICKRIO_BOT_NAME' && process.env.WICKRIO_BOT_NAME !== undefined){
var obj = {
"value": process.env.WICKRIO_BOT_NAME,
"encrypted": false
};
newObjectResult.WICKRIO_BOT_NAME = obj;
continue;
} else if (key === 'PAGERDUTY_API_KEY' && process.env.PAGERDUTY_API_KEY !== undefined){
var obj = {
"value": process.env.PAGERDUTY_API_KEY,
"encrypted": false
};
newObjectResult.PAGERDUTY_API_KEY = obj;
continue;
} else if(key === 'NEIGHBOR_BOTS_LIST' && process.env.NEIGHBOR_BOTS_LIST !== undefined){
var obj = {
"value": process.env.NEIGHBOR_BOTS_LIST,
"encrypted": false
};
newObjectResult.NEIGHBOR_BOTS_LIST = obj;
continue;
}
var obj = {
"value": newObjectResult[key],
"encrypted": false
};
newObjectResult[key] = obj;
}
for (var key in dataParsed.apps[0].env.tokens) {
delete dataParsed.apps[0].env.tokens[key];
}
try {
var cp = execSync('cp processes.json processes_backup.json');
if (process.env.WICKRIO_BOT_NAME !== undefined) {
var newName = "WickrIO-Monitor-Bot_" + process.env.WICKRIO_BOT_NAME;
} else if (newObjectResult.WICKRIO_BOT_NAME.value !== undefined) {
var newName = "WickrIO-Monitor-Bot_" + newObjectResult.WICKRIO_BOT_NAME.value;
} else {
var newName = "WickrIO-Monitor-Bot";
}
dataParsed.apps[0].name = newName;
var assign = Object.assign(dataParsed.apps[0].env.tokens, newObjectResult);
var ps = fs.writeFileSync('./processes.json', JSON.stringify(dataParsed, null, 2));
} catch (err) {
console.log(err);
}
console.log(answer);
return;
}).catch(err => {
console.log(err);
});
}
function readFileInput() {
try {
var rfs = fs.readFileSync('./processes.json', 'utf-8');
if (!rfs) {
console.log("Error reading processes.json!")
return rfs;
} else
return rfs.trim().split('\n');
}
catch (err) {
console.log(err);
process.exit();
}
}
function processConfigured()
{
var processes;
try {
processes = fs.readFileSync('./processes.json', 'utf-8');
if (!processes) {
console.log("Error reading processes.json!")
return false;
}
}
catch (err) {
console.log(err);
return false;
}
var pjson = JSON.parse(processes);
if (pjson.apps[0].env.tokens === undefined) {
return false;
}
if (pjson.apps[0].env.tokens.WICKRIO_BOT_NAME === undefined) {
return false;
}
if (pjson.apps[0].env.tokens.PAGERDUTY_API_KEY === undefined) {
return false;
}
if (pjson.apps[0].env.tokens.NEIGHBOR_BOTS_LIST === undefined) {
return false;
}
return true;
}