Force a non-lazy run if global.ini has changed
[puppet-git.git] / ppg-apply
blobee16adbd55878d84263c43762440c0dd52751ac8
1 #!/bin/bash
3 if [[ -n "${PPG_DEBUG}" ]]; then
4 set -x
5 fi
7 pushd /etc/puppet >/dev/null || exit 1
9 PPG_EXEC_PATH=$( dirname $(readlink -f "${BASH_SOURCE[0]}" ) )
10 GIT_EXEC_PATH=$(git --exec-path)
11 GIT_DIR=$(git rev-parse --git-dir)
12 export GIT_SSH="${PPG_EXEC_PATH}/ppg-ssh"
14 . ${PPG_EXEC_PATH}/ppg-functions
16 if [ -z "$GIT_DIR" ]; then
17 echo >&2 "Not in a git checkout"
18 exit 1
21 ## FIXME?: check whether these are overriden
22 PUPPETCONF=/etc/puppet/puppet.conf
23 REPORT_PATH=/var/lib/puppet/state/last_run_report.yaml
25 # puppet.conf syntax is compat with git config syntax
26 # (both are INI style). How lucky can we be? :-)
27 report_bool=$(git config -f $PUPPETCONF --bool --get agent.report)
29 # note: default is true
30 if [ "$report_bool" = 'false' ]; then
31 echo >&2 "ERROR: reports are disabled!"
32 exit 1
35 if [ -x /etc/puppet/local-puppet-apply ]; then
36 puppetcmd=/etc/puppet/local-puppet-apply
37 else
38 puppetcmd="puppet apply --detailed-exitcodes $@"
41 if /bin/mountpoint -q /mnt/cluster ; then
42 # timeout at 50m, so the next hourly run has a go at it
43 # sleep ~30s between tries at the flock
44 # default 10 flocks, but will be overridden by /mnt/cluster/conf/lock/puppet
45 ${PPG_EXEC_PATH}/flock_multi --timeout 50m --sleeptime 30 --queuemonitor puppet 10 \
46 $puppetcmd
47 pexit=$?
48 else
49 # some hosts don't have /mnt/cluster
50 # some need puppet to set it up first
51 $puppetcmd
52 pexit=$?
55 # From http://docs.puppetlabs.com/man/apply.html
56 # 0 means ?? (guess: no changes)
57 # 2 means there were changes
58 # 4 means failures
59 # 6 means changes & failures
60 # Also
61 # 200 means flock_multi found an error
62 # 201 means flock_multi timed out waiting for the flock
64 if [[ $pexit -eq 00 ]] || [[ $pexit -eq 2 ]]; then
65 ppg_lastgood_cleanup
66 # mark as good
67 lastgood_sha1=$(git rev-parse --revs-only ppg/lastgood)
68 if [ -n "$lastgood_sha1" ]; then
69 git push --force . HEAD:ppg/lastgood
70 else
71 # setup with reflog - may be handy
72 # for diagnosis
73 git branch -l ppg/lastgood
75 elif [[ $pexit -eq 200 ]]; then
76 echo flock_multi error!
77 exit $pexit
78 elif [[ $pexit -eq 201 ]]; then # soft error
79 echo flock_multi timed out
80 exit 0
81 else
82 ## TODO: do we want to warn in any way?
86 hostname=$(xhostname)
87 reportsdir='/etc/puppet/.ppg/reports'
88 mkdir -p ${reportsdir}/${hostname}
90 datestamp=$(date -u --rfc-3339=seconds|sed 's/ /_/')
91 cp ${REPORT_PATH} ${reportsdir}/${hostname}/${datestamp}.yaml
93 # NOTE: callers rely on getting an error through if push-reports
94 # fails
95 exec $PPG_EXEC_PATH/ppg-push-reports