-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppg-apply
executable file
·97 lines (81 loc) · 2.48 KB
/
ppg-apply
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
#!/bin/bash
if [[ -n "${PPG_DEBUG}" ]]; then
set -x
fi
pushd /etc/puppet >/dev/null || exit 1
PPG_EXEC_PATH=$( dirname $(readlink -f "${BASH_SOURCE[0]}" ) )
GIT_EXEC_PATH=$(git --exec-path)
GIT_DIR=$(git rev-parse --git-dir)
export GIT_SSH="${PPG_EXEC_PATH}/ppg-ssh"
. ${PPG_EXEC_PATH}/ppg-functions
if [ -z "$GIT_DIR" ]; then
echo >&2 "Not in a git checkout"
exit 1
fi
## FIXME?: check whether these are overriden
PUPPETCONF=/etc/puppet/puppet.conf
REPORT_PATH=/var/lib/puppet/state/last_run_report.yaml
# puppet.conf syntax is compat with git config syntax
# (both are INI style). How lucky can we be? :-)
report_bool=$(git config -f $PUPPETCONF --bool --get agent.report)
# note: default is true
if [ "$report_bool" = 'false' ]; then
echo >&2 "ERROR: reports are disabled!"
exit 1
fi
if [ -x /etc/puppet/local-puppet-apply ]; then
puppetcmd=/etc/puppet/local-puppet-apply
else
puppetcmd="puppet apply --detailed-exitcodes $@"
fi
if /bin/mountpoint -q /mnt/cluster ; then
# timeout at 50m, so the next hourly run has a go at it
# sleep ~30s between tries at the flock
# default 10 flocks, but will be overridden by /mnt/cluster/conf/lock/puppet
${PPG_EXEC_PATH}/flock_multi --timeout 50m --sleeptime 30 --queuemonitor puppet 10 \
$puppetcmd
pexit=$?
else
# some hosts don't have /mnt/cluster
# some need puppet to set it up first
$puppetcmd
pexit=$?
fi
# From http://docs.puppetlabs.com/man/apply.html
# 0 means ?? (guess: no changes)
# 2 means there were changes
# 4 means failures
# 6 means changes & failures
# Also
# 200 means flock_multi found an error
# 201 means flock_multi timed out waiting for the flock
if [[ $pexit -eq 00 ]] || [[ $pexit -eq 2 ]]; then
ppg_lastgood_cleanup
# mark as good
lastgood_sha1=$(git rev-parse --revs-only ppg/lastgood)
if [ -n "$lastgood_sha1" ]; then
git push --force . HEAD:ppg/lastgood
else
# setup with reflog - may be handy
# for diagnosis
git branch -l ppg/lastgood
fi
elif [[ $pexit -eq 200 ]]; then
echo flock_multi error!
exit $pexit
elif [[ $pexit -eq 201 ]]; then # soft error
echo flock_multi timed out
exit 0
else
## TODO: do we want to warn in any way?
:
fi
[ -n "${PPG_NOREPORT}" ] && exit 0
hostname=$(xhostname)
reportsdir='/etc/puppet/.ppg/reports'
mkdir -p ${reportsdir}/${hostname}
datestamp=$(date -u --rfc-3339=seconds|sed 's/ /_/')
cp ${REPORT_PATH} ${reportsdir}/${hostname}/${datestamp}.yaml
# NOTE: callers rely on getting an error through if push-reports
# fails
exec $PPG_EXEC_PATH/ppg-push-reports