Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add puppet pre-commit hooks

  • Loading branch information...
commit 6acef375b6283d6e9ea70aef22800d2c19517b81 1 parent c2060e1
@ssm authored
Showing with 170 additions and 0 deletions.
  1. +58 −0 puppet/git/pre-commit
  2. +112 −0 puppet/svn/pre-commit
View
58 puppet/git/pre-commit
@@ -0,0 +1,58 @@
+#!/bin/bash
+
+# pre-commit hook to syntax check puppet manifests and erb templates
+# at checkin.
+
+# Stig Sandbeck Mathisen <ssm@fnord.no>
+
+# Todo:
+# - Emit correct filename in .pp checks instead of /dev/fd/<num>
+# - Check for invalid characters with "iconv"
+
+errors=0
+message=$(mktemp /tmp/error_msg.XXXXXX)
+
+if git-rev-parse --verify HEAD >/dev/null 2>&1
+then
+ against=HEAD
+else
+ # Initial commit: diff against an empty tree object
+ against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
+fi
+
+## Check: whitespace
+git diff-index --check --cached $against -- > $message
+if [ "$?" -ne "0" ]; then
+ cat $message
+ errors=$(expr $errrors + 1)
+fi
+
+## Check: syntax
+for file in $(git diff-index --cached --diff-filter=AM --name-only --cached $against); do
+ # Do not check empty files
+ if [ $(git cat-file -s :0:"${file}") -gt 0 ]; then
+ case "$file" in
+ *.pp)
+ # TODO: rewrite /dev/fd/foo:line to ${file}:line
+ puppet parser validate --ignoreimport \
+ <(git cat-file blob :0:"${file}") > $message
+ ;;
+ *.erb)
+ git cat-file blob :0:"${file}" \
+ | erb -x -P -T - | ruby -c > $message
+ ;;
+ esac
+ if [ "$?" -ne "0" ]; then
+ echo "[${file}]"
+ cat $message
+ errors=$(expr $errrors + 1)
+ fi
+ fi
+done
+
+rm -rf $message
+
+if [ "$errors" -ne "0" ]; then
+ echo "Error: $errors errors found, aborting commit."
+ exit 1
+fi
View
112 puppet/svn/pre-commit
@@ -0,0 +1,112 @@
+#!/bin/bash
+
+# Stig Sandbeck Mathisen <ssm@fnord.no>
+
+set -u
+
+#------------------------------
+# Paranoia...
+export HOME=/
+export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
+
+#------------------------------
+# Command line arguments
+REPOSITORY="$1"
+TRANSACTION="$2"
+
+#------------------------------
+# Constants
+puppetversion=$(puppet --version)
+if [ -z "${puppetversion/0.*}" -o -z "${puppetversion/2.6.*}" ]; then
+ export CHECK_PP="puppet --confdir=/tmp --vardir=/tmp --parseonly --ignoreimport"
+else
+ export CHECK_PP="puppet parser validate"
+fi
+
+
+#------------------------------
+# Subroutines
+
+#------------------------------
+get_file() {
+ line="$1"
+
+ if [ -x /bin/mktemp ]; then
+ tempfile="$(/bin/mktemp /tmp/${line##*/}.XXXX)"
+ elif test -f /bin/tempfile; then
+ tempfile=$(/bin/tempfile)
+ else
+ tempfile="/tmp/puppet.$$"
+ set -C
+ fi
+
+ case $line in
+ *.pp)
+ # Transform "include c_acme::$hostname" into "include c_acme::hostname"
+ svnlook cat --transaction "$TRANSACTION" "$REPOSITORY" "$line" |
+ perl -pe 's/^(\s*include\s*.*::)\$/$1/' > $tempfile
+ ;;
+ *)
+ svnlook cat --transaction "$TRANSACTION" "$REPOSITORY" "$line" > $tempfile
+ ;;
+ esac
+
+ set +C
+
+ echo $tempfile
+
+}
+
+#------------------------------
+check_syntax_erb() {
+ file=$(get_file "$1")
+
+ erb -P -x -T '-' "$file" | ruby -c
+ status=$?
+
+ rm -f "$file"
+
+ if [ "$status" != "0" ]; then
+ report_syntax_error "$1"
+ fi
+}
+
+#------------------------------
+check_syntax_puppet() {
+ file=$(get_file "$1")
+
+ $CHECK_PP "$file" >&2
+
+ status=$?
+
+ rm -f "$file"
+
+ if [ "$status" != "0" ]; then
+ report_syntax_error "$1"
+ fi
+}
+
+#------------------------------
+report_syntax_error() {
+ echo >&2 "Error: Failed syntax check for $1"
+ exit 1
+}
+
+#------------------------------
+# Main loop
+
+svnlook changed --transaction "$TRANSACTION" "$REPOSITORY" | while read tx_act tx_file ; do
+ echo "Syntax check: $tx_act $tx_file"
+ case "$tx_act" in
+ A|U|UU)
+ case "$tx_file" in
+ *.pp)
+ check_syntax_puppet "$tx_file"
+ ;;
+ *.erb)
+ check_syntax_erb "$tx_file"
+ ;;
+ esac
+ ;;
+ esac
+done
Please sign in to comment.
Something went wrong with that request. Please try again.