-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
534 lines (462 loc) · 13.2 KB
/
utils.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
"use strict";
const chalk = require("chalk");
const fs = require("fs");
const cliProgress = require("cli-progress");
const fse = require("fs-extra");
const { resolve } = require("path");
const exec = require("child_process").exec;
const colors = require("colors");
const { execSync } = require("child_process");
const fetch = require("node-fetch");
const { pipeline } = require("node:stream/promises");
const filePath = pathName => {
if (pathName) {
return resolve(process.cwd(), pathName);
}
return resolve(process.cwd());
};
async function downloadFile(file_url, targetPath) {
// create a new progress bar instance and use shades_classic theme
const bar = new cliProgress.SingleBar(
{},
{
format:
colors.green(" {bar}") +
" {percentage}% | ETA: {eta}s | {value}/{total} | Speed: {speed} kbit",
barCompleteChar: "\u2588",
barIncompleteChar: "\u2591"
}
);
bar.start();
// Save variable to know progress
var received_bytes = 0;
var total_bytes = 0;
var req = await fetch(file_url, { method: "GET" });
await pipeline(req.body, fs.createWriteStream(targetPath));
bar.update(100);
bar.stop();
return "File succesfully downloaded";
}
const execCommand = (command, ignoreError) => {
return new Promise((resolve, reject) => {
exec(command, { maxBuffer: 1024 * 1000 }, (error, stdout, stderr) => {
if (error !== null) {
if (ignoreError) {
return resolve("done");
}
return reject(error);
}
console.log(stdout);
console.log(stderr);
return resolve("done");
});
});
};
const execCurl = (url, output) => {
return execCommand(`curl -L ${url} --output ${output}`);
};
const log = (msg, color = "green") => {
console.log(chalk[color](msg));
};
const sleep = ms => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
module.exports.execCommand = execCommand;
module.exports.log = log;
module.exports.sleep = sleep;
module.exports.filePath = filePath;
module.exports.downloadLatesVersion = async configs => {
log("Downloading erxes ...");
const { DOMAIN } = configs || {};
// download the latest build
await execCurl(
"https://api.github.com/repos/erxes/erxes/releases/latest",
"gitInfo.json"
);
const gitInfo = await fse.readJSON(filePath("gitInfo.json"));
let fileName = `${gitInfo.tag_name}`;
if (DOMAIN.includes("localhost")) {
fileName = `${gitInfo.tag_name}-local`;
}
await downloadFile(
`https://github.com/erxes/erxes/releases/download/${gitInfo.tag_name}/erxes-${fileName}.tar.gz`,
"build.tar.gz"
);
process.chdir(filePath());
log("Extracting tar ...");
await execCommand(`tar xf build.tar.gz`);
if (DOMAIN.includes("localhost")) {
await execCommand(`mv build-local build`);
}
log("Backing up build tar ...");
await fse.copy(filePath("build.tar.gz"), filePath("build-backup.tar.gz"));
};
module.exports.startServices = async configs => {
log("Starting services using pm2 ...");
const {
JWT_TOKEN_SECRET,
DOMAIN,
MONGO_URL = "",
ELASTICSEARCH_URL,
ELK_SYNCER,
USE_DASHBOARD,
RABBITMQ_HOST,
REDIS_HOST,
REDIS_PORT,
REDIS_PASSWORD
} = configs || {};
const optionalDbConfigs = {};
if (RABBITMQ_HOST) {
optionalDbConfigs.RABBITMQ_HOST = RABBITMQ_HOST;
}
if (REDIS_HOST) {
optionalDbConfigs.REDIS_HOST = REDIS_HOST;
optionalDbConfigs.REDIS_PORT = REDIS_PORT;
optionalDbConfigs.REDIS_PASSWORD = REDIS_PASSWORD;
}
const generateMongoUrl = dbName => {
if (MONGO_URL.includes("replicaSet")) {
return MONGO_URL.replace("erxes?", `${dbName}?`);
}
return `${MONGO_URL}/${dbName}`;
};
PORT_UI = (configs.UI || {}).PORT || 3000;
PORT_WIDGETS = (configs.WIDGETS || {}).PORT || 3200;
PORT_API = (configs.API || {}).PORT || 3300;
PORT_INTEGRATIONS = (configs.INTEGRATIONS || {}).PORT || 3400;
PORT_DASHBOARD_UI = (configs.DASHBOARD || {}).PORT_UI || 4200;
PORT_DASHBOARD_API = (configs.DASHBOARD || {}).PORT_API || 4300;
let API_DOMAIN = `http://localhost:${PORT_API}`;
let INTEGRATIONS_API_DOMAIN = `http://localhost:${PORT_INTEGRATIONS}`;
let WIDGETS_DOMAIN = `http://localhost:${PORT_WIDGETS}`;
let DASHBOARD_UI_DOMAIN = `http://localhost:${PORT_DASHBOARD_UI}`;
let DASHBOARD_API_DOMAIN = `http://localhost:${PORT_DASHBOARD_API}`;
const dasbhoardSchemaPath = "build/dashboard-api/schema";
const HELPERS_DOMAIN = `https://helper.erxes.io/`;
if (!DOMAIN.includes("localhost")) {
API_DOMAIN = `${DOMAIN}/api`;
INTEGRATIONS_API_DOMAIN = `${DOMAIN}/integrations`;
WIDGETS_DOMAIN = `${DOMAIN}/widgets`;
DASHBOARD_UI_DOMAIN = `${DOMAIN}/dashboard/front`;
DASHBOARD_API_DOMAIN = `${DOMAIN}/dashboard/api`;
}
const API_MONGO_URL = generateMongoUrl("erxes");
const commonEnv = {
NODE_ENV: "production",
JWT_TOKEN_SECRET: JWT_TOKEN_SECRET || "",
MONGO_URL: API_MONGO_URL,
ELASTICSEARCH_URL,
ELK_SYNCER,
MAIN_APP_DOMAIN: DOMAIN,
WIDGETS_DOMAIN: WIDGETS_DOMAIN,
INTEGRATIONS_API_DOMAIN: INTEGRATIONS_API_DOMAIN,
LOGS_API_DOMAIN: configs.LOGS_API_DOMAIN || "http://localhost:3800",
ENGAGES_API_DOMAIN: configs.ENGAGES_API_DOMAIN || "http://localhost:3900",
VERIFIER_API_DOMAIN: configs.VERIFIER_API_DOMAIN || "http://localhost:4100",
...(configs.API || {})
};
const apps = [
{
name: "api",
script: filePath("build/api"),
env: {
PORT: PORT_API,
DASHBOARD_DOMAIN: USE_DASHBOARD ? DASHBOARD_UI_DOMAIN : null,
HELPERS_DOMAIN: USE_DASHBOARD ? HELPERS_DOMAIN : null,
...commonEnv,
...optionalDbConfigs,
DEBUG: "erxes-api:*"
}
},
{
name: "cronjobs",
script: filePath("build/api/cronJobs"),
env: {
PORT_CRONS: 3600,
...commonEnv,
PROCESS_NAME: "crons",
...optionalDbConfigs,
DEBUG: "erxes-crons:*"
}
},
{
name: "workers",
script: filePath("build/api/workers"),
env: {
PORT_WORKERS: 3700,
...commonEnv,
...optionalDbConfigs,
DEBUG: "erxes-workers:*"
}
},
{
name: "integrations",
script: filePath("build/integrations"),
env: {
PORT: PORT_INTEGRATIONS,
NODE_ENV: "production",
DEBUG: "erxes-integrations:*",
DOMAIN: INTEGRATIONS_API_DOMAIN,
MAIN_APP_DOMAIN: DOMAIN,
MAIN_API_DOMAIN: API_DOMAIN,
MONGO_URL: generateMongoUrl("erxes_integrations"),
...optionalDbConfigs,
...(configs.INTEGRATIONS || {})
}
},
{
name: "engages",
script: filePath("build/engages"),
env: {
PORT: 3900,
NODE_ENV: "production",
DEBUG: "erxes-engages:*",
MAIN_API_DOMAIN: API_DOMAIN,
MONGO_URL: generateMongoUrl("erxes_engages"),
...optionalDbConfigs,
...(configs.ENGAGES || {})
}
},
{
name: "logger",
script: filePath("build/logger"),
env: {
PORT: 3800,
NODE_ENV: "production",
DEBUG: "erxes-logs:*",
MONGO_URL: generateMongoUrl("erxes_logger"),
...optionalDbConfigs,
...(configs.LOGGER || {})
}
},
{
name: "email-verifier",
script: filePath("build/email-verifier"),
env: {
PORT: 4100,
NODE_ENV: "production",
DEBUG: "erxes-email-verifier:*",
MONGO_URL: generateMongoUrl("erxes_email_verifier"),
...(configs.EMAIL_VERIFIER || {})
}
}
];
if (USE_DASHBOARD) {
log("Starting dashboard ...");
if (!ELK_SYNCER) {
return log(
'Dashboard is not started "If you want to use dashboard you need to start elksyncer"',
"red"
);
}
if (!REDIS_HOST || !REDIS_PORT) {
return log(
'Dashboard is not started "If you want to use dashboard you need to start redis"',
"red"
);
}
const CUBE_API_SECRET = Math.random().toString();
apps.push({
name: "dashboard-api",
script: filePath("build/dashboard-api"),
env: {
NODE_ENV: "production",
PORT: PORT_DASHBOARD_API,
DEBUG: "erxes-dashboards:*",
DB_NAME: "erxes",
CUBEJS_URL: DASHBOARD_API_DOMAIN,
CUBEJS_API_SECRET: CUBE_API_SECRET,
CUBEJS_DB_TYPE: "elasticsearch",
CUBEJS_DB_URL: ELASTICSEARCH_URL,
SCHEMA_PATH: dasbhoardSchemaPath,
REDIS_URL: `redis://${REDIS_HOST}:${REDIS_PORT || 6379}?password=${
REDIS_PASSWORD || ""
}`,
REDIS_PASSWORD: REDIS_PASSWORD
}
});
const subscriptionsUrl = `${
API_DOMAIN.includes("https") ? "wss" : "ws"
}//${API_DOMAIN}/subscriptions`;
await fs.promises.writeFile(
filePath("build/dashboard-ui/js/env.js"),
`
window.env = {
NODE_ENV: "production",
REACT_APP_API_URL: "${API_DOMAIN}",
REACT_APP_DASHBOARD_API_URL: "${DASHBOARD_API_DOMAIN}",
REACT_APP_API_SUBSCRIPTION_URL: "${subscriptionsUrl}"
}
`
);
apps.push({
name: "dashboard-ui",
script: "serve",
env: {
PM2_SERVE_PATH: filePath("build/dashboard-ui"),
PM2_SERVE_PORT: PORT_DASHBOARD_UI,
PM2_SERVE_SPA: "true"
}
});
}
if (ELK_SYNCER) {
log("Starting elkSyncer ...");
apps.push({
name: "elkSyncer",
cwd: filePath("build/elkSyncer"),
script: "main.py",
interpreter: "/usr/bin/python3",
env: {
MONGO_URL: API_MONGO_URL,
ELASTICSEARCH_URL
}
});
}
const uiConfigs = configs.UI || {};
const subscriptionsUrl = `${API_DOMAIN.replace("https", "wss").replace(
"http",
"ws"
)}/subscriptions`;
if (uiConfigs.disableServe) {
log(
"Default serve is disabled. Please serve using services like nginx, aws s3 ...",
"yellow"
);
} else {
await fs.promises.writeFile(
filePath("build/ui/js/env.js"),
`
window.env = {
NODE_ENV: "production",
REACT_APP_API_URL: "${API_DOMAIN}",
REACT_APP_API_SUBSCRIPTION_URL: "${subscriptionsUrl}",
REACT_APP_CDN_HOST: "${WIDGETS_DOMAIN}",
${
USE_DASHBOARD
? `REACT_APP_DASHBOARD_URL: "${DASHBOARD_UI_DOMAIN}"`
: ""
}
}
`
);
apps.push({
name: "ui",
script: "serve",
env: {
PM2_SERVE_PATH: filePath("build/ui"),
PM2_SERVE_PORT: PORT_UI,
PM2_SERVE_SPA: "true"
}
});
}
apps.push({
name: "widgets",
script: filePath("build/widgets/dist"),
env: {
PORT: PORT_WIDGETS,
NODE_ENV: "production",
ROOT_URL: WIDGETS_DOMAIN,
API_URL: API_DOMAIN,
API_SUBSCRIPTIONS_URL: subscriptionsUrl,
...(configs.WIDGETS || {})
}
});
// create ecosystem
await fse.writeFile(
filePath("ecosystem.config.js"),
`
module.exports = {
apps: ${JSON.stringify(apps)}
}
`
);
// generate nginx config
generateNginxConf({
DOMAIN,
PORT_UI,
PORT_WIDGETS,
PORT_API,
PORT_INTEGRATIONS,
USE_DASHBOARD,
PORT_DASHBOARD_API,
PORT_DASHBOARD_UI
});
log("Running migrations ...");
await execCommand(
`MONGO_URL="${API_MONGO_URL}" node ${filePath(
"build/api/commands/migrate.js"
)}`
);
const ecosystemPath = filePath("ecosystem.config.js");
return execSync(`pm2 start ${ecosystemPath}`);
};
const generateNginxConf = async ({
DOMAIN,
PORT_UI,
PORT_WIDGETS,
PORT_API,
PORT_INTEGRATIONS,
USE_DASHBOARD,
PORT_DASHBOARD_API,
PORT_DASHBOARD_UI
}) => {
const commonConfig = `
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
`;
let dashboardConfig = ``;
if (USE_DASHBOARD) {
dashboardConfig = `
location /dashboard/front {
proxy_pass http://127.0.0.1:${PORT_DASHBOARD_UI}/;
${commonConfig}
}
location /dashboard/api/ {
proxy_pass http://127.0.0.1:${PORT_DASHBOARD_API}/;
${commonConfig}
}
`;
}
await fs.promises.writeFile(
filePath("nginx.conf"),
`
server {
listen 80;
server_name ${DOMAIN.replace("https://", "").replace(
"http://",
""
)};
# erxes build path
index index.html;
error_log /var/log/nginx/erxes.error.log;
access_log /var/log/nginx/erxes.access.log;
location / {
proxy_pass http://127.0.0.1:${PORT_UI}/;
${commonConfig}
}
location /widgets/ {
proxy_pass http://127.0.0.1:${PORT_WIDGETS}/;
${commonConfig}
}
location /api/ {
proxy_pass http://127.0.0.1:${PORT_API}/;
${commonConfig}
}
location /integrations/ {
proxy_pass http://127.0.0.1:${PORT_INTEGRATIONS}/;
${commonConfig}
}
${dashboardConfig}
}
`
);
};
module.exports.downloadFile = downloadFile;
module.exports.execCurl = execCurl;