Skip to content

Instantly share code, notes, and snippets.

@DanielG
Created January 1, 2015 19:51
Show Gist options
  • Select an option

  • Save DanielG/3021e374c7ae8c8779a3 to your computer and use it in GitHub Desktop.

Select an option

Save DanielG/3021e374c7ae8c8779a3 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -e
server=$1
shift
usage () {
printf '%s' "\
Usage: $0 [OPTIONS...] SERVER\n\
Options:\n\
--with-ghc=GHC_PATH\n\
Set the path to the GHC executable.\n\
--with-cabal=CABAL_PATH\n\
Set the path to the cabal-install (cabal) executable.\n\
--with-remote-ghc=REMOTE_GHC_PATH\n\
Set the path to the GHC executable on the system running the\n\
cabal build.\n\
--with-remote-cabal=REMOTE_CABAL_PATH\n\
Set the path to the cabal-install (cabal) executable on the\n\
system running the cabal build\n\
"
}
GHC_PATH=ghc
REMOTE_GHC_PATH=ghc
CABAL_PATH=cabal
REMOTE_CABAL_PATH=cabal
OPTS=$(getopt -o h --long with-ghc:,with-cabal:,with-remote-ghc:,with-remote-cabal: -n "$(basename $0)" -- "$@")
eval set -- "$OPTS"
while true ; do
case "$1" in
-h|--help) usage; exit; ;;
--with-ghc) GHC_PATH=$2; shift; shift ;;
--with-cabal) CABAL_PATH=$2; shift; shift ;;
--with-remote-ghc) REMOTE_GHC_PATH=$2; shift; shift ;;
--with-remote-cabal) REMOTE_GHC_CABAL=$2; shift; shift ;;
--) shift; break ;;
*) echo "Error parsing argument: $1">&2; exit 1 ;;
esac
done
REMOTE_CABAL_ARGS="$@"
control_dir=$(mktemp -p "${TMPDIR:-/tmp/}" -d cabalr-control-XXXX) || exit 1
trap 'rm -r '"$control_dir" 0 2 15
SSH_OPTS="-o ControlMaster=auto -o ControlPath=$control_dir/%l-%h-%p-%u-%r"
SSH="ssh $server $SSH_OPTS"
ssh_init() {
ssh -NnT -o ControlPersist=yes
trap 'ssh -NnT -O exit > /dev/null' 0 2 15
}
if [ ! -f ./*.cabal ]; then
echo "Not in a cabal package" >&2
exit 1
fi
if ! [ -f ./*.sandbox.config -a -d ./.cabal-sandbox ]; then
echo "Must be in a cabal sandbox!" >&2
echo "run: $ cabal sandbox init" >&2
exit 1
fi
GHC_VERSION='"$GHC_PATH" --version | sed '"'"'s/^.*version //'"'"
GHC_TARGET_ARCH='"$GHC_PATH" --info "target platform" | $GHC_PATH -e "do s <- getContents; let Just x = lookup \"Target platform\" $ read s in putStrLn x"'
ghc_version="$(eval $GHC_VERSION)"
ghc_target_arch="$(eval $GHC_TARGET_ARCH)"
$SSH -- sh -x <<EOF
set -e
GHC_PATH=$REMOTE_GHC_PATH
CABAL_PATH=$REMOTE_CABAL_PATH
remote_ghc_version="\$($GHC_VERSION)"
if [ x"$ghc_version" != x"\$remote_ghc_version" ]; then
echo "GHC version mismatch! (local: $ghc_version, remote: \$remote_ghc_version)" >&2
exit 1
fi
remote_ghc_target_arch="\$($GHC_TARGET_ARCH)"
if [ x"$ghc_target_arch" != x"\$remote_ghc_target_arch" ]; then
echo "GHC target arch mismatch! (local: $ghc_target_arch, remote: \$remote_ghc_target_arch)" >&2
exit 1
fi
EOF
rv=$?
if [ $rv -ne 0 ]; then
exit $rv
fi
$SSH -- mkdir -p $(pwd)
rsync -e "ssh $SSH_OPTS" -ar \
--delete \
./ "$server:$(pwd)"
if [ -e cabal.remote.config ]; then
build_depends=$(cat cabal.remote.config \
| sed -n -e 's/build-depends:[[:space:]]*//p' -e 's/,//g')
fi
$SSH -- sh -x <<EOF
set -e
GHC_PATH=$REMOTE_GHC_PATH
CABAL_PATH=$REMOTE_CABAL_PATH
# TODO: CABAL_OPTS, GHC path stuff
cd $(pwd)
"$REMOTE_CABAL_PATH" sandbox init
"$REMOTE_CABAL_PATH" update
if [ "$build_depends" ]; then
export PATH=\$(pwd)/.cabal-sandbox/bin:$PATH
"$REMOTE_CABAL_PATH" install --with-ghc=$REMOTE_GHC_PATH $build_depends $REMOTE_CABAL_INSTALL_ARGS
fi
"$REMOTE_CABAL_PATH" $REMOTE_CABAL_ARGS --with-ghc=$REMOTE_GHC_PATH
EOF
if [ x"$?" != x"0" ]; then
echo "Remote end failed." >&2
exit 1
fi
rsync -e "ssh $SSH_OPTS" -arv --delete \
"$server:$(pwd)/.cabal-sandbox/" "./.cabal-sandbox/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment