Files
puppet-git-hooks/pre-commit

97 lines
2.8 KiB
Bash
Executable File

#!/bin/sh
warned=0
final_ret=0
# Check for ruby binary
which ruby >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Ruby not found" 1>&2
exit 1
fi
# Check for Puppet binary
which puppet >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Puppet not found" 1>&2
exit 1
fi
# Check for puppet-lint
which puppet-lint >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "puppet-lint not found" 1>&2
exit 1
fi
# Check for erb
which erb >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "erb not found" 1>&2
exit 1
fi
styleguide_url='http://docs.puppetlabs.com/guides/style_guide.html'
if [ -f ".puppetstyle.url" ]; then
styleguide_url=$(cat ".puppetstyle.url")
fi
if git-rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
for FILE in $(git diff-index --name-only $against) ; do
echo "${FILE}"
case "${FILE}" in
*.pp )
puppet parser validate "${FILE}"
rc=$?
if [[ $rc != 0 ]]; then
echo -e "\e[0;31m'puppet parser validate' failed on ${FILE} - push denied. Run tests locally and confirm they pass before pushing. \e[0m"
final_ret=1
fi
puppet-lint --fail-on-warnings --with-filename "${FILE}"
rc=$?
if [[ $rc != 0 ]]; then
if [[ $warned = 0 ]]; then
echo -e "\e[0;33m" "Please follow the puppet module styleguide (${styleguide_url})." "\e[0m"
echo -e "\e[0;33m" "The above ERROR/WARNING messages are informational only. The commit has not been blocked for this reason. In the future, these may cause your commit to be refused." "\e[0m"
warned=1
fi
echo -e "\e[0;33m" "Style guide compliance errors in ${FILE}:" "\e[0m"
fi
;;
*.erb )
cat "${FILE}" | erb -x -T - | ruby -c 2>&1 >/dev/null
rc=$?
if [[ $rc != 0 ]]; then
echo -e "\e[0;31m" "Ruby syntax checker failed on template ${FILE}:" "\e[0m"
final_ret=1
fi
;;
*.yml|*.yaml)
# syntax YAML files, https://ttboj.wordpress.com/2013/08/25/finding-yaml-errors-in-puppet/
ruby -ryaml -e "YAML.parse(File.open('${FILE}'))"
if [[ $? -ne 0 ]]; then
echo -e "\e[0;31m" "YAML syntax error in ${FILE}" "\e[0m"
final_ret=1
fi
;;
*.json)
ruby -rrubygems -e "require 'json'; JSON.parse(File.open('${FILE}').read)"
if [[ $? -ne 0 ]]; then
echo -e "\e[0;31m" "JSON syntax error in ${FILE}" "\e[0m"
final_ret=1
fi
;;
esac
done
exit $final_ret