-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (91 loc) · 3.15 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const execSync = require('child_process').execSync;
const sanitize = require('sanitize-filename');
const fs = require('fs');
const util = require('util');
// Environment variables & other things
const vars = {
scriptMode: process.env.DW_SCRIPT_MODE,
appLogfile: process.env.DW_APP_LOGFILE || 'server.log',
deployingFilename: process.env.DW_DEPLOY_FILENAME,
prodFilename: process.env.DW_PROD_FILENAME,
scriptLogfile: process.env.DW_SCRIPT_LOGFILE,
cwd: process.env.DW_CWD,
dbUser: process.env.DW_DB_USER,
dbHost: process.env.DW_DB_HOST,
dbOid: process.env.DW_DB_OID,
pKillProcessText: process.env.DW_PKILL_TEXT,
currentDate: sanitize(new Date().toISOString(), { replacement: '-' }),
};
if (vars.scriptMode === 'backup') {
vars.backupDirname = `backups/${vars.currentDate}-from-backup`;
} else {
vars.backupDirname = `backups/${vars.currentDate}-from-deploy-backup`;
}
// Set up logging and working directory
process.chdir(vars.cwd);
const logFile = fs.createWriteStream(`${vars.scriptLogfile}`, { flags: 'a' });
const logStdout = process.stdout;
console.log = function (d) { //
logFile.write(`${util.format(d)}\n`);
logStdout.write(`${util.format(d)}\n`);
};
function execSyncEx(command) {
const result = execSync(command, [], { stdio: 'inherit' });
console.log(result.toString('utf8').replace(/\n$/, ''));
}
function start() {
if (!vars.prodFilename || !vars.appLogfile) { throw new Error('Variables missing. Exiting.'); }
execSyncEx(`nohup java -jar ${vars.prodFilename} --spring.profiles.active='postgres,enable-resource-caching' &> ${vars.appLogfile}&`, [], { stdio: 'inherit' });
}
function stop() {
if (!vars.pKillProcessText) { throw new Error('Variables missing. Exiting.'); }
try {
execSync(`killall -w -u ec2-user -r "${vars.pKillProcessText}"`, [], { stdio: 'inherit' });
} catch (err) {
// Ignore errors here, probably just that the process was not running
return '';
}
return '';
}
function backup() {
if (!vars.appLogfile || !vars.prodFilename || !vars.dbHost || !vars.dbUser || !vars.dbOid) { throw new Error('Variables missing. Exiting.'); }
execSyncEx(`mkdir -pv ${vars.backupDirname}`);
execSyncEx(`touch ${vars.appLogfile} && cp -vf ${vars.appLogfile} ${vars.backupDirname}/`); // fail silently
execSyncEx(`cp -vf ${vars.prodFilename} ${vars.backupDirname}/`);
execSyncEx(`pg_dump -U ${vars.dbUser} -h ${vars.dbHost} -o ${vars.dbOid} > ${vars.backupDirname}/db_backup_${vars.currentDate}.sql`);
execSyncEx(`wc -c ${vars.backupDirname}/db_backup_${vars.currentDate}.sql`);
}
function deploy() {
// Because deployments are important
for (const currentValue in vars) {
if (!currentValue) {
throw new Error('One or more environment variables not set. Exiting!');
}
}
backup();
// deploying
execSyncEx(`mv -vf ${vars.deployingFilename} ${vars.prodFilename}`);
stop();
start();
}
switch (vars.scriptMode) {
case 'deploy':
deploy();
break;
case 'backup':
backup();
break;
case 'stop':
stop();
break;
case 'start':
start();
break;
case 'restart':
stop();
start();
break;
default:
console.log('No mode specified, doing nothing');
break;
}