ppg-apply: use flock_multi to rate-limit puppet runs
[puppet-git.git] / ppg-reports-to-dashboard
blob1b91b2190650b8579d0ca40e9b9792f4e32e4435
1 #!/bin/bash
3 if [[ -n "${PPG_DEBUG}" ]]; then
4 set -x
5 fi
6 PPG_EXEC_PATH=$( dirname $(readlink -f "${BASH_SOURCE[0]}" ) )
8 export GIT_DIR=${1:-/var/lib/ppg/reports-import.git}
9 pushd ${GIT_DIR} >/dev/null
11 if [[ "$(git config --get --bool core.bare)" != 'true' ]]; then
12 echo >&2 "ERROR: must run on a bare repository"
13 exit 1
16 trackdir=/var/lib/ppg/reports-import.track
17 reportstmpdir=$(mktemp -d --tmpdir=/var/tmp ppg-reports-to-dashboard.XXXXXXX )
18 seenany=no
20 for head in $(git branch | /bin/grep -v '(no branch)' ); do
21 lastimportstate=
22 if [ -e ${trackdir}/${head} ]; then
23 lastimportstate=$(<${trackdir}/${head})
26 if [ -n "${lastimportstate}" ]; then
27 # we have a local head, walk deltas
28 logparams="${lastimportstate}..refs/heads/${head}"
29 else
30 # Initial import will take only two days
31 # of old reports
32 logparams=" --since=2.days.ago refs/heads/${head}"
34 # !? - the last line of git log has no \n so tac returns a
35 # first line that runs together the last two lines. workaround injecting
36 # a newline...
37 for commit in $( (git log --pretty=format:%H ${logparams} && echo '') | tac); do
38 seenany=yes
39 # the branchname is the hostname
40 fname=reports/${head}.yaml
41 epoch=$(git show -s --pretty=format:%ct ${commit})
42 # magic:
43 # - get the file without checking out the commit
44 # - store it in a tmpdir for later import with rake
45 git show ${commit}:${fname} > ${reportstmpdir}/${epoch}-$head.yaml
46 echo ${commit} > ${trackdir}/${head}
47 done
48 done
49 if [ "${seenany}" = 'yes' ]; then
50 pushd /usr/share/puppet-dashboard >/dev/null
51 rake RAILS_ENV=production reports:import REPORT_DIR=${reportstmpdir} | grep -v 'ETA:.*Importing:'
52 popd >/dev/null
55 # the rake/import process needs the file to stick around for a while
56 # and does not delete it when done, so this is fugly
57 find /var/tmp -type d -name 'ppg-reports-to-dashboard.*' -user puppet-dashboard -mmin +120 -print0 | \
58 xargs -0 --no-run-if-empty -L 100 rm -fr