Skip to content

Instantly share code, notes, and snippets.

@bwhaley
Created August 10, 2012 23:21
Show Gist options
  • Save bwhaley/3318910 to your computer and use it in GitHub Desktop.
Save bwhaley/3318910 to your computer and use it in GitHub Desktop.
post-receive hook for updating git repos on multiple puppet masters
#!/bin/sh
set -e
git-update-server-info
#
# Hook to update the /etc/puppet with the lastest git changes
#
# To enable this hook, rename this file to "post-receive".
read oldrev newrev refname
syncuser=puppetsync
destination=/etc/puppet/env
## 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=`echo $refname | sed -n 's/^refs\/heads\///p'`
BRANCH_DIR=$destination
SSH_ARGS="-i /redacted/path/to/id_rsa -o ConnectTimeout=10 -o StrictHostKeyChecking=no"
## Functions
function update_puppet () {
## Git update for us-east puppetmaster
server=$1
REPO="git@${2-git.REDACTED.com}:puppet.git"
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 && git pull origin $BRANCH ; } \
|| { mkdir -p $BRANCH_DIR && cd $BRANCH_DIR \
&& git clone $REPO $BRANCH && cd $BRANCH \
&& git checkout -b $BRANCH origin/$BRANCH ; }
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
update_puppet HOSTNAME_REDACTED
update_puppet HOSTNAME_REDACTED
update_puppet HOSTNAME_REDACTED
exit $stat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment