piston/lxc/execute

60 lines
1.4 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
dir="$( cd "$( dirname "$0" )" && pwd )"
2018-11-02 06:21:22 +01:00
touch $dir/lockfile
if [ -z "$1" ]; then
echo "invalid args"
exit
fi
if [ -z "$2" ]; then
echo "invalid args"
exit
fi
lang=$1
2018-11-02 06:21:22 +01:00
epoch=$(date +%s%N)
basepath="/var/lib/lxc/piston/rootfs"
filepath="/tmp/$epoch/code.code"
argpath="/tmp/$epoch/args.args"
arg=$(basename $argpath)
# write arg file
mkdir -p $basepath/tmp/$epoch
chmod 777 $basepath/tmp/$epoch
cat $2 > $basepath$filepath
echo "${@:3}" > $basepath$argpath
2018-10-24 07:34:33 +02:00
# process incrementor
2018-11-02 06:21:22 +01:00
exec 200>$dir/lockfile
flock -x 200
2018-10-24 07:34:33 +02:00
oldinc=$(cat $dir/i | awk '{$1=$1};1')
newinc=$(expr $oldinc + 1)
if (( newinc >= 151 )); then
newinc=1
echo 1 > $dir/i
else
echo $newinc > $dir/i
fi
2018-11-02 06:21:22 +01:00
exec 200>&-
2018-10-24 07:34:33 +02:00
# runner
timeout -s KILL 20 \
lxc-attach --clear-env -n piston -- \
2021-01-14 20:14:26 +01:00
/bin/bash -l -c "bash /exec/$lang $newinc $epoch"
2018-10-24 07:34:33 +02:00
# process janitor
lxc-attach --clear-env -n piston -- \
/bin/bash -l -c "\
2018-10-25 01:34:25 +02:00
for i in {1..100}; do pkill -u runner$newinc --signal SIGKILL; done ;\
2021-01-12 19:50:50 +01:00
find /tmp -user runner$newinc -exec /bin/rm -rf {} \; ;\
find /var/tmp -user runner$newinc -exec /bin/rm -rf {} \; ;\
find /var/lock -user runner$newinc -exec /bin/rm -rf {} \; ;\
2021-01-12 19:50:50 +01:00
find /dev/shm -user runner$newinc -exec /bin/rm -rf {} \; ;\
find /run/lock -user runner$newinc -exec /bin/rm -rf {} \; ;\
2018-10-25 01:40:21 +02:00
" > /dev/null 2>&1 &
2018-10-22 23:53:55 +02:00
rm -rf $basepath/tmp/$epoch