Pre-commit hooks for your modules?

193 views
Skip to first unread message

Jakov Sosic

unread,
Dec 10, 2012, 5:27:26 PM12/10/12
to puppet...@googlegroups.com
Hi.

I was wondering what kind of precommit hooks are you guys using?

Here's what I've come up to in last hour:

$ cat .hg/hgrc | grep -A 1 hooks
[hooks]
pretxncommit.puppet = .hg/check_puppet.rb

$ cat .hg/check_puppet.rb

#!/usr/bin/ruby
def puppet_parser_validate(file)
if !system('puppet parser validate ' + file + ' > /dev/null 2>&1')
print('Syntax error in file: ' + file + "\n")
system('puppet parser validate ' + file)
exit(1)
end
end

def puppet_lint(file)
if !system('puppet-lint --no-80chars-check ' + file + ' > /dev/null
2>&1')
print('Coding style error in file: ' + file + "\n")
system('puppet-lint --no-80chars-check ' + file)
exit(1)
end
end

def puppet_erb_check(file)
if !system('erb -x -T \'-\' ' + file + ' | ruby -c > /dev/null 2>&1')
print('Syntax error in erb template: ' + file + "\n")
system('erb -x -T \'-\' ' + file + ' | ruby -c')
exit(1)
end
end

# go through list of files, and call adequate checks
IO.popen('hg status').readlines.each { |file|
file.sub!(/^\w (.*)\n/,'\1')
if file.match('.pp$')
puppet_parser_validate file
puppet_lint file
elsif file.match('.erb$')
puppet_erb_check file
end
}




These are very basic checks, but I would like to implement also
something like checking if file from 'source =>' is present in module's
files/ or if template from manifest is present in templates/ and things
like that.

Do you have any other ideas?




--
Jakov Sosic
www.srce.unizg.hr

llowder

unread,
Dec 10, 2012, 5:33:25 PM12/10/12
to puppet...@googlegroups.com


On Monday, December 10, 2012 4:27:26 PM UTC-6, Jakov Sosic wrote:
Hi.

I was wondering what kind of precommit hooks are you guys using?


Here is what I use:

#!/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 \
            --error-level all \
            --fail-on-warnings \
            --no-80chars-check \
            --no-class_parameter_defaults-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 --environment test $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

Corey Osman

unread,
Jun 3, 2013, 3:05:08 PM6/3/13
to puppet...@googlegroups.com
Here is what I have:

Essentially I use the command line tools instead of ruby functions.  

This tells me exactly where the problem is down to the line number.

+1 for checking the erb files, I'll have to add that to my script.

I have the json check for my hieradata since I keep hiera in JSON format.

Reply all
Reply to author
Forward
0 new messages