官术网_书友最值得收藏!

Using commit hooks

It would be nice if we knew there was a syntax error in the manifest before we even committed it. You can have Puppet check the manifest using the puppet parser validate command:

# puppet parser validate/etc/puppet/manifests/site.pp 
err: Could not parse for environment production: Syntax error at end of file at /etc/puppet/manifests/site.pp:3

This is especially useful because a mistake anywhere in the manifest will stop Puppet from running on any node, even on nodes that don't use that particular part of the manifest. So checking in a bad manifest can cause Puppet to stop applying updates to production for some time, until the problem is discovered, and this could potentially have serious consequences.

The best way to avoid this is to automate the syntax check by using a pre-commit hook in your version control repo.

How to do it…

If you are using Git for version control, you can add a script, .git/hooks/pre-commit that syntax checks all files about to be committed. This example is taken from the Puppet Labs wiki:

#!/bin/sh

syntax_errors=0
error_msg=$(mktemp /tmp/error_msg.XXXXXX)

if git rev-parse --quiet --verify HEAD > /dev/null
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Get list of new/modified manifest and template files to check (in git index)
for indexfile in `git diff-index --diff-filter=AM --name-only --cached $against | egrep '\.(pp|erb)'`
do
    # Don't check empty files
    if [ `git cat-file -s :0:$indexfile` -gt 0 ]
    then
        case $indexfile in
            *.pp )
                # Check puppet manifest syntax
                git cat-file blob :0:$indexfile | puppet parser validate --ignoreimport > $error_msg ;;
            *.erb )
                # Check ERB template syntax
                git cat-file blob :0:$indexfile | erb -x -T - | ruby -c 2> $error_msg > /dev/null ;;
        esac
        if [ "$?" -ne 0 ]
        then
            echo -n "$indexfile: "
            cat $error_msg
            syntax_errors=`expr $syntax_errors + 1`
        fi
    fi
done

rm -f $error_msg

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

How it works…

The commit hook script will prevent you from committing any files with syntax errors:

# git commit -m "spot the deliberate mistake" manifests/site.pp
err: Could not parse for environment production: Syntax error at end of file; expected '}' at /etc/puppet/manifests/site.pp:3
manifests/site.pp: Error: 1 syntax errors found, aborting commit.

There's more…

You can find this script, and more details about it, on the Puppet Labs wiki: http://projects.puppetlabs.com/projects/1/wiki/Puppet_Version_Control

You can use a similar update hook to prevent broken manifests from being pushed to the Puppetmaster: see the wiki page for details.

See also

  • Using version control in this chapter
主站蜘蛛池模板: 虎林市| 改则县| 封丘县| 灌阳县| 木兰县| 攀枝花市| 车险| 马公市| 手机| 舒兰市| 阜南县| 木兰县| 武清区| 台山市| 稻城县| 江油市| 淅川县| 太保市| 静宁县| 祥云县| 通山县| 东乡县| 武清区| 郎溪县| 金坛市| 龙胜| 资兴市| 凌海市| 合江县| 龙江县| 广丰县| 边坝县| 普安县| 同江市| 苍南县| 上林县| 玛曲县| 佛冈县| 门源| 北票市| 福州市|