Created
April 5, 2017 18:36
-
-
Save jamesmcdonald/60a1b384392c91ea8c78e3fa4035ac2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# post-receive hook to trigger r10k over ssh on updates to puppet control repo | |
# Set SSHTARGET in ~/.config/puppet-update to eg: | |
# root@puppet.example.com | |
# The ssh target should have an authorized_key with eg: | |
# command="/usr/local/sbin/puppet-update.sh" | |
# That script need simply call: | |
# /path/to/r10k deploy environment $SSH_ORIGINAL_COMMAND | |
# On the puppet master, /etc/puppetlabs/r10k/r10k.yaml should look something like | |
# :cachedir: '/var/cache/r10k' | |
# | |
# :sources: | |
# :myrepo: | |
# remote: 'gitolite3@git.example.com:puppet' | |
# basedir: '/etc/puppetlabs/code/environments' | |
# Caveat: If you push a branch deletion and an updated Puppetfile in the same | |
# push command, the updates to the Puppetfile will not be deployed. You'll have | |
# to run manually or make another change to the Puppetfile. | |
CONFIGFILE=~/.config/puppet-update | |
if [ ! -r $CONFIGFILE ]; then | |
echo "$CONFIGFILE doesn't exist, not updating" >&2 | |
exit 1 | |
fi | |
source $CONFIGFILE | |
update() { | |
ssh $SSHTARGET -- --verbose info $@ | |
if [ $? -ne 0 ]; then | |
echo "WARNING: Update had errors: puppet may not be completely updated" >&2 | |
exit 1 | |
fi | |
} | |
while read oldref newref refname | |
do | |
refname=$(basename $refname) | |
updateargs= | |
if echo ${newref} | egrep -vq '[^0]' | |
then | |
echo "Branch $refname is being deleted, updating all to trigger cleanup" | |
update | |
break | |
fi | |
# If this isn't a new branch and the Puppetfile has been changed, add --puppetfile | |
# The new branch check is needed because diff won't work in that case; new | |
# environments get Puppetfile deployed by r10k automatically (could also use ||) | |
if echo ${oldref} | egrep -q '[^0]' && \ | |
git diff --name-only ${oldref}..${newref} | grep -q Puppetfile | |
then | |
updateargs="--puppetfile" | |
fi | |
update ${refname} ${updateargs} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment