-
-
Save alerque/9566806 to your computer and use it in GitHub Desktop.
git pre-commit hook (make executable at .git/hooks/pre-commit) to check dns zones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Fail the hook if anything isn't 100% copacetic | |
set -e | |
# If anything goes wrong, try to show what and clean up after ourselves | |
function show_log () { | |
cat $tmpfile | | |
grep -v 'ignoring out-of-zone data' | | |
grep -v 'loaded serial' | | |
sed "s#$tmpdir/##"; | |
} | |
trap '[[ $? -ne 0 ]] && show_log ; rm -rf $tmpdir' EXIT INT HUP | |
# Exit clean if hook skip requested | |
[[ $SKIP_PRECOMMIT_HOOK = 1 ]] && exit 0 | |
# Operate in the repo root and use temp files | |
cd $(git rev-parse --show-toplevel) | |
tmpdir=$(mktemp -d precommitXXXXXX) | |
tmpfile=$(mktemp $tmpdir/errorsXXXXXX) | |
# Checkout all the bind conf in the repo and any zone files that have changed | |
fuction list_checkables () { | |
git ls-files --cached | | |
grep '\.conf$' | |
git diff-index --cached --name-only HEAD | | |
grep '\.zone$' | |
} | |
list_checkables | git checkout-index --prefix=$tmpdir/ --stdin | |
# For each conf file, test-load every zone | |
find $tmpdir -type f -name '*.conf' | | |
while read conffile; do | |
named-checkconf -z $conffile > $tmpfile || exit 1 | |
done | |
# For each zone file, do strict parse testing | |
# (testing the conf already checks each zone so using a dummy zone suffices) | |
find $tmpdir -type f -name '*.zone' | | |
while read zonefile; do | |
named-checkzone -i full -k fail -m fail -M fail -n fail -r fail -S fail \ | |
example.com $zonefile > $tmpfile || exit 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment