Skip to content

Instantly share code, notes, and snippets.

@johann8384
Created October 17, 2013 19:39
Show Gist options
  • Save johann8384/7030946 to your computer and use it in GitHub Desktop.
Save johann8384/7030946 to your computer and use it in GitHub Desktop.
post-receive hook for synchronizing BIND masters
#!/bin/bash
unset GIT_DIR
REPO_DIR=/var/lib/gitosis/repositories/internal_dns.git
CLONE_DIR=/var/named/chroot/var/named/data
ERR_OUT=/tmp/named_error.out.$$
if [ ! -e "${CLONE_DIR}/.git" ]; then
git clone "$REPO_DIR" "$CLONE_DIR"
fi
cd "$CLONE_DIR"
git fetch
git checkout master
git reset --hard origin/master
# Syntax check
sudo /usr/sbin/named-checkconf -t /var/named/chroot -z &> $ERR_OUT
if [ $? != 0 ]; then
echo "*******************"
echo "SYNTAX CHECK FAILED"
echo "*******************"
echo -e "DNS config syntax check failed:\n\n$(<$ERR_OUT)" | mail -s "DNS config syntax check failed" -c notifications@example.org
exit 1
fi
rm $ERR_OUT 2>/dev/null
# Since this is the master and nothing is querying us, it's "cleaner" and easier to just restart vs. reload.
# This also handles the case of a first deployment where named is not yet running.
sudo /sbin/service named stop
# Make sure it's really stopped
count=0
while /bin/true; do
count=$(($count + 1))
if [ $count -ge 10 ]; then
echo "Not waiting anymore for named to shut down"
exit 1
fi
if [ -n "$(pgrep named)" ]; then
echo "The 'named' process is still running"
sleep 1s
else
echo "The 'named' process has stopped"
break
fi
done
# Start it back up
sudo /sbin/service named start
# Push updates to other masters
MASTERS="master1.example.org master2.example.org master3.example.org"
# This will technically trigger an endless loop of pushes and repushes, but the fact that the post-receive hook isn't run if no new objects are pushed "saves" us
for MASTER in $MASTERS; do
if [ "$(hostname)" = $MASTER ]; then
# We don't need to push to ourselves
continue
fi
git push --mirror ssh://gitosis@${MASTER}/internal_dns.git
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment