bscpkgs/garlic/sh/garlic
Rodrigo Arias Mallo 3445a72686 garlic tool: copy recursively from .garlic/
It allows an experiment to store a CTF trace in the resultTree (which is
not recommended for large traces).
2021-03-12 11:13:35 +01:00

232 lines
4.5 KiB
Bash
Executable File

#!/bin/bash -e
garlicPrefix=@garlicPrefix@
sshHost=@sshHost@
PATH=@PATH@
usage() { echo "Usage: garlic [-RFwv] trebuchet" 1>&2; exit 1; }
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"
}
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"
}
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
}
do_fetch() {
expName=$(basename $experiment)
user=$(ssh -G "$sshHost" | awk '/^user /{print $2}')
exp=$garlicPrefix/$user/out/$expName
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"
rsync -vrt --copy-links \
--include='*/*/garlic_config.json' \
--include='*/*/std*.log' \
--include='*/*/*/std*.log' \
--include='*/*/*/.garlic' \
--include='*/*/*/.garlic/**' \
--exclude='*/*/*/*' \
"$exp" "$outputDir"
if [ ! $enableKeep ]; then
nix-build -E "(with import ./default.nix; \
garlic.pp.store { \
trebuchet = (import \"$trebuchetDrv\" ); \
experiment = (import \"$experimentDrv\"); \
})"
rm -rf "$outputDir/$expName"
fi
}
do_run() {
>&2 $trebuchet
}
do_delete() {
expName=$(basename $experiment)
rm -rf $outputDir/$expName
}
waitResults=1
verbose=
operation=
target=
enableRun=
enableFetch=
enableKeep=
enableDelete=
while getopts "vwRFKD" o; do
case "${o}" in
R) enableRun=1 ;;
F) enableFetch=1 ;;
K) enableKeep=1 ;;
D) enableDelete=1 ;;
w) waitResults=0 ;;
v) verbose=1 ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
trebuchet="$1"
if [ -z "$enableRun" -a -z "$enableFetch" -a -z "$enableDelete" ]; then
>&2 echo "missing operation"
usage
fi
if [ -z "$trebuchet" ]; then
>&2 echo "missing experiment"
usage
fi
checkMountpoint
checkTrebuchet $trebuchet
outputDir=$(findOutputDir)
experiment=$(findExperiment "$trebuchet")
checkExperiment $experiment
trebuchetDrv=$(drvFromOutput $trebuchet)
experimentDrv=$(drvFromOutput $experiment)
if [ $enableRun ]; then do_run; fi
if [ $enableFetch ]; then do_fetch; fi
if [ $enableDelete ]; then do_delete; fi