-
Notifications
You must be signed in to change notification settings - Fork 27
/
manage.js
130 lines (113 loc) · 3.4 KB
/
manage.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
require('dotenv-flow').config()
const { resolve } = require('node:path')
const process = require('node:process')
const fse = require('fs-extra')
const range = require('lodash.range')
// DO not upgrade execa to v6.0.0 as it is esm module
const { command: execaCommand } = require('execa')
const fetch = require('node-fetch')
const config = require('./config')
// download and initialize the judgers
// start !
const logDir = resolve(__dirname, 'logs')
const baseConfig = {
apps: [
{
name: 'app',
script: resolve(__dirname, 'app.js'),
out_file: resolve(logDir, 'app.out.log'),
error_file: resolve(logDir, 'app.err.log'),
log_date_format: 'YYYY-MM-DD HH:mm:ss X',
merge_logs: true,
restart_delay: 500,
env: {
NODE_ENV: 'production',
},
},
{
name: 'updater',
script: resolve(__dirname, 'services', 'updater.js'),
out_file: resolve(logDir, 'updater.out.log'),
error_file: resolve(logDir, 'updater.err.log'),
log_date_format: 'YYYY-MM-DD HH:mm:ss X',
merge_logs: true,
env: {
NODE_ENV: 'production',
},
},
],
}
if (config.mail && config.mail.enable) {
baseConfig.apps.push({
name: 'mailer',
script: resolve(__dirname, 'services', 'mailer.js'),
out_file: resolve(logDir, 'mailer.out.log'),
error_file: resolve(logDir, 'mailer.err.log'),
log_date_format: 'YYYY-MM-DD HH:mm:ss X',
merge_logs: true,
env: {
NODE_ENV: 'production',
},
})
}
async function judgeSetup () {
let judgers = +config.judgers
if (!(judgers >= 1 && judgers <= 10)) { judgers = 1 }
const judgersDir = resolve(__dirname, 'services')
await fse.emptyDir(resolve(judgersDir, 'Judger'))
// 下载最新版的 judger
await execaCommand(`git clone https://github.com/acm309/Judger ${resolve(judgersDir, 'Judger')}`)
await execaCommand(`make -C ${resolve(judgersDir, 'Judger')}`)
await Promise.all([
fse.copy(resolve(judgersDir, 'Judger', 'Judge'), resolve(judgersDir, 'node-0', 'Judge')),
fse.copy(resolve(judgersDir, 'Judger', 'config.ini'), resolve(judgersDir, 'node-0', 'config.ini')),
])
await Promise.all(
range(1, judgers)
.map(i => fse.copy(
resolve(judgersDir, 'node-0'),
resolve(judgersDir, `node-${i}`),
)),
)
const pm2config = baseConfig
range(judgers).forEach(i =>
pm2config.apps.push({
name: `node-${i}`,
script: resolve(judgersDir, `node-${i}/judger.js`),
out_file: resolve(logDir, `node-${i}-out.log`),
error_file: resolve(logDir, `node-${i}-err.log`),
log_date_format: 'YYYY-MM-DD HH:mm:ss X',
merge_logs: true,
cwd: resolve(judgersDir, `node-${i}`),
env: {
NODE_ENV: 'production',
},
}),
)
return fse.outputJSON('pm2.config.json', pm2config, { spaces: 2, EOL: '\n' })
}
async function staticFilesSetUp () {
const res = await fetch('https://api.github.com/repos/acm309/PutongOJ-FE/releases')
const json = await res.json()
const url = json[0].assets[0].browser_download_url
await execaCommand(`wget ${url} -O dist.zip`)
await execaCommand('unzip -o dist.zip -d dist')
await execaCommand('cp -r dist/* public/', {
shell: true,
})
}
function main () {
return Promise.all([
judgeSetup(),
staticFilesSetUp(),
])
}
main()
.then(() => {
console.log('ok')
process.exit(0)
})
.catch((err) => {
console.error(err)
process.exit(-1)
})