Skip to content

Instantly share code, notes, and snippets.

@fluxrad
Created April 11, 2012 19:04
Show Gist options
  • Save fluxrad/2361452 to your computer and use it in GitHub Desktop.
Save fluxrad/2361452 to your computer and use it in GitHub Desktop.
A pre-commit git hook to validate puppet syntax
#!/bin/bash
# pre-commit git hook to check the validity of a puppet manifest
#
# Prerequisites:
# gem install puppet-lint puppet
#
# Install:
# /path/to/repo/.git/hooks/pre-comit
# Source RVM if needed
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
echo "### Checking puppet syntax, for science! ###"
# for file in `git diff --name-only --cached | grep -E '\.(pp|erb)'`
for file in `git diff --name-only --cached | grep -E '\.(pp)'`
do
# Only check new/modified files
if [[ -f $file ]]
then
puppet-lint \
--no-80chars-check \
--no-autoloader_layout-check \
--no-nested_classes_or_defines-check \
--no-only_variable_string-check \
--no-2sp_soft_tabs-check \
--with-filename $file
# Set us up to bail if we receive any syntax errors
if [[ $? -ne 0 ]]
then
syntax_is_bad=1
else
echo "$file looks good"
fi
fi
done
echo ""
echo "### Checking if puppet manifests are valid ###"
# validating the whole manifest takes too long. uncomment this
# if you want to test the whole shebang.
# for file in `find . -name "*.pp"`
# for file in `git diff --name-only --cached | grep -E '\.(pp|erb)'`
for file in `git diff --name-only --cached | grep -E '\.(pp)'`
do
if [[ -f $file ]]
then
puppet parser validate --mode user $file
if [[ $? -ne 0 ]]
then
echo "ERROR: puppet parser failed at: $file"
syntax_is_bad=1
else
echo "OK: $file looks valid"
fi
fi
done
echo ""
if [[ $syntax_is_bad -eq 1 ]]
then
echo "FATAL: Syntax is bad. See above errors"
echo "Bailing"
exit 1
else
echo "Everything looks good."
fi
@dhoppe
Copy link

dhoppe commented Apr 14, 2012

Here another solution. ;)

!/bin/bash

error_counter=0
error_message=$(mktemp /tmp/error_mesage.XXXXXX)

for file in git diff --cached --name-only | egrep '\.(pp|erb)'
do
if [ git cat-file -s :0:$file -gt 0 ]
then
case $file in
*.pp)
# Check manifest syntax
puppet parser validate $file > $error_message

            if [ $? -ne 0 ]
            then
                error_counter=`expr $error_counter + 1`
            fi

            # Check manifeststyle
            puppet-lint \
                --fail-on-warnings \
                --no-80chars-check \
                --no-autoloader_layout-check \
                --no-hard_tabs-check \
                --no-nested_classes_or_defines-check \
                --no-only_variable_string-check \
                $file >> $error_message

            if [ $? -ne 0 ]
            then
                error_counter=`expr $error_counter + 1`
            fi ;;
        *.erb)
            # Check template syntax
            git cat-file blob :0:$file | erb -P -x -T - | ruby -c 2> $error_message > /dev/null

            if [ $? -ne 0 ]
            then
                error_counter=`expr $error_counter + 1`
            fi ;;
    esac
    if [ $error_counter -ne 0 ]
    then
        echo "$file:"
        cat $error_message
    fi
fi

done

rm -f $error_message

if [ $error_counter -ne 0 ]
then
echo ""
echo "Error: $error_counter errors found, aborting commit."
exit 1
fi

vim: tabstop=3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment