cibo

git clone git://git.lin.moe/cibo.git

 1#!/bin/sh
 2
 3set -e
 4
 5: ${varsh_memlimit:="10KiB"}
 6: ${varsh_maxprocs:="1"}
 7# TODO: varshell cpu and timeout
 8
 9function msg() {
10	local color=${2:-green}
11	case "$color" in
12		red) color="31";;
13		green) color="32";;
14		yellow) color="33";;
15		blue) color="34";;
16		gray) color="37";;
17		*) color="32";;
18	esac
19	printf "\033[1;%sm>>>\033[1;0m %s\n" "$color" "$1" | xargs >&2
20}
21
22function fatal() {
23	msg "$1" red
24	exit 1
25}
26
27cifile=$(mktemp)
28trap 'rm -f $cifile' EXIT
29cat > $cifile
30
31run() {
32	local fname=$1 	
33	(
34		cleanup_cmds=""
35		trap 'eval "$cleanup_cmds"' EXIT
36		
37		varfile=$(mktemp)
38		cleanup_cmds="$cleanup_cmds
39				rm -f $varfile"
40		GOMAXPROCS="$varsh_maxprocs" GOMEMLIMIT="$varsh_memlimit" varshell -f "$fname" "$cifile" > $varfile
41		. $varfile
42
43		msg "${fname:+[$fname]} job starting"
44
45		if [ -n "$depends" ]; then
46			msg "${fname:+[$fname]} depends $depends"
47			for dep in "$depends"; do
48				run $dep
49			done
50		fi
51	
52		source @sharedir@/$worker_type
53		${worker_type}_start
54		cleanup_cmds="$cleanup_cmds
55				${worker_type}_stop"
56
57
58		msg "${fname:+[$fname]} entrypoint running"
59		{
60			cat $cifile
61			echo $'\n'$entrypoint
62		} | ${worker_type}_runcmd
63		msg "${fname:+[$fname]} job ended"
64)}
65
66run