Create new workspace for every eval

This commit is contained in:
1computer1 2019-06-15 21:22:43 -04:00
parent bc67dbbfd7
commit 700b66c183
23 changed files with 17 additions and 99 deletions

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.apl printf %s "$1" > program.apl
apl --OFF -s -f program.apl || true apl --OFF -s -f program.apl || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.sh printf %s "$1" > program.sh
bash program.sh || true bash program.sh || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.c printf %s "$1" > program.c
gcc program.c -o program && ./program || true gcc program.c -o program && ./program || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.clj printf %s "$1" > program.clj
clojure program.clj || true clojure program.clj || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.cpp printf %s "$1" > program.cpp
g++ program.cpp -o program && ./program || true g++ program.cpp -o program && ./program || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,10 +1,6 @@
if [ "$EVAL_EXPR" = "true" ]; then if [ "$EVAL_EXPR" = "true" ]; then
printf %s "$1" | csharp -e printf %s "$1" | csharp -e
else else
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.cs printf %s "$1" > program.cs
csc program.cs >/dev/null && mono program.exe || true csc program.cs >/dev/null && mono program.exe || true
cd .. && rm -rf "$CODEDIR"
fi fi

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.exs printf %s "$1" > program.exs
elixir program.exs || true elixir program.exs || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.fs printf %s "$1" > program.fs
fsharpc --optimize- program.fs >/dev/null && mono program.exe || true fsharpc --optimize- program.fs >/dev/null && mono program.exe || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,7 +1,3 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
export GOCACHE=/tmp/"$CODEDIR"/cache export GOCACHE=/tmp/"$CODEDIR"/cache
printf %s "$1" > program.go printf %s "$1" > program.go
go run program.go || true go run program.go || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,10 +1,6 @@
if [ "$EVAL_EXPR" = "true" ]; then if [ "$EVAL_EXPR" = "true" ]; then
ghc -e "$1" || true ghc -e "$1" || true
else else
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.hs printf %s "$1" > program.hs
ghc -e main program.hs || true ghc -e main program.hs || true
cd .. && rm -rf "$CODEDIR"
fi fi

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > Main.java printf %s "$1" > Main.java
javac Main.java && java Main || true javac Main.java && java Main || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.lua printf %s "$1" > program.lua
lua5.3 program.lua || true lua5.3 program.lua || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.ml printf %s "$1" > program.ml
ocamlopt -cclib --static -o program program.ml && ./program || true ocamlopt -cclib --static -o program program.ml && ./program || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,5 +1,3 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.pas printf %s "$1" > program.pas
# fpc does not use stderr, ld however does, capture both # fpc does not use stderr, ld however does, capture both
@ -10,5 +8,3 @@ if [ $? -eq 0 ]; then
else else
printf %s "$res" printf %s "$res"
fi fi
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.pl printf %s "$1" > program.pl
perl program.pl || true perl program.pl || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.php printf %s "$1" > program.php
php program.php || true php program.php || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.pl printf %s "$1" > program.pl
swipl --quiet program.pl || true swipl --quiet program.pl || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.py printf %s "$1" > program.py
python program.py || true python program.py || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.py printf %s "$1" > program.py
python program.py || true python program.py || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.rkt printf %s "$1" > program.rkt
racket program.rkt || true racket program.rkt || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.rb printf %s "$1" > program.rb
ruby program.rb || true ruby program.rb || true
cd .. && rm -rf "$CODEDIR"

View file

@ -1,6 +1,2 @@
mkdir "$CODEDIR" && cd "$CODEDIR"
printf %s "$1" > program.rs printf %s "$1" > program.rs
rustc -C opt-level=0 --color never program.rs && ./program || true rustc -C opt-level=0 --color never program.rs && ./program || true
cd .. && rm -rf "$CODEDIR"

View file

@ -6,6 +6,8 @@ const childProcess = require('child_process');
const util = require('util'); const util = require('util');
const path = require('path'); const path = require('path');
const exec = util.promisify(childProcess.exec);
class LanguageHandler extends AkairoHandler { class LanguageHandler extends AkairoHandler {
constructor(client, { constructor(client, {
directory, directory,
@ -93,15 +95,16 @@ class LanguageHandler extends AkairoHandler {
const cpus = this.getCompilerConfig(dockerID, 'cpus', 'string'); const cpus = this.getCompilerConfig(dockerID, 'cpus', 'string');
const memory = this.getCompilerConfig(dockerID, 'memory', 'string'); const memory = this.getCompilerConfig(dockerID, 'memory', 'string');
const name = `comp_iler-${dockerID}-${Date.now()}`; const name = `comp_iler-${dockerID}-${Date.now()}`;
const proc = childProcess.spawn('docker', [
'run', '--rm', `--name=${name}`, '-u1000', '-w/tmp/', '-dt',
'--net=none', `--cpus=${cpus}`,
`-m=${memory}`, `--memory-swap=${memory}`,
`1computer1/comp_iler:${dockerID}`, '/bin/sh'
]);
try { try {
await this.handleSpawn(proc); await exec([
`docker run --rm --name=${name}`,
'-u1000:1000 -w/tmp/ -dt',
`--net=none --cpus=${cpus} -m=${memory} --memory-swap=${memory}`,
`1computer1/comp_iler:${dockerID} /bin/sh`
].join(' '));
await exec(`docker exec ${name} mkdir eval`);
await exec(`docker exec ${name} chmod 711 eval`);
this.containers.set(dockerID, { name }); this.containers.set(dockerID, { name });
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log(`Started container ${name}.`); console.log(`Started container ${name}.`);
@ -116,9 +119,11 @@ class LanguageHandler extends AkairoHandler {
const { evalQueue, setupQueue } = this.queues.get(dockerID); const { evalQueue, setupQueue } = this.queues.get(dockerID);
return evalQueue.enqueue(async () => { return evalQueue.enqueue(async () => {
const { name } = await setupQueue.enqueue(() => this.setupContainer(dockerID)); const { name } = await setupQueue.enqueue(() => this.setupContainer(dockerID));
const now = Date.now();
await exec(`docker exec ${name} mkdir eval/${now}`);
await exec(`docker exec ${name} chmod 777 eval/${now}`);
const proc = childProcess.spawn('docker', [ const proc = childProcess.spawn('docker', [
'exec', 'exec', '-u1001:1001', `-w/tmp/eval/${now}`,
`-eCODEDIR=${Date.now()}`,
...Object.entries(env).map(([k, v]) => `-e${k}=${v}`), ...Object.entries(env).map(([k, v]) => `-e${k}=${v}`),
name, '/bin/sh', '/var/run/run.sh', code name, '/bin/sh', '/var/run/run.sh', code
]); ]);
@ -126,6 +131,7 @@ class LanguageHandler extends AkairoHandler {
const timeout = this.getCompilerConfig(dockerID, 'timeout', 'number'); const timeout = this.getCompilerConfig(dockerID, 'timeout', 'number');
try { try {
const result = await this.handleSpawn(proc, timeout); const result = await this.handleSpawn(proc, timeout);
await exec(`docker exec ${name} rm -rf eval/${now}`);
return result; return result;
} catch (err) { } catch (err) {
this.containers.delete(dockerID); this.containers.delete(dockerID);
@ -196,7 +202,7 @@ class LanguageHandler extends AkairoHandler {
cmd = `docker kill --signal=9 ${name} >/dev/null 2>/dev/null`; cmd = `docker kill --signal=9 ${name} >/dev/null 2>/dev/null`;
} }
await util.promisify(childProcess.exec)(cmd); await exec(cmd);
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log(`Killed container ${name}.`); console.log(`Killed container ${name}.`);
} }