-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (47 loc) · 1.25 KB
/
server.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
'use strict';
var Docker = require('dockerode')
var docker = new Docker({socketPath: '/var/run/docker.sock'})
var app = require('express')()
var http = require('http').Server(app)
var io = require('socket.io')(http)
var Docker = require('dockerode');
var docker = new Docker({
socketPath: '/var/run/docker.sock'
});
function runExec(container, command, callback) {
var options = {
Cmd: ['python', '-c', command],
Env: ['VAR=ttslkfjsdalkfj'],
AttachStdout: true,
AttachStderr: true
};
container.exec(options, function(err, exec) {
if (err) return;
exec.start(function(err, stream) {
if (err) return;
var chunks = []
stream.on('data', function(chunk) {
chunks.push(chunk.toString().replace(/[^ -~\n]+/g, ""))
})
stream.on('end', function() {
callback(chunks.join("\n").split('\n'))
})
});
});
}
io.on('connection', function(socket) {
socket.on('run', function(code) {
docker.createContainer({
Image: 'python',
Tty: true,
Cmd: ['/bin/bash']
}, function(err, container) {
container.start({}, function(err, data) {
runExec(container, code, (m) => socket.emit('result', m));
});
});
})
})
http.listen(3000, function() {
console.log('listening on 3000')
})