Tuesday, December 6, 2011

Example Puppet 2.7 git pre-commit script

I had a hard time finding a decent pre-commit script for puppet 2.7. This is composed of snippets I found at code.seas.harvard.edu. The pre-commit script will check the puppet syntax of the changed .pp files, and also check if an attempt has been made to properly document the .pp file.

# install this as .git/hooks/pre-commit to check Puppet manifests
# for errors before committing changes.


[ "$SKIP_PRECOMMIT_HOOK" = 1 ] && exit 0

# Make sure we're at top level of repository.
cd $(git rev-parse --show-toplevel)

trap 'rm -rf $tmpdir $tmpfile1 $tmpfile2' EXIT INT HUP
tmpdir=$(mktemp -d precommitXXXXXX)
tmpfile1=$(mktemp errXXXXXX)
tmpfile2=$(mktemp errXXXXXX)

echo "$(basename $0): Validating changes."

# Here we copy files out of the index into a temporary directory. This
# protects us from a the situation in which we have staged an invalid
# configuration using ``git add`` but corrected the changes in the
# working directory. If we checked the files "in place", we would
# fail to detect the errors.

git diff-index --cached --name-only HEAD |
grep '\.pp$' |
git checkout-index --stdin --prefix=$tmpdir/

find $tmpdir -type f -name '*.pp' |
while read manifest; do
puppet parser validate $manifest | sed "s#$tmpdir/##" >> $tmpfile1 2>&1

if ! head -1 $manifest | grep -q '^#'; then
echo $manifest | sed "s#$tmpdir/##" >> $tmpfile2

if [ -s "$tmpfile1" ]; then
echo Error: Puppet parse problem:
echo ----------------------------
cat $tmpfile1
echo ----------------------------


if [ -s "$tmpfile2" ]; then
echo Error: missing manifest documentation
echo see http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Manifest_Documentation
echo for more information.
echo Files with problems:
echo -------------------------------------
cat $tmpfile2
echo -------------------------------------


exit $rc


Andrew Leonard said...

This seems to blow up if you modify a manifest but not the files that it imports - the files that would otherwise be imported don't get copied to temporary directory, and "puppet parser validate" complains "No file(s) found for import". It also looks like ignoreimport is broken in 2.7.x (ignores everything), with a fix (maybe) pending.

Job Snijders said...

Hi Andrew,

Thanks for your comment. This script is far from optimal and basically broken with current puppet version. As soon as I have found something more decent that does what we want 'check before commit' I'll update my blog. If you have come across anything that really does what we want, please do share :-)

- Job

jowdjbrown said...

Wij zijn een informeel en laagdrempelig juridisch advieskantoor. Wij hebben als doel om informatie en juridische ondersteuning toegankelijk te maken voor werknemers die met een Sociaal Plan te maken krijgen. Wij doen dit binnen werkgeversbudgetten zonder kosten voor de werknemer.dutch lawyer

davidsmith said...

If you want to get success in the first attempts. 200-310 practice tests