Created
April 25, 2013 21:29
-
-
Save hunner/5463326 to your computer and use it in GitHub Desktop.
Put this in `modules/gitolite_hooks/files/puppet/post-receive` and feed to the gitolite module.
This file contains hidden or 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
| # | |
| # Hook to update the /etc/puppetlabs/puppet with the lastest git changes. | |
| # Licensed Apache 2.0 | |
| # | |
| # To enable this hook, name this file "post-receive". | |
| syncuser="puppetsync" | |
| gituser="gitolite" | |
| gitserver="hostname.example.com" | |
| gitrepo="puppet.git" | |
| destination="/etc/puppetlabs/puppet/environments" | |
| puppetmasters="pm1.example.com pm2.example.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 fetch --all \ | |
| && git reset --hard 'origin/$BRANCH' \ | |
| && git submodule sync \ | |
| && git submodule update --init ; } \ | |
| || { mkdir -p $BRANCH_DIR \ | |
| && cd $BRANCH_DIR \ | |
| && git clone $REPO $BRANCH --branch $BRANCH \ | |
| && cd $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