piston/docker/execute

92 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
dir="$( cd "$( dirname "$0" )" && pwd )"
if [ -z "$1" ]; then
echo "invalid args"
exit
fi
if [ -z "$2" ]; then
echo "invalid args"
exit
fi
lang=$1
filepath=$(realpath $2)
file=$(basename $2)
argpath="/tmp/$(date +%s%N).args"
arg=$(basename $argpath)
# write arg file
echo "${@:3}" > $argpath
bin=
case "$lang" in
"python2")
bin=executor_python2
;;
"python" | "python3")
bin=executor_python3
;;
"ruby")
bin=executor_ruby
;;
"javascript" | "js" | "node")
bin=executor_node
;;
"c")
bin=executor_c
;;
"cpp" | "c++")
bin=executor_cpp
;;
"go")
bin=executor_go
;;
"c#" | "csharp" | "cs")
bin=executor_csharp
;;
"r")
bin=executor_r
;;
"php")
bin=executor_php
;;
"nasm" | "asm")
bin=executor_nasm
;;
"java")
bin=executor_java
;;
*)
echo "invalid language"
exit
esac
docker run \
-m 64m \
--network none \
--rm \
--log-driver none \
-v $filepath:/$file:ro \
-v $argpath:/$arg:ro \
-v $dir/executors/python2:/executor_python2:ro \
-v $dir/executors/python3:/executor_python3:ro \
-v $dir/executors/ruby:/executor_ruby:ro \
-v $dir/executors/node:/executor_node:ro \
-v $dir/executors/c:/executor_c:ro \
-v $dir/executors/cpp:/executor_cpp:ro \
-v $dir/executors/go:/executor_go:ro \
-v $dir/executors/csharp:/executor_csharp:ro \
-v $dir/executors/r:/executor_r:ro \
-v $dir/executors/php:/executor_php:ro \
-v $dir/executors/nasm:/executor_nasm:ro \
-v $dir/executors/java:/executor_java:ro \
piston \
runuser \
-l runner \
-c "bash /$bin /$file $file 2>&1 | head -c 65536"
rm -f /tmp/$file
rm -f /tmp/$arg