forked from drwahl/puppet-git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-receive
executable file
·165 lines (147 loc) · 5.62 KB
/
pre-receive
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
#!/bin/bash
# Puppet attempts to source ~/.puppet and will error if $HOME is not set
if [[ -z $HOME ]]
then
HOME="$(grep "${USER}:" /etc/passwd | awk -F ':' '{print $6}')"
export HOME
fi
failures=0
RC=0
hook_dir="$(dirname "$0")"
hook_symlink="$(readlink -f "$0")"
# Figure out where commit hooks are if pre-receive is setup as a symlink
if [[ ! -z "$hook_symlink" ]]; then
subhook_root="$(dirname "$hook_symlink")/commit_hooks"
else
subhook_root="$hook_dir/commit_hooks"
fi
tmptree=$(mktemp -d)
# Prevent tput from throwing an error by ensuring that $TERM is always set
if [[ -z "$TERM" ]]; then
TERM=dumb
fi
export TERM
# Decide if we want puppet-lint
# Decide if we want the puppet future parser (already on puppet 4?)
CHECK_PUPPET_LINT="enabled"
USE_PUPPET_FUTURE_PARSER="enabled"
CHECK_INITIAL_COMMIT="disabled"
if [[ -e ${subhook_root}/config.cfg ]] ; then
source "${subhook_root}/config.cfg"
fi
# Only puppet 3.2.1 - 3.8 support "--parser future" option.
case $(puppet --version) in
4*) USE_PUPPET_FUTURE_PARSER="disabled" ;;
esac
while read -r oldrev newrev refname; do
git archive "$newrev" | tar x -C "$tmptree"
# for a new branch oldrev is 0{40}, set oldrev to the commit where we branched off the parent
if [[ $oldrev == "0000000000000000000000000000000000000000" ]]; then
oldrev=$(git rev-list --boundary $newrev --not --all | sed -n 's/^-//p')
fi
files_list=''
if [[ "x$oldrev" == 'x' ]]; then
if [[ $CHECK_INITIAL_COMMIT != "disabled" ]] ; then
files_list=$(git ls-tree --full-tree -r HEAD --name-only)
else
echo "Skipping file checks this is the initial commit..."
fi
else
files_list=$(git diff --name-only "$oldrev" "$newrev" --diff-filter=ACM)
fi
for changedfile in $files_list; do
tmpmodule="$tmptree/$changedfile"
[[ -f "$tmpmodule" ]] || continue
#check puppet manifest syntax
if type puppet >/dev/null 2>&1; then
if [[ $(echo "$changedfile" | grep -q '\.*\.epp$'; echo $?) -eq 0 ]]; then
${subhook_root}/puppet_epp_syntax_check.sh "$tmpmodule" "${tmptree}/"
RC=$?
if [[ $RC -ne 0 ]]; then
failures=$((failures + 1))
fi
elif [[ $(echo "$changedfile" | grep -q '\.*\.pp$'; echo $?) -eq 0 ]]; then
${subhook_root}/puppet_manifest_syntax_check.sh "$tmpmodule" "${tmptree}/" "$USE_PUPPET_FUTURE_PARSER"
RC=$?
if [[ $RC -ne 0 ]]; then
failures=$((failures + 1))
fi
fi
else
echo "puppet not installed. Skipping puppet syntax checks..."
fi
if type ruby >/dev/null 2>&1; then
#check ruby syntax
if [[ $(echo "$changedfile" | grep -q '\.*\.rb$'; echo $?) -eq 0 ]]; then
${subhook_root}/ruby_syntax_check.sh "$tmpmodule" "${tmptree}/"
RC=$?
if [[ $RC -ne 0 ]]; then
failures=$((failures + 1))
fi
fi
#check erb (template file) syntax
if type erb >/dev/null 2>&1; then
if [[ $(echo "$changedfile" | grep -q '\.*\.erb$'; echo $?) -eq 0 ]]; then
${subhook_root}/erb_template_syntax_check.sh "$tmpmodule" "${tmptree}/"
RC=$?
if [[ $RC -ne 0 ]]; then
failures=$((failures + 1))
fi
fi
else
echo "erb not installed. Skipping erb template checks..."
fi
#check hiera data (yaml/yml) syntax
if [[ $(echo "$changedfile" | grep -q '\.*\.yaml$\|\.*\.yml$'; echo $?) -eq 0 ]]; then
${subhook_root}/yaml_syntax_check.sh "$tmpmodule" "${tmptree}/"
RC=$?
if [[ $RC -ne 0 ]]; then
failures=$((failures + 1))
fi
fi
#check hiera data (json) syntax
if [[ $(echo "$changedfile" | grep -q '\.*\.json$'; echo $?) -eq 0 ]]; then
${subhook_root}/json_syntax_check.sh "$tmpmodule" "${tmptree}/"
RC=$?
if [[ $RC -ne 0 ]]; then
failures=$((failures + 1))
fi
fi
else
echo "ruby not installed. Skipping erb/yaml/ruby checks..."
fi
#puppet manifest styleguide compliance
if [[ $CHECK_PUPPET_LINT != "disabled" ]] ; then
if type puppet-lint >/dev/null 2>&1; then
if [[ $(echo "$changedfile" | grep -q '\.*\.pp$' ; echo $?) -eq 0 ]]; then
${subhook_root}/puppet_lint_checks.sh "$CHECK_PUPPET_LINT" "$tmpmodule" "${tmptree}/"
RC=$?
if [[ $RC -ne 0 ]]; then
failures=$((failures + 1))
fi
fi
else
echo "puppet-lint not installed. Skipping puppet-lint tests..."
fi
fi
#r10k puppetfile syntax check
if hash r10k >/dev/null 2>&1; then
if [ "$changedfile" = "Puppetfile" ]; then
${subhook_root}/r10k_syntax_check.sh $tmptree/$changedfile
RC=$?
if [ "$RC" -ne 0 ]; then
failures=`expr $failures + 1`
fi
fi
else
echo "r10k not installed. Skipping r10k Puppetfile test..."
fi
done
done
rm -rf "$tmptree"
#summary
if [[ $failures -ne 0 ]]; then
echo -e "$(tput setaf 1)Error: $failures subhooks failed. Declining push.$(tput sgr0)"
exit 1
fi
exit 0