From 700b66c183b653353bec3ce9658c105f56b8134c Mon Sep 17 00:00:00 2001 From: 1computer1 Date: Sat, 15 Jun 2019 21:22:43 -0400 Subject: [PATCH] Create new workspace for every eval --- docker/apl/run.sh | 4 ---- docker/bash/run.sh | 4 ---- docker/c/run.sh | 4 ---- docker/clojure/run.sh | 4 ---- docker/cpp/run.sh | 4 ---- docker/csharp/run.sh | 4 ---- docker/elixir/run.sh | 4 ---- docker/fsharp/run.sh | 4 ---- docker/go/run.sh | 4 ---- docker/haskell/run.sh | 4 ---- docker/java/run.sh | 4 ---- docker/lua/run.sh | 4 ---- docker/ocaml/run.sh | 4 ---- docker/pascal/run.sh | 4 ---- docker/perl/run.sh | 4 ---- docker/php/run.sh | 4 ---- docker/prolog/run.sh | 4 ---- docker/python2/run.sh | 4 ---- docker/python3/run.sh | 4 ---- docker/racket/run.sh | 4 ---- docker/ruby/run.sh | 4 ---- docker/rust/run.sh | 4 ---- src/struct/LanguageHandler.js | 28 +++++++++++++++++----------- 23 files changed, 17 insertions(+), 99 deletions(-) diff --git a/docker/apl/run.sh b/docker/apl/run.sh index ca97857..8ac5a27 100644 --- a/docker/apl/run.sh +++ b/docker/apl/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.apl apl --OFF -s -f program.apl || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/bash/run.sh b/docker/bash/run.sh index f2b44e4..5dbb459 100644 --- a/docker/bash/run.sh +++ b/docker/bash/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.sh bash program.sh || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/c/run.sh b/docker/c/run.sh index bffd691..f8f5345 100644 --- a/docker/c/run.sh +++ b/docker/c/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.c gcc program.c -o program && ./program || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/clojure/run.sh b/docker/clojure/run.sh index 43196d7..dc5b764 100644 --- a/docker/clojure/run.sh +++ b/docker/clojure/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.clj clojure program.clj || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/cpp/run.sh b/docker/cpp/run.sh index b708d1f..d9e2f85 100644 --- a/docker/cpp/run.sh +++ b/docker/cpp/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.cpp g++ program.cpp -o program && ./program || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/csharp/run.sh b/docker/csharp/run.sh index 934f897..dcf5f79 100644 --- a/docker/csharp/run.sh +++ b/docker/csharp/run.sh @@ -1,10 +1,6 @@ if [ "$EVAL_EXPR" = "true" ]; then printf %s "$1" | csharp -e else - mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.cs csc program.cs >/dev/null && mono program.exe || true - - cd .. && rm -rf "$CODEDIR" fi diff --git a/docker/elixir/run.sh b/docker/elixir/run.sh index 50d1768..ab64413 100644 --- a/docker/elixir/run.sh +++ b/docker/elixir/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.exs elixir program.exs || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/fsharp/run.sh b/docker/fsharp/run.sh index fe0fa14..2a0d0a2 100644 --- a/docker/fsharp/run.sh +++ b/docker/fsharp/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.fs fsharpc --optimize- program.fs >/dev/null && mono program.exe || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/go/run.sh b/docker/go/run.sh index a6ee9a5..9f4c157 100644 --- a/docker/go/run.sh +++ b/docker/go/run.sh @@ -1,7 +1,3 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - export GOCACHE=/tmp/"$CODEDIR"/cache printf %s "$1" > program.go go run program.go || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/haskell/run.sh b/docker/haskell/run.sh index 1017cb1..690c29e 100644 --- a/docker/haskell/run.sh +++ b/docker/haskell/run.sh @@ -1,10 +1,6 @@ if [ "$EVAL_EXPR" = "true" ]; then ghc -e "$1" || true else - mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.hs ghc -e main program.hs || true - - cd .. && rm -rf "$CODEDIR" fi diff --git a/docker/java/run.sh b/docker/java/run.sh index 5f54b23..db8e8ce 100644 --- a/docker/java/run.sh +++ b/docker/java/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > Main.java javac Main.java && java Main || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/lua/run.sh b/docker/lua/run.sh index eea2f9f..f229802 100644 --- a/docker/lua/run.sh +++ b/docker/lua/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.lua lua5.3 program.lua || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/ocaml/run.sh b/docker/ocaml/run.sh index 2c56ae7..cdaaa9a 100644 --- a/docker/ocaml/run.sh +++ b/docker/ocaml/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.ml ocamlopt -cclib --static -o program program.ml && ./program || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/pascal/run.sh b/docker/pascal/run.sh index 26a3896..99afe11 100644 --- a/docker/pascal/run.sh +++ b/docker/pascal/run.sh @@ -1,5 +1,3 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.pas # fpc does not use stderr, ld however does, capture both @@ -10,5 +8,3 @@ if [ $? -eq 0 ]; then else printf %s "$res" fi - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/perl/run.sh b/docker/perl/run.sh index 12dda3e..3ba6d9e 100644 --- a/docker/perl/run.sh +++ b/docker/perl/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.pl perl program.pl || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/php/run.sh b/docker/php/run.sh index 6048485..aeac323 100644 --- a/docker/php/run.sh +++ b/docker/php/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.php php program.php || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/prolog/run.sh b/docker/prolog/run.sh index 7e6ad7a..81cbdee 100644 --- a/docker/prolog/run.sh +++ b/docker/prolog/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.pl swipl --quiet program.pl || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/python2/run.sh b/docker/python2/run.sh index 3f782bb..480954f 100644 --- a/docker/python2/run.sh +++ b/docker/python2/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.py python program.py || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/python3/run.sh b/docker/python3/run.sh index 3f782bb..480954f 100644 --- a/docker/python3/run.sh +++ b/docker/python3/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.py python program.py || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/racket/run.sh b/docker/racket/run.sh index e371054..0b89642 100644 --- a/docker/racket/run.sh +++ b/docker/racket/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.rkt racket program.rkt || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/ruby/run.sh b/docker/ruby/run.sh index 8090145..688c3b0 100644 --- a/docker/ruby/run.sh +++ b/docker/ruby/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.rb ruby program.rb || true - -cd .. && rm -rf "$CODEDIR" diff --git a/docker/rust/run.sh b/docker/rust/run.sh index 5b8eba6..b150608 100644 --- a/docker/rust/run.sh +++ b/docker/rust/run.sh @@ -1,6 +1,2 @@ -mkdir "$CODEDIR" && cd "$CODEDIR" - printf %s "$1" > program.rs rustc -C opt-level=0 --color never program.rs && ./program || true - -cd .. && rm -rf "$CODEDIR" diff --git a/src/struct/LanguageHandler.js b/src/struct/LanguageHandler.js index 068b957..5107f91 100644 --- a/src/struct/LanguageHandler.js +++ b/src/struct/LanguageHandler.js @@ -6,6 +6,8 @@ const childProcess = require('child_process'); const util = require('util'); const path = require('path'); +const exec = util.promisify(childProcess.exec); + class LanguageHandler extends AkairoHandler { constructor(client, { directory, @@ -93,15 +95,16 @@ class LanguageHandler extends AkairoHandler { const cpus = this.getCompilerConfig(dockerID, 'cpus', 'string'); const memory = this.getCompilerConfig(dockerID, 'memory', 'string'); 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 { - 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 }); // eslint-disable-next-line no-console console.log(`Started container ${name}.`); @@ -116,9 +119,11 @@ class LanguageHandler extends AkairoHandler { const { evalQueue, setupQueue } = this.queues.get(dockerID); return evalQueue.enqueue(async () => { 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', [ - 'exec', - `-eCODEDIR=${Date.now()}`, + 'exec', '-u1001:1001', `-w/tmp/eval/${now}`, ...Object.entries(env).map(([k, v]) => `-e${k}=${v}`), name, '/bin/sh', '/var/run/run.sh', code ]); @@ -126,6 +131,7 @@ class LanguageHandler extends AkairoHandler { const timeout = this.getCompilerConfig(dockerID, 'timeout', 'number'); try { const result = await this.handleSpawn(proc, timeout); + await exec(`docker exec ${name} rm -rf eval/${now}`); return result; } catch (err) { this.containers.delete(dockerID); @@ -196,7 +202,7 @@ class LanguageHandler extends AkairoHandler { 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 console.log(`Killed container ${name}.`); }