-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathpre-commit
executable file
·251 lines (226 loc) · 7.7 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/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-commit
#
# Original:
# blog: http://techblog.roethof.net/puppet/a-puppet-git-pre-commit-hook-is-always-easy-to-have/
#
# Authors:
# Ronny Roethof
# Mattias Geniar <[email protected]>
# Rob Nelson <[email protected]>
# Jake Rogers <[email protected]>
# Geoff Davis <[email protected]>
# set colors
c_fail='1' # red
c_pass='2' # green
if [ -n "${TERM}" ] && [ "${TERM}" != "dumb" ]; then
function header() { tput bold; echo -e "\n${1}"; tput sgr0; }
function fail() { tput setaf $c_fail; echo -ne "${1}"; tput sgr0; }
function pass() { tput setaf $c_pass; echo -ne "${1}"; tput sgr0; }
else
function header() { echo -e "\n${1}"; }
function fail() { echo -ne "${1}"; }
function pass() { echo -ne "${1}"; }
fi
function use_bundle() {
path_to_bundle=$(command -v bundle)
if [[ -x "$path_to_bundle" ]] && [[ -f Gemfile ]]; then
if "${path_to_bundle}" check 2>&1 >/dev/null; then
return 0
else
fail "A bundler setup is present but incomplete. Run 'bundle install' or remove the Gemfile.\n"
exit 1
fi
else
return 1
fi
}
function use_pdk() {
if [[ $(/usr/bin/env uname -s) = 'Linux' ]] || [[ $(/usr/bin/env uname -s) = 'Darwin' ]]; then
path_to_pdk=$(command -v pdk)
elif which powershell.exe 2>&1 >/dev/null; then
path_to_pdk='powershell.exe -command pdk'
fi
if [[ -n "$path_to_pdk" ]]; then
pdk_version=$(${path_to_pdk} --version)
return 0
else
return 1
fi
}
function prefer_pdk() {
# By default, prefer the PDK over a bundler setup
# If a user sets a file or envvar, prefer the bundler setup
prefer_pdk=0
if [[ -e ~/.prefer_bundler ]] || [[ -e ./.prefer_bundler ]] || [[ -n "${PREFER_BUNDLER}" ]]; then
prefer_pdk=1
fi
return $prefer_pdk
}
function setup_paths() {
# Make sure the necessary tools are installed. If they aren't, just die and
# stop the commit. Force the use of these tools before a commit is allowed.
# Prefer the PDK by default
if prefer_pdk && use_pdk; then
path_to_puppet="${path_to_pdk} bundle exec puppet"
path_to_puppet_lint="${path_to_pdk} bundle exec puppet-lint"
path_to_erb="${path_to_pdk} bundle exec erb"
path_to_ruby="${path_to_pdk} bundle exec ruby"
# Second preference is for a bundler setup
elif use_bundle; then
path_to_puppet="${path_to_bundle} exec puppet"
path_to_puppet_lint="${path_to_bundle} exec puppet-lint"
path_to_erb="${path_to_bundle} exec erb"
path_to_ruby="${path_to_bundle} exec ruby"
# If neither the PDK or bundler are available, rely on actual binaries
else
path_to_puppet=$(command -v puppet)
if ! [[ -x "$path_to_puppet" ]]; then
echo "The puppet binary wasn't found. Sorry, I won't allow you to commit without puppet installed."
fail "Please install puppet and try again.\n"
exit 1
fi
path_to_puppet_lint=$(command -v puppet-lint)
if ! [[ -x "$path_to_puppet_lint" ]]; then
echo "The puppet-lint binary wasn't found. Sorry, I won't allow you to commit without puppet-lint installed."
fail "Please install puppet-lint and try again.\n"
exit 1
fi
path_to_erb=$(command -v erb)
if ! [[ -x "$path_to_erb" ]]; then
echo "The erb binary wasn't found. Sorry, I won't allow you to commit without erb installed."
fail "Please install erb (Ruby Templating) and try again.\n"
exit 1
fi
path_to_ruby=$(command -v ruby)
if ! [[ -x "$path_to_ruby" ]]; then
echo "The ruby binary wasn't found. Sorry, I won't allow you to commit without ruby installed."
fail "Please install ruby and try again.\n"
exit 1
fi
fi
}
function checkyaml() {
if [ -n "$path_to_pdk" ] && { [ $(cut -d. -f1 <<<$pdk_version) -gt 1 ] || [ $(cut -d. -f2 <<<$pdk_version) -ge 9 ]; }; then
$path_to_pdk validate yaml "$1"
else
$path_to_ruby -e "require 'yaml'; YAML.load_file('$1')"
fi
}
setup_paths
if [[ $(git diff --name-only --cached | grep -E '\.(pp)') ]]; then
header "*** Checking puppet code for style ***"
# 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 that end in *.pp extension
if [[ -f $file && $file == *.pp ]]; then
# allow user-defined over-ride of linting rules with .puppet-lint.rc
if [[ -f "$(git rev-parse --show-toplevel)/.puppet-lint.rc" ]]; then
$path_to_puppet_lint --config "$(git rev-parse --show-toplevel)/.puppet-lint.rc" "$file"
else
$path_to_puppet_lint --with-filename "$file"
fi
# Set us up to bail if we receive any syntax errors
if [[ $? -ne 0 ]]; then
fail "FAILED: "; echo "$file"
syntax_is_bad=1
else
pass "PASSED: "; echo "$file"
fi
fi
done
header "*** Checking puppet manifest syntax ***"
# 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 && $file == *.pp ]]; then
$path_to_puppet parser validate $file
if [[ $? -ne 0 ]]; then
fail "FAILED: "; echo "$file"
syntax_is_bad=1
else
pass "PASSED: "; echo "$file"
fi
fi
done
fi
if [[ ! -z $(git diff --name-only --cached | grep -E manifests/site.pp) ]]; then
echo "*** Checking if the catalog compiles ***"
$path_to_puppet apply --noop manifests/site.pp
if [[ $? -ne 0 ]]; then
fail "FAILED: "; echo "catalog compilation"
syntax_is_bad=1
else
pass "PASSED: "; echo "catalog compilation"
fi
fi
if [[ $(git diff --name-only --cached | grep -E '\.(epp)') ]]; then
header "*** Checking puppet template(epp) syntax ***"
for file in $(git diff --name-only --cached | grep -E '\.(epp)'); do
if [[ -f $file && $file == *.epp ]]; then
$path_to_puppet epp validate $file
if [[ $? -ne 0 ]]; then
fail "FAILED: "; echo "$file"
syntax_is_bad=1
else
pass "PASSED: "; echo "$file"
fi
fi
done
fi
if [[ $(git diff --name-only --cached | grep -E '\.(erb$)') ]]; then
header "*** Checking ruby template(erb) syntax ***"
for file in $(git diff --name-only --cached | grep -E '\.(erb$)'); do
if [[ -f $file ]]; then
$path_to_erb -P -x -T '-' $file | $path_to_ruby -c | grep -v '^Syntax OK'
if [[ "${PIPESTATUS[1]}" -ne 0 ]]; then
fail "FAILED: "; echo "$file"
syntax_is_bad=1
else
pass "PASSED: "; echo "$file"
fi
fi
done
fi
if [[ $(git diff --name-only --cached | grep -E '\.(rb$)') ]]; then
header "*** Checking ruby syntax ***"
for file in $(git diff --name-only --cached | grep -E '\.(rb$)'); do
if [[ -f $file ]]; then
$path_to_ruby -c $file | grep -v '^Syntax OK'
if [[ "${PIPESTATUS[0]}" -ne 0 ]]; then
fail "FAILED: "; echo "$file"
syntax_is_bad=1
else
pass "PASSED: "; echo "$file"
fi
fi
done
fi
if [[ $(git diff --name-only --cached | grep -E '\.(yaml$|yml$)') ]]; then
header "*** Checking YAML syntax ***"
for file in $(git diff --name-only --cached | grep -E '\.(yaml$|yml$)'); do
if [[ -f $file ]]; then
checkyaml $file
if [[ $? -ne 0 ]]; then
fail "FAILED: "; echo "$file"
syntax_is_bad=1
else
pass "PASSED: "; echo "$file"
fi
fi
done
fi
if [[ $syntax_is_bad -eq 1 ]]; then
fail "\nErrors Found, Commit REJECTED\n"
exit 1
else
pass "\nNo Errors Found, Commit ACCEPTED\n"
fi