Where I work(read as: play) we use a lot of git. As an operator we often have a service running with its configs in git. A common pattern we use is to have a post-receive hook on the git repository set up to update the git checkout on a remote server. We accomplish this through a post-receive hook that sshes into the remote server and calls a script called git-sync with some options. The git-sync script github project forked from the puppet-sync script project that we use specifically for puppet dynamic git environments. Hunner <3 More dynamic git environments with puppet. Finch <3.
A hook for a project goes in the hooks/post-receive file of the git server's bare repo. Lets look at one now:
Example git post-receive hook
#!/bin/sh ## File: akwardly incorrect PWD=`pwd` REPONAME=`basename $PWD | sed 's/.git$//'` REPO="git@gitserver.cat.pdx.edu:$REPONAME.git" DEPLOY="/etc/nagios3/conf.d/flat" SSH_ARGS="-i /shadow/home/git/.ssh/git@gitserver.cat.pdx.edu" GITSYNC="nagios@nagios.cat.pdx.edu" SYNC_COMMAND="/usr/local/bin/git-sync" while read oldrev newrev refname do BRANCH=`echo $refname | sed -n 's/^refs\/heads\///p'` if [ $BRANCH != "master" ]; then echo "Branch is not master, therefore not pushing to nagios" exit 0 fi [ "$newrev" -eq 0 ] 2> /dev/null && DELETE='--delete' || DELETE='' ssh $SSH_ARGS "$GITSYNC" "$SYNC_COMMAND" \ --branch "$BRANCH" \ --repository "$REPO" \ --deploy "$DEPLOY" \ $DELETE done ssh nagios@gitserver.cat.pdx.edu '/etc/init.d/nagios3 reload'
The hook will exit before doing anything if the branch is not
masterand if it is, will run the git-sync script remotely on the nagios host, then go back in to bounce the nagios service.
The git-sync script essentially performs a
git fectch; git checkout HEADIt doesn't worry itself with merging, and it is submodule aware.
A file, .git-sync-stamp, must be created by the administrator of the system. This is how git-sync knows it is in charge of managing the repository. It is definitely not recommended that you add this file to git. However, that should more or less work if you never want to think about it. I also wrote this puppet defined type to manage the stamp, initial vcsrepo, and public_key_file for you.
A puppet defined type to initalize gitsync managed folders
define gitsync::gitsync( $deploy, $repo, $user, $source, $public_key, $public_key_type ){ ssh_authorized_key { "${user}-${name}-gitsync": user => $user, ensure => $present, type => $public_key_type, } vcsrepo { $deploy: ensure => present, provider => git, user => $user, source => $source require => Ssh_authorized_key["${user}-${name}-gitsync"], } file { "${deploy}/.git-sync-stamp": ensure => present, owner => $user, mode => 0644, require => Vcsrepo[$deploy], }
The last thing to note is that I didn't write git-sync. I've modified it but it was mostly written by Reid Vandewielle and others. Marut <3
No comments:
Post a Comment