apply: fix typo
[puppet-git.git] / ppg-apply
blob6f94bea2f83e64cd942777111098e46450fe2c8c
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)
13 if [ -z "$GIT_DIR" ]; then
14 echo >&2 "Not in a git checkout"
15 exit 1
18 ## FIXME?: check whether these are overriden
19 PUPPETCONF=/etc/puppet/puppet.conf
20 REPORT_PATH=/var/lib/puppet/state/last_run_report.yaml
22 # puppet.conf syntax is compat with git config syntax
23 # (both are INI style). How lucky can we be? :-)
24 report_bool=$(git config -f $PUPPETCONF --bool --get agent.report)
26 # note: default is true
27 if [ "$report_bool" = 'false' ]; then
28 echo >&2 "ERROR: reports are disabled!"
29 exit 1
31 if [ -x /etc/puppet/local-puppet-apply ]; then
32 /etc/puppet/local-puppet-apply
33 pexit=$?
34 else
35 puppet apply --detailed-exitcodes $@
36 pexit=$?
39 # From http://docs.puppetlabs.com/man/apply.html
40 # 0 means ?? (guess: no changes)
41 # 2 means there were changes
42 # 4 means failures
43 # 6 means changes & failures
44 if [[ $pexit -eq 00 ]] || [[ $pexit -eq 2 ]]; then
45 # mark as good
46 lastgood_sha1=$(git rev-parse --revs-only refs/heads/ppg/lastgood)
47 if [ -n "$lastgood_sha1" ]; then
48 git push . HEAD refs/heads/ppg/lastgood
49 else
50 # setup with reflog - may be handy
51 # for diagnosis
52 git branch -l refs/heads/ppg/lastgood HEAD
54 else
55 ## TODO: do we want to warn in any way?
59 # TODO: handle reports
60 hostname=$(/bin/hostname -f)
61 worktree='/etc/puppet/.ppg/scratch-worktree'
62 mkdir -p ${worktree}/reports
64 export GIT_DIR=/etc/puppet/.ppg
65 pushd ${worktree} &>/dev/null
67 if ! git rev-parse --revs-only \
68 refs/heads/${hostname} &>/dev/null ; then
69 # it is going to be the first commit
70 # FIXME: any special handling?
73 cp ${REPORT_PATH} ${worktree}/reports/${hostname}.yaml
74 git update-index --add reports/${hostname}.yaml
75 git commit -m 'ppg apply report'
77 headname=$(git rev-parse --symbolic-full-name --revs-only HEAD)
78 if [[ "${headname}" != "refs/heads/${hostname}" ]]; then
79 # we are in thefirst run, auto-created 'master' branch
80 git branch ${hostname} HEAD
81 git checkout ${hostname}
82 git branch -D master
85 popd &>/dev/null
86 unset GIT_DIR
88 $PPG_EXEC_PATH/ppg-push-reports
90 popd &>/dev/null