forked from OriginTrail/ot-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (71 loc) · 3.34 KB
/
index.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
require('dotenv').config();
const fs = require('fs-extra');
const path = require('path');
const appRootPath = require('app-root-path');
const { exec, execSync } = require('child_process');
const rc = require('rc');
const OTNode = require('./ot-node');
const pjson = require('./package.json');
const configjson = require('./config/config.json');
process.env.NODE_ENV = process.env.NODE_ENV && ['development', 'testnet', 'mainnet'].indexOf(process.env.NODE_ENV) >= 0
? process.env.NODE_ENV : 'development';
let config = JSON.parse(fs.readFileSync('./.origintrail_noderc', 'utf8'));
const defaultConfig = JSON.parse(JSON.stringify(configjson[process.env.NODE_ENV]));
config = rc(pjson.name, defaultConfig);
(async () => {
let userConfig = null;
try {
if (process.env.NODE_ENV === 'development' && process.argv.length === 3) {
const configurationFilename = process.argv[2];
userConfig = JSON.parse(fs.readFileSync(process.argv[2]));
userConfig.configFilename = configurationFilename;
}
} catch (error) {
console.log('Unable to read user configuration from file: ', process.argv[2]);
process.exit(1);
}
try {
const node = new OTNode(userConfig);
await node.start();
} catch (e) {
console.error(`Error occurred while starting new version, error message: ${e}. ${e.stack}`);
if (!config.autoUpdate.enabled) {
console.log('Auto update is disabled. Shutting down the node...');
process.exit(1);
}
const backupCodeDirectory = path.join(config.autoUpdate.backupDirectory, 'auto-update', 'backup');
if (fs.ensureDir(backupCodeDirectory)) {
console.log('Starting back old version of OT-Node.');
const destination = appRootPath.path;
await fs.ensureDir(destination);
await fs.copy(backupCodeDirectory, destination);
await new Promise((resolve, reject) => {
const command = `cd ${destination} && npm install --omit=dev`;
const child = exec(command);
// Wait for results
child.stdout.on('end', resolve);
child.stdout.on('data', (data) => console.log(`AutoUpdater - npm install --omit=dev: ${data.replace(/\r?\n|\r/g, '')}`));
child.stderr.on('data', (data) => {
if (data.toLowerCase().includes('error')) {
// npm passes warnings as errors, only reject if "error" is included
data = data.replace(/\r?\n|\r/g, '');
console.error('AutoUpdater - Error installing dependencies');
console.error(`AutoUpdater - ${data}`);
reject();
} else {
console.log(`AutoUpdater - ${data}`);
}
});
});
execSync(`cd ${destination} && npx sequelize --config=./config/sequelizeConfig.js db:migrate`, { stdio: 'inherit' });
process.exit(1);
} else {
console.error(`Failed to start OT-Node, no backup code available. Error message: ${e.message}`);
process.exit(1);
}
}
})();
process.on('uncaughtException', (err) => {
console.error('Something went really wrong! OT-node shutting down...', err);
process.exit(1);
});