piston/lxc/execute

56 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
dir="$( cd "$( dirname "$0" )" && pwd )"
touch $dir/lockfile
if [ -z "$1" ] || [ -z "$2" ]; then
echo "invalid args"
exit
fi
language=$1
id=$2
basepath="/var/lib/lxc/piston/rootfs"
# process incrementor
exec 200>$dir/lockfile
flock 200
touch $dir/i
runner=$(cat $dir/i)
let 'runner = runner % 150 + 1'
echo $runner > $dir/i
exec 200>&-
# prevent users from spying on each other
lxc-attach --clear-env -n piston -- \
/bin/bash -c "
chown runner$runner: -R /tmp/$id
chmod 700 /tmp/$id
" > /dev/null 2>&1
# runner
timeout -s KILL 20 \
lxc-attach --clear-env -n piston -- \
/bin/bash -c "runuser runner$runner /exec/$language $id"
# process janitor
lxc-attach --clear-env -n piston -- \
/bin/bash -c "
while pgrep -u runner$runner > /dev/null
do
pkill -u runner$runner --signal SIGKILL
done
find /tmp -user runner$runner -delete
find /var/tmp -user runner$runner -delete
find /var/lock -user runner$runner -delete
find /dev/shm -user runner$runner -delete
find /run/lock -user runner$runner -delete
" > /dev/null 2>&1 &
rm -rf $basepath/tmp/$id