-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
221 lines (198 loc) · 6.09 KB
/
app.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
import express from 'express'
import fs from 'fs'
import config from './config.js'
import Runner from './src/runner.js'
import Branches from './src/branches.js'
import TestMerges from './src/testmerges.js'
import Metrics from './src/metrics.js'
import { log, serverConfig } from './src/utils.js'
const hostname = '0.0.0.0'
const port = 3000
const app = express()
app.use(express.json())
const RunnerInstance = new Runner
app.use((req, res, next) => {
if (req.header('Api-Key') === config.apiKey) next()
else res.status(401).send('No.')
})
app.get('/status', (req, res) => {
res.json({
maxCompileJobs: RunnerInstance.maxJobs,
currentCompileJobs: RunnerInstance.currentJobs.map(e => e.serverId),
queuedJobs: RunnerInstance.queuedJobs.map(e => e.serverId)
}).end()
})
app.get('/branch/:server', (req, res) => {
const server = req.params.server
if (!server) return res.status(400).json({error: 'Missing server ID'})
try {
res.status(200).json({ branch: Branches.getBranch(server) })
} catch(e) {
res.status(500).json({error: e.message})
}
})
app.post('/switch-branch', (req, res) => {
const server = req.body.server
const branch = req.body.branch
if (!server) return res.status(400).json({error: 'Missing server ID'})
if (!branch) return res.status(400).json({error: 'Missing branch'})
if (RunnerInstance.currentJobs.find((job) => job.serverId === server)) {
res.status(400).json({error: 'Unable to switch the branch of a server that is currently building'})
return
}
try {
Branches.switchBranch(server, branch)
res.status(200).json({success: true})
} catch(e) {
res.status(500).json({error: e.message})
}
})
app.post('/build', (req, res) => {
const server = req.body.server
if (!server) return res.status(400).json({error: 'Missing server ID'})
try {
res.status(200).json({success: true}).end()
log(`Manual build for ${server} triggered`)
RunnerInstance.build(server)
} catch(e) {
log(e)
// res.status(500).json({error: e.message})
}
})
app.post('/cancel', (req, res) => {
const server = req.body.server
if (!server) return res.status(400).json({error: 'Missing server ID'})
try {
log(`Manual cancel for ${server} triggered`)
const Build = RunnerInstance.getBuildByServerId(server)
if (Build) {
res.status(200).json({success: true}).end()
RunnerInstance.removeFromQueue(server)
Build.cancel()
} else {
res.status(404).json({success: false}).end()
}
} catch(e) {
log(e)
// res.status(500).json({error: e.message})
}
})
app.post('/switch-map', (req, res) => {
const server = req.body.server
const map = req.body.map
if (!server) return res.status(400).json({error: 'Missing server ID'})
if (!map) return res.status(400).json({error: 'Missing map'})
try {
res.status(200).json({success: true}).end()
RunnerInstance.build(server, { switchToMap: map, skipCdn: true })
} catch(e) {
log(e)
// res.status(500).json({error: e.message})
}
})
app.post('/restart', (req, res) => {
const server = req.body.server
if (!server) return res.status(400).json({error: 'Missing server ID'})
try {
const servers = serverConfig.servers
let valid = false
for (const cServer in servers) {
const settings = servers[cServer]
if (cServer === server && settings.active) {
valid = true
break
}
}
if (!valid) return res.status(400).json({error: 'Invalid server ID'})
fs.closeSync(fs.openSync(`/remote_ss13/restarter/triggers/${server}`, 'w'))
res.status(200).json({success: true})
} catch(e) {
res.status(500).json({error: e.message})
}
})
app.post('/toggle-tracy', (req, res) => {
const server = req.body.server
const enable = req.body.enable
if (!server) return res.status(400).json({error: 'Missing server ID'})
if (enable === undefined) return res.status(400).json({error: 'Missing enable'})
try {
const servers = serverConfig.servers
let valid = false
for (const cServer in servers) {
const settings = servers[cServer]
if (cServer === server && settings.active) {
valid = true
break
}
}
if (!valid) return res.status(400).json({error: 'Invalid server ID'})
const path = `/ss13_servers/${server}/tracyprofiling`
if (enable) {
fs.closeSync(fs.openSync(path, 'w'))
} else {
fs.unlinkSync(path)
}
res.status(200).json({success: true})
} catch(e) {
res.status(500).json({error: e.message})
}
})
app.get('/test-merges/:server?', async (req, res) => {
const server = req.params.server
try {
const allTestMerges = await TestMerges.getAll(server)
res.status(200).json(allTestMerges)
} catch (e) {
res.status(500).json({error: e.message})
}
})
app.post('/test-merges', async (req, res) => {
const pr = req.body.pr
const server = req.body.server
const requester = req.body.requester
const commit = req.body.commit
if (!pr) return res.status(400).json({error: 'Missing PR ID'})
if (!server) return res.status(400).json({error: 'Missing server ID'})
try {
await TestMerges.add(pr, server, requester, commit)
res.status(200).json({success: true})
} catch (e) {
res.status(500).json({error: e.message})
}
})
app.put('/test-merges', async (req, res) => {
const pr = req.body.pr
const server = req.body.server
const updater = req.body.updater
const commit = req.body.commit
if (!pr) return res.status(400).json({error: 'Missing PR ID'})
if (!server) return res.status(400).json({error: 'Missing server ID'})
try {
await TestMerges.update(pr, server, updater, commit)
res.status(200).json({success: true})
} catch (e) {
res.status(500).json({error: e.message})
}
})
app.delete('/test-merges', async (req, res) => {
const pr = req.body.pr
const server = req.body.server
if (!pr) return res.status(400).json({error: 'Missing PR ID'})
if (!server) return res.status(400).json({error: 'Missing server ID'})
try {
await TestMerges.remove(pr, server)
res.status(200).json({success: true})
} catch (e) {
res.status(500).json({error: e.message})
}
})
app.get('/stats', async (req, res) => {
const data = await Metrics.getBuildMetrics()
res.json(data).end()
})
app.listen(port, hostname, () => {
console.log(`Server running at https://${hostname}:${port}/`)
})
setInterval(() => {
RunnerInstance.run()
}, 2 * 1000 * 60)