]> git.donarmstrong.com Git - dsa-puppet.git/blob - tools/git-hooks/pre-commit
add memcache to open ports
[dsa-puppet.git] / tools / git-hooks / pre-commit
1 #!/bin/bash
2
3 if ! cmp -s ${PWD}/.git/hooks/pre-commit ${PWD}/tools/git-hooks/pre-commit ; then
4         rm -f ${PWD}/.git/hooks/pre-commit
5         ln -sf ../../tools/git-hooks/pre-commit ${PWD}/.git/hooks/pre-commit
6         exec ${PWD}/.git/hooks/pre-commit
7 fi
8
9 if git rev-parse --verify HEAD &>/dev/null; then
10         against=HEAD
11 else
12         # Initial commit: diff against an empty tree object
13         against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
14 fi
15
16 check_puppet_manifest () {
17         local file=$1
18
19         local pp=${workdir}/${file}
20         mkdir -p $(dirname ${pp})
21         git cat-file blob :0:${file} | sed 's/^import .*/#&/' >${pp}
22         trap "rm -f ${pp}" RETURN
23
24         local output=$(puppet parser validate ${pp} 2>&1)
25         if [ $? -ne 0 ] || [ -n "${output}" ]; then
26                 echo '** Syntax check failed:' >&2
27                 echo "${output}" >&2
28                 return 1
29         fi
30
31         # Check manifests for exactly one class or define per file.
32         case "${file}" in
33                 modules/*/manifests/*)
34                         if [[ "$(egrep -c '^(class|define) ' ${file})" != "1" ]]; then
35                                 echo "** Multiple class/defines:" >&2
36                                 egrep -n '^(class|define) ' ${file} >&2
37                                 return 1
38                         fi
39                         ;;
40         esac
41
42         case "${file}" in
43                 manifests/site.pp|modules/*/manifests/*) ;;
44                 *) return
45         esac
46
47         if [ -x /usr/bin/pcregrep ]; then
48                 local wsfail=$({
49                         # grep -nPT would be nice. grep -P seems broken on gudeploy01.
50                         # Patterns checked for are (in this order):
51                         # - leading SPACE
52                         # - SPACE followed by TAB
53                         # - TAB followed by SPACE
54                         # - non-leading TAB (but allow around '#' comment leader)
55                         # - trailing whitespace
56                         pcregrep -n -e '^ ' -e ' \t' -e '\t ' -e '[^\t#]\t+(?!#)' -e '\s+$' ${pp}
57                 })
58                 if [[ $(wc -l <${pp}) > $(cat -s ${pp}|wc -l) ]]; then
59                         wsfail+="Excess blank line(s). 'cat -s' is your friend."
60                 fi
61                 if [[ -n "${wsfail}" ]]; then
62                         echo '** Bad whitespace (see Manifest Standards):' >&2
63                         echo "${wsfail}" >&2
64                         return 1
65                 fi
66         else
67                 echo "Missing pcregrep: apt-get install pcregrep" >&2
68         fi
69
70         case "${file}" in
71                 manifests/site.pp|modules/*/manifests/*)
72                         if [[ -n "$(which puppet-lint)" ]]; then
73                                 puppet-lint --no-hard_tabs-check --no-2sp_soft_tabs-check --no-80chars-check ${pp} >&2 | uniq
74                         else
75                                 echo "Please install puppet-lint (gem install puppet-lint)" >&2
76                         fi
77                         ;;
78                 *)
79                         return
80                         ;;
81         esac
82
83         return 0
84 }
85
86 # Check a Puppet template.
87 #
88 check_puppet_template () {
89         local file=$1
90
91         # Beware of 'Syntax OK' message on success.
92         local output=$((git cat-file blob :0:${file}|erb -x -P -T -|ruby -c >/dev/null) 2>&1)
93         if [[ $? -ne 0 || -n "${output}" ]]; then
94                 echo '** Syntax check failed:' >&2
95                 echo "${output}" >&2
96                 return 1
97         fi
98
99         return 0
100 }
101
102 readonly failed=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
103 readonly missing=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
104 readonly parse=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
105 readonly retest=$(mktemp ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
106 readonly workdir=$(mktemp -d ${TMPDIR:-/tmp}/git.puppet.pre-commit.XXXXXXXXXX)
107 trap "rm -f ${failed} ${missing} ${parse} ${retest}; rm -rf ${workdir}" EXIT
108
109 error=0
110 warning=0
111 for file in $(git diff-index --name-only --diff-filter=AM --cached ${against}); do
112         output=""
113         rc=0
114
115         case "${file}" in
116                 manifests/site.pp)
117                         output=$(check_puppet_manifest ${file} 2>&1); rc=$?;;
118                 modules/*)
119
120                         case "${file}" in
121                                 modules/*/manifests/*)
122                                         case "${file}" in
123                                                 # FIXME: doing this so the whitespace check happens
124                                                 *.pp) output=$(check_puppet_manifest ${file} 2>&1); rc=$?;;
125                                         esac
126                                         ;;
127                                 modules/*/templates/*)
128                                         output=$(check_puppet_template ${file} 2>&1); rc=$?;;
129                         esac
130                         ;;
131
132         esac
133
134         if [[ ${rc} -ne 0 || -n "${output}" ]]; then
135                 echo "** ${file}" >&2
136                 echo "${output}" >&2
137                 if [[ ${file} != *.pp ]]; then
138                         error=1
139                 # puppet-lint warnings are acceptable.
140                 elif [[ -n "$(egrep -v '^WARNING: ' <<<${output})" ]]; then
141                         error=1
142                 else
143                         warning=1
144                 fi
145         fi
146 done
147
148 if [ "$error" -ne 0 ]; then
149         echo "** Please correct the above errors." >&2
150         exit 1
151 fi
152
153 if [ "$warning" -ne 0 ]; then
154         sleep 5
155 fi
156
157 exit 0