-
-
Save hunner/3342358 to your computer and use it in GitHub Desktop.
post-receive hook for updating git repos on multiple puppet masters
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/sh | |
set -e | |
git-update-server-info | |
# | |
# Hook to update the /etc/puppetlabs/puppet with the lastest git changes | |
# | |
# To enable this hook, rename this file to "post-receive". | |
syncuser="puppetsync" | |
gituser="gitolite" | |
gitserver="git.domain.com" | |
gitrepo="puppet.git" | |
destination="/etc/puppetlabs/puppet/environments" | |
puppetmasters="puppet01.domain.com puppet02.domain.com" | |
## script config | |
NOBOLD="\033[0m" | |
BOLD="\033[1m" | |
BLACK="\033[30m" | |
GREY="\033[0m" | |
RED="\033[31m" | |
GREEN="\033[32m" | |
YELLOW="\033[33m" | |
BLUE="\033[34m" | |
MAGENTA="\033[35m" | |
CYAN="\033[36m" | |
WHITE="\033[37m" | |
## repo information | |
BRANCH_DIR=$destination | |
SSH_ARGS="-o ConnectTimeout=10 -o StrictHostKeyChecking=no" | |
## Functions | |
function update_puppet () { | |
## Git update for us-east puppetmaster | |
server=$1 | |
BRANCH=`echo $2 | sed -n 's/^refs\/heads\///p'` | |
REPO="${gituser}@${gitserver}:${gitrepo}" | |
echo | |
echo "INFO: updating puppet repo on $server" | |
echo | |
if [ "$newrev" -eq 0 ] 2> /dev/null ; then | |
# branch is being deleted | |
echo "Deleting remote branch $BRANCH_DIR/$BRANCH" | |
ssh $SSH_ARGS ${syncuser}@${server} /bin/sh <<-EOF | |
cd $BRANCH_DIR && rm -rf $BRANCH | |
EOF | |
else | |
# branch is being updated | |
echo "Updating remote branch $BRANCH_DIR/$BRANCH" | |
ssh $SSH_ARGS ${syncuser}@${server} /bin/sh <<-EOF | |
{ cd $BRANCH_DIR/$BRANCH 2> /dev/null && git pull origin $BRANCH && git submodule update ; } \ | |
|| { mkdir -p $BRANCH_DIR && cd $BRANCH_DIR \ | |
&& git clone $REPO $BRANCH && cd $BRANCH \ | |
&& git checkout -b $BRANCH origin/$BRANCH && git submodule update --init; } | |
EOF | |
fi | |
stat=$? | |
if [[ $stat != 0 ]] ; then | |
echo -e "${RED}ERROR: unable to update ${CYAN}${server}:${destination}" | |
echo -e "${RED}INFO:${CYAN}check the configuration and run the update on the server again" | |
exit $status | |
else | |
echo | |
echo "INFO: update of puppet repo on $server complete" | |
echo | |
fi | |
} | |
## Script | |
while read oldrev newrev refname; do | |
for host in $puppetmasters ; do | |
update_puppet $host $refname | |
done | |
done | |
exit $stat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment