bscpkgs/garlic/sh/garlic

232 lines
4.5 KiB
Plaintext
Raw Normal View History

#!/bin/bash -e
2020-10-30 00:47:45 +08:00
garlicPrefix=@garlicPrefix@
2020-10-30 00:47:45 +08:00
sshHost=@sshHost@
PATH=@PATH@
usage() { echo "Usage: garlic [-RFwv] trebuchet" 1>&2; exit 1; }
2020-11-06 02:31:21 +08:00
findClosure() {
what=$1
from=$2
mexp=$(nix-store -qR "$from" | grep -E -- "$what")
n=$(echo "$mexp" | awk 'BEGIN { count=0 } NF { count++ } END { print count }')
if [ $n -eq 0 ]; then
>&2 echo "$exp: $what not found"
exit 1
fi
if [ $n -gt 1 ]; then
>&2 echo "$exp: multiple $what found"
exit 1
fi
echo "$mexp"
2020-10-30 00:47:45 +08:00
}
findExperiment() {
grep -o -- "/nix/store/.*-experiment" "$1"
}
findOutputDir() {
garlic_sandbox=$(nix show-config |\
grep extra-sandbox-paths |\
grep -o '/garlic=[^ ]*' || true)
if [ -z "$garlic_sandbox" ]; then
>&2 echo "Missing extra-sandbox-paths /garlic mountpoint"
>&2 echo "Check the ~/.config/nix/nix.conf file"
exit 1
fi
mountdir_rel=$(echo "$garlic_sandbox" | sed 's@^/garlic=@@g')
mountdir=$(readlink -f "$mountdir_rel")
echo "$mountdir/cache"
}
2020-10-30 00:47:45 +08:00
drvFromOutput() {
nix-store -q --deriver $1
}
checkTrebuchet() {
if [ ! -e "$trebuchet" ]; then
>&2 echo "$trebuchet: not found"
exit 1
fi
if [ ! -f "$trebuchet" ]; then
>&2 echo "$trebuchet: not a file"
exit 1
fi
# FIXME: We need a better way to determine a trebuchet
if [ -z "$(grep "This trebuchet launches" $trebuchet)" ]; then
>&2 echo "$trebuchet: not a trebuchet"
exit 1
fi
return 0
}
checkExperiment() {
if [ ! -e "$experiment" ]; then
>&2 echo "$experiment: not found"
exit 1
fi
if [ ! -f "$experiment" ]; then
>&2 echo "$experiment: not a file"
exit 1
fi
# FIXME: We need a better way to determine a experiment
if [ -z "$(grep "This is an experiment" $experiment)" ]; then
>&2 echo "$experiment: not an experiment"
exit 1
fi
return 0
}
checkMountpoint() {
if [ ! -e "$garlicPrefix/garlic.control" ]; then
>&2 echo "error: missing $garlicPrefix/garlic.control"
>&2 echo "Is the mountpoint enabled?"
exit 1
fi
}
2020-10-30 00:47:45 +08:00
do_fetch() {
expName=$(basename $experiment)
user=$(ssh -G "$sshHost" | awk '/^user /{print $2}')
exp=$garlicPrefix/$user/out/$expName
2020-10-30 00:47:45 +08:00
if [ ! -e "$exp" ]; then
echo "missing experiment: $exp"
exit 1
fi
cwd=$(pwd)
repeat=1
while [ 1 ]; do
repeat=0
cd $exp
test $verbose && >&2 echo "$exp: checking units"
for unit in *-unit; do
cd $exp/$unit
if [ ! -e status ]; then
test $verbose && >&2 echo "$unit: no status"
repeat=1
else
st=$(cat status)
test $verbose && >&2 echo "$unit: $st"
if [ "$st" != "completed" ]; then
repeat=1
fi
fi
done
if [ $repeat -eq 0 ]; then
break
fi
if [ $waitResults -eq 1 ]; then
#echo "waiting 3 seconds to try again"
sleep 3
else
break
fi
done
if [ $repeat -eq 1 ]; then
>&2 echo "$exp: execution incomplete"
exit 1
fi
cd "$cwd"
test $verbose && >&2 echo "$exp: execution complete, fetching results"
mkdir -p "$outputDir"
2020-10-30 00:47:45 +08:00
2021-02-25 18:16:35 +08:00
rsync -vrt --copy-links \
2020-10-30 00:47:45 +08:00
--include='*/*/garlic_config.json' \
--include='*/*/std*.log' \
--include='*/*/*/std*.log' \
2021-02-08 21:14:08 +08:00
--include='*/*/*/.garlic' \
--include='*/*/*/.garlic/**' \
2020-10-30 00:47:45 +08:00
--exclude='*/*/*/*' \
"$exp" "$outputDir"
2020-10-30 00:47:45 +08:00
if [ ! $enableKeep ]; then
nix-build -E "(with import ./default.nix; \
garlic.pp.store { \
trebuchet = (import \"$trebuchetDrv\" ); \
experiment = (import \"$experimentDrv\"); \
})"
2020-10-30 00:47:45 +08:00
rm -rf "$outputDir/$expName"
fi
2020-10-30 00:47:45 +08:00
}
do_run() {
>&2 $trebuchet
2020-10-30 00:47:45 +08:00
}
do_delete() {
expName=$(basename $experiment)
rm -rf $outputDir/$expName
}
2020-10-30 00:47:45 +08:00
waitResults=1
2020-11-06 02:31:21 +08:00
verbose=
2020-10-30 00:47:45 +08:00
operation=
target=
enableRun=
enableFetch=
enableKeep=
enableDelete=
2020-10-30 00:47:45 +08:00
while getopts "vwRFKD" o; do
2020-10-30 00:47:45 +08:00
case "${o}" in
R) enableRun=1 ;;
F) enableFetch=1 ;;
K) enableKeep=1 ;;
D) enableDelete=1 ;;
2020-10-30 00:47:45 +08:00
w) waitResults=0 ;;
2020-11-06 02:31:21 +08:00
v) verbose=1 ;;
2020-10-30 00:47:45 +08:00
*) usage ;;
esac
done
shift $((OPTIND-1))
trebuchet="$1"
2020-10-30 00:47:45 +08:00
if [ -z "$enableRun" -a -z "$enableFetch" -a -z "$enableDelete" ]; then
2020-10-30 00:47:45 +08:00
>&2 echo "missing operation"
usage
fi
if [ -z "$trebuchet" ]; then
2020-11-06 02:31:21 +08:00
>&2 echo "missing experiment"
usage
2020-10-30 00:47:45 +08:00
fi
checkMountpoint
2020-11-06 02:31:21 +08:00
checkTrebuchet $trebuchet
outputDir=$(findOutputDir)
experiment=$(findExperiment "$trebuchet")
2020-11-06 02:31:21 +08:00
checkExperiment $experiment
2020-10-30 00:47:45 +08:00
trebuchetDrv=$(drvFromOutput $trebuchet)
experimentDrv=$(drvFromOutput $experiment)
if [ $enableRun ]; then do_run; fi
if [ $enableFetch ]; then do_fetch; fi
if [ $enableDelete ]; then do_delete; fi