Created
March 29, 2017 14:09
-
-
Save tocttou/b2e97096790463032c241c076776d474 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// no error handling | |
// code not checked to be working - just to illustrate how to do it | |
const crypto = require("crypto"); | |
const mkdirp = require("mkdirp-promise"); | |
const io = require("socket.io")(); | |
const fs = require("fs"); | |
const Docker = require("dockerode"); | |
const byline = require("byline"); | |
const StringDecoder = require("string_decoder").StringDecoder; | |
// using the unix socket interface | |
const docker = new Docker({ socketPath: "/var/run/docker.sock" }); | |
const decoder = new StringDecoder("utf8"); | |
io.on("connection", socket => { | |
socket.on("code-recieved", async function(code) { | |
const uniqueId = crypto.randomBytes(16).toString("hex"); | |
await mkdirp(`/tmp/${uniqueId}`); | |
fs.writeFileSync(`/tmp/${uniqueId}/exec.chpl`, code, "utf8"); | |
docker.createContainer( | |
{ | |
Image: "chapel-latest", | |
name: uniqueId, | |
Cmd: ["/bin/bash", "runner.sh"], | |
Tty: true, | |
Binds: ["/tmp:/tmp"], | |
Env: [`UNIQUE_ID=${uniqueId}`] | |
}, | |
(err, container) => { | |
container.attach( | |
{ stream: true, stdout: true, stderr: true }, | |
(err, stream) => { | |
byline(stream).on("data", line => { | |
// data decoded but not cleaned (removing color codes) | |
// not adapted to HTML either (replacing newline character occurences with <br /> | |
const decodedData = decoder.write(line); | |
socket.emit("outputStream", { | |
uniqueId, | |
type: "data", | |
stream: decodedData | |
}); | |
}); | |
byline(stream).on("error", line => { | |
const decodedData = decoder.write(line); | |
socket.emit("outputStream", { | |
uniqueId, | |
type: "error", | |
stream: decodedData | |
}); | |
}); | |
stream.on("end", () => { | |
socket.emit("outputStream", { uniqueId, type: "exit" }); | |
}); | |
container.start((err, data) => {}); | |
} | |
); | |
} | |
); | |
}); | |
}); | |
// start socket server | |
io.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chpl -o code /tmp/$UNIQUE_ID/exec.chpl | |
chmod +x code | |
./code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment