forked from codewars/codewars-runner-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
193 lines (162 loc) · 4.62 KB
/
Copy pathserver.js
File metadata and controls
193 lines (162 loc) · 4.62 KB
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
// TODO: Security still needs to be added to most endpoints
var express = require('express'),
config = require('./lib/config'),
docker = require('./lib/docker'),
exec = require('child_process').exec,
os = require('os'),
key = require('./apikey');
var app = express();
//shutdown when instructed by gracefulreload
process.on('message', function(msg) {
if (msg == 'shutdown') {
process.exit(0);
}
});
//app.use(require('response-time')(5));
//app.use(require('connect-timeout')(10000));
app.use(require('body-parser')());
app.use(function(err, req, res, next) {
console.log( err );
err.request = req;
res.send(500, { error: err })
});
app.use(function(req, res, next)
{
if(req.method == 'GET')
{
next();
return;
}
var thiskey = req.body.key;
if(!key || thiskey == key)
{
next();
}
else
{
res.send(403);
}
});
app.get('/', function(req, res) {
res.type( "text/plain" );
res.send( 200, "Codewars Runner is live!" );
});
app.get('/version', function(req, res) {
res.end(config.version);
});
app.post('/build', function(req, res) {
exec('node build', function(err, stdout, stderr) {
res.end(JSON.stringify({
stdout: stdout,
stderr: stderr
}));
});
});
// updates to the latest code and docker images
// responds to both get and post for backwards comparability and easier debugging.
app.post('/update', function(req, res)
{
//TODO make this require somehwere better
var updateSh = require('child_process').spawn('sh', [ 'setup/update.sh' ], {
cwd: process.env.PWD
});
var buffer = [];
updateSh.stdout.on('data', function(data)
{
buffer.push(data);
});
updateSh.stderr.on('data', function(data)
{
buffer.push(data);
});
updateSh.on('exit', function (code)
{
buffer.push(code);
res.end(buffer.join(''));
//now that the update is finished, reload the server
require('child_process').spawn('sh', ['setup/reload.sh'], {detached: true}).unref();
//this will kill us, so we don't need to do anything more
});
useTimeout(90000, res, buffer);
});
app.post('/run', function(req, res) {
var image = req.body.image,
taggedImage = docker.taggedImage(image);
delete req.body.image;
console.time(taggedImage);
console.log(taggedImage);
docker.run(image, 'run', req.body, function(error, data, stdout, stderr){
console.timeEnd(taggedImage);
json = safeParse(stdout);
if (json) {
json.freeMem = os.freemem();
json.v = config.version;
res.end(JSON.stringify(json));
} else {
console.warn('stdout did not return valid data, going with stderr');
console.info(stderr);
error = error || {};
res.end(JSON.stringify({
statusCode: error.statusCode,
failed: {
stdout: stdout,
stderr: stderr,
reason: error.reason
},
freeMem: os.freemem(),
v: config.version,
details: error.json
}));
}
});
useTimeout(10000, res);
});
app.get('/status', function(req, res) {
docker.run(null, 'run', {l: 'javascript', c: 'console.log(1+1)'}, function(err, data, stdout, stderr) {
res.end(JSON.stringify({
healthy: stdout.indexOf('stdout":"2\\n",') > 0,
freeMem: os.freemem(),
version: config.version,
dryRun: {
error: err && err.toString(),
data: data,
stdout: stdout,
stderr: stderr
}
}));
});
useTimeout(5000, res);
});
function useTimeout(timeout, res, buffer) {
setTimeout(function() {
res.end(JSON.stringify({
buffer: buffer && buffer.join(''),
timeout: true,
reason: 'Response timed out, took longer than ' + timeout + 'ms'
}));
}, timeout);
}
function safeParse(json) {
try
{
return json ? JSON.parse(json) : null;
}
catch(ex)
{
console.log(ex);
return null
}
}
// for testing only
//app.post('/run', function(req, res) {
// var image = req.body.image || 'codewars/cli-runner';
// delete req.body.image;
//
// docker.runCmd(image, 'run', req.body, function(error, out){
// console.log(out);
// res.end(out);
// });
//});
server = app.listen(8080, function() {
console.log('Listening on port %d', server.address().port);
});