-
Notifications
You must be signed in to change notification settings - Fork 12
/
newInstance.js
319 lines (302 loc) · 10.2 KB
/
newInstance.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
var config = require('./config.json');
var globalURL = config.globalURI;
var localIp = 'http://localhost';
var customer = process.env.customer||makeid(10);
var appName = process.env.app||'testApp';
var usePrefix = config.useCollectionPrefix === undefined ? true:config.useCollectionPrefix;
console.log(usePrefix);
var databaseURI = config.databaseURI+(usePrefix?'':'/'+appName);
var psPort = (Number(process.env.no)+1337) || '1337';
var fs = require('fs');
var appProfile;
var appFileFolder = "./configs/";
var appFilePath = appFileFolder + appName + ".json";
var cloudCodePath = config.cloudCodeFolder;
var instanceCount=0;
const util = require('util');
const exec = util.promisify(require('child_process').exec);
var dashboardPassword;
var dashboardAdminPassword;
var generateInstanceJson = function() {
return json = {
"customer":customer,
"useCollectionPrefix":usePrefix,
"app":appName,
"enable":true,
"serverURL":"http://localhost",
"publicServerURL":globalURL+"/"+appName,
"parseServer": {
"port":psPort,
"databaseURI":databaseURI,
"cloud":cloudCodePath+appName+"/main.js",
"appId":makeid(20),
"masterKey":makeid(20)
}
};
};
function pm2StartParseServer(appName) {
return new Promise(async function(res,rej) {
const { stdout, stderr } = await exec('pm2 start appServers.json --only '+appName);
if(stderr) {
console.log('stderr:', stderr);
rej();
} else {
console.log('stdout:', stdout);
res();
}
});
}
function pm2RestartRoute() {
return new Promise(async function(res,rej) {
const { stdout, stderr } = await exec('pm2 start initial.json');
if(stderr) {
console.log('stderr:', stderr);
rej();
} else {
console.log('stdout:', stdout);
res();
}
});
}
function makeid(num) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < num; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
var getCount = function(cb) {
//read instance count
fs.readFile('./count.json',function(err,data) {
if(err) {
data ={count:1};
} else {
data = JSON.parse(data);
++data.count;
}
cb(data.count);
});
};
var setCount = function(count) {
var data={
count:count
};
fs.writeFile('./count.json',JSON.stringify(data),function (err, data) {
if(!err) {
} else {
console.log('count.json write error!');
}
});
};
function updatePm2AppServersJson() {
return new Promise(function(res,rej) {
fs.readFile('./appServers.json',function(err,data) {
let app = {
name:appName,
script:"./app.js",
watch:[
cloudCodePath+appName
],
env:{
app:appName,
AWS_ACCESS_KEY_ID:config.S3FilesAdapter.accessKey,
AWS_SECRET_ACCESS_KEY:config.S3FilesAdapter.secretKey
}
};
if(err) {
data ={};
data.apps=[];
} else {
data = JSON.parse(data);
}
data.apps.push(app);
fs.writeFile('./appServers.json',JSON.stringify(data),function (err, data) {
if(!err) {
res();
} else {
console.log('appServers.json write error!');
}
});
});
});
}
function createCloudCodeFiles() {
//create cloud code main.js
if (!fs.existsSync(cloudCodePath)){
fs.mkdirSync(cloudCodePath);
}
if (!fs.existsSync(cloudCodePath+appName)){
fs.mkdirSync(cloudCodePath+appName);
}
fs.closeSync(fs.openSync(cloudCodePath+appName+"/main.js", 'w'));
}
function updateParseDashboardJson() {
return new Promise(function(res,rej) {
fs.readFile('./dashboards.json',function(err,data) {
let dashboard = {
apps:[],
users:[]
};
if(!err) {
dashboard = JSON.parse(data);
}
let app = {
serverURL:appProfile.publicServerURL,
appId:appProfile.parseServer.appId,
masterKey:appProfile.parseServer.masterKey,
appName:appProfile.app
};
dashboard.apps.push(app);
var found=false;
for(let i in dashboard.users) {
if(dashboard.users[i].user===customer) {
dashboardPassword = dashboard.users[i].pass;
dashboard.users[i].apps.push({
appId:appProfile.parseServer.appId
});
found=true;
}
}
if(!found) {
dashboardPassword = makeid(10);
let user = {
user:appProfile.customer,
pass:dashboardPassword,
apps:[{
appId:appProfile.parseServer.appId
}
]
};
dashboard.users.push(user);
}
found=false;
for(let i in dashboard.users) {
if(dashboard.users[i].user==='admin') {
dashboard.users[i].apps.push({
appId:appProfile.parseServer.appId
});
found=true;
}
}
if(!found) {
dashboardAdminPassword = makeid(20);
var u = {
user:"admin",
pass:dashboardAdminPassword,
apps:[
{appId:appProfile.parseServer.appId}
]
};
dashboard.users.push(u);
}
fs.writeFile('./dashboards.json',JSON.stringify(dashboard),function (err, data) {
if(!err) {
res();
} else {
console.log('dashboards.json write error!');
}
});
});
});
}
new Promise(function (resolve, reject) {
if (!fs.existsSync(appFileFolder)){
fs.mkdirSync(appFileFolder);
}
fs.stat(appFilePath, function(err, stat) {
if(err == null) {
console.log('app already exists');
reject();
} else if(err.code == 'ENOENT') {
resolve();
} else {
console.log('Some other error: ', err.code);
reject();
}
});
}).then(function(){
//get and increase port number
return new Promise(function(res,rej) {
getCount(function(count) {
if(Number(count)>config.maxInstances) {
console.log("exceeds maximum instances! please check config.json");
rej();
} else {
instanceCount=count;
if(!process.env.no) {
psPort = config.portFrom + Number(count);
}
setCount(count);
appProfile = generateInstanceJson();
fs.writeFile(appFilePath,
JSON.stringify(appProfile), function (err,data) {
if (err) {
throw new Error(err);
}
res();
});
}
});
});
}).then(function(){
//create cloud code main.js
return new Promise(function(res,rej) {
createCloudCodeFiles();
res();
});
}).then(function(){
//add pm2 script in appServers.json
return updatePm2AppServersJson();
}).then(function(){
//start server
return pm2StartParseServer(appName);
}).then(function(){
//update parse dashboard server
return updateParseDashboardJson();
}).then(function(){
//add routing to route.json and restart routing
return new Promise(function(res,rej) {
fs.readFile('./route-proxy.json',function(err,data) {
var route = {
};
if (!err) {
route = JSON.parse(data);
}
route[".*/"+appName]=localIp+":"+psPort;
fs.writeFile('./route-proxy.json',JSON.stringify(route),function (err, data) {
if(!err) {
return pm2RestartRoute().then(function(){
res();
});
} else {
console.log("route-proxy write file error!");
rej();
}
});
});
});
}).then(function(){
//show admin information if needed
if(dashboardAdminPassword) {
console.log("This is firstly start parse dashboard, admin is created.");
console.log("---------------------------------------------------------");
console.log("dashboard URL: "+config.parseDashboardURI);
console.log("username: admin");
console.log("password: "+dashboardAdminPassword);
console.log("---------------------------------------------------------");
}
console.log("No."+instanceCount+" parse server instance is created!");
console.log("The app and customer information are shown as following, you can copy & paste to your customer");
console.log("---------------------------------------------------------");
console.log("app name: " + appProfile.app);
console.log("api URL: " + appProfile.publicServerURL);
console.log("application id: " + appProfile.parseServer.appId);
console.log("master key: "+appProfile.parseServer.masterKey +' //please keep master key in secret');
console.log("dashboard URL: "+config.parseDashboardURI);
console.log("dashboard username:" + appProfile.customer);
console.log("dashboard password: "+dashboardPassword +' //please keep this password in secret');
console.log("---------------------------------------------------------");
//show new customer information
}).catch(function (err) {
return;
});