Skip to content

Instantly share code, notes, and snippets.

@paurkedal
Created September 8, 2016 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paurkedal/566c42fadd64100be1f3e3910fbd992d to your computer and use it in GitHub Desktop.
Save paurkedal/566c42fadd64100be1f3e3910fbd992d to your computer and use it in GitHub Desktop.
#! /bin/bash
set -e
if [ -r /etc/opam-deploy.conf ]; then
. /etc/opam-deploy.conf
fi
rsync_options="-rlptD --delete"
backlog=3
run() { echo "+ $*"; "$@"; }
confirm()
{
while true; do
echo -n "$*"
read ans
case "$ans" in
y|yes) return;;
n|no) exit;;
esac
done
}
if [ -z "$BUILD_HOST" ]; then
echo "BUILD_HOST must be set in /etc/opam-deploy.conf."
fi
if [ $# -eq 0 ]; then
if [ -z "$OPAMSWITCH" ]; then
echo "Set OPAMSWITCH in /etc/opam-deploy.conf or pass it as an argument."
exit 64
fi
switch="$OPAMSWITCH"
elif [ $# -eq 1 ]; then
switch="$1"
else
echo "Usage: $0 [SWITCH]"
exit 64
fi
next="/opt/opam/${switch}-next"
[ -e /opt/opam ] || run install -d -m 755 /opt/opam
if [ ! -e "${next}" -a -e "/opt/opam/${switch}" ]; then
run cp -al "/opt/opam/${switch}" "${next}"
fi
run rsync $rsync_options "rsync://${BUILD_HOST}/opam/${switch}/" "${next}/"
confirm "Activate ${next}? "
i=`expr $backlog + 1`
if [ -e "/opt/opam/${switch}.prev-$i" ]; then
run rm -rf "/opt/opam/${switch}.prev-$i"
fi
for i in `seq $backlog -1 1`; do
if [ -e "/opt/opam/${switch}.prev-$i" ]; then
run mv "/opt/opam/${switch}.prev-$i" "/opt/opam/${switch}.prev-`expr $i + 1`"
fi
done
if [ -e "/opt/opam/${switch}" ]; then
run mv "/opt/opam/${switch}" "/opt/opam/${switch}.prev-1"
fi
run mv "${next}" "/opt/opam/${switch}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment