Skip to content

Instantly share code, notes, and snippets.

@ariofrio
Created July 25, 2012 21:48
Show Gist options
  • Save ariofrio/3178891 to your computer and use it in GitHub Desktop.
Save ariofrio/3178891 to your computer and use it in GitHub Desktop.
Post-receive script for Goatee deployment, inspired by Heroku
#!/bin/bash
# Fail fast.
set -e
# Debug.
# set -x
function indent() {
c='s/^/ /'
case $(uname) in
Darwin) sed -l "$c";;
*) sed -u "$c";;
esac
}
# Get state.
OLDPORT=`cat /tmp/goatee.port 2> /dev/null || echo 8000`
OLDDEPLOYDIR=`cat /tmp/goatee.dir 2> /dev/null || true`
OLDPID=`cat /tmp/goatee.pid 2> /dev/null || true`
PORT=$[ 8000 + ($OLDPORT + 100) % 1000 ]
DEPLOYDIR=`mktemp -d /tmp/goatee-$PORT-XXXXXX`
echo "-----> Copying app to a deployment directory"
git archive master | tar -x -C $DEPLOYDIR 2>&1 | indent
cd $DEPLOYDIR
echo "-----> Installing dependencies using npm"
npm install 2>&1 | indent
if [ $OLDPID ]; then
echo "-----> Stopping old version on port $OLDPORT"
#kill -s INT $OLDPID 2>&1 | indent # see https://github.com/ddollar/foreman/issues/235
killall node || true
sudo iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports $OLDPORT 2>&1 | indent
fi
if [ $OLDDEPLOYDIR ]; then
echo "-----> Cleaning up old version on port $OLDPORT"
rm -rf $OLDDEPLOYDIR 2>&1 | indent
fi
echo "-----> Starting app"
foreman start -p $PORT > $DEPLOYDIR.log 2>&1 &
echo $! > /tmp/goatee.pid
echo $PORT > /tmp/goatee.port
echo $DEPLOYDIR > /tmp/goatee.dir
echo "-----> Setting up port redirection 80 <=> $PORT"
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports $PORT 2>&1 | indent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment