Created
November 16, 2012 15:29
-
-
Save gaahrdner/4088204 to your computer and use it in GitHub Desktop.
bash script for a pull version of dynamic puppet environments, when you don't have post-receive hooks
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
#! /bin/bash | |
# This script takes two arguments: | |
# * the repository URL for your puppet modules | |
# * the destination directory to check out all the branches into | |
# | |
# It then checks out all possible branches so you can have dynamic environments | |
# | |
# It would probably work better if we just checkout master instead of multiple | |
# cloning operations, but we'll leave that for version 2 | |
# Conjunction function | |
function die { | |
log $* | |
exit 2 | |
} | |
function log { | |
echo "$(date) $*" | |
} | |
function contains { | |
local e | |
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done | |
return 1 | |
} | |
function usage_and_exit { | |
echo "Usage: dynamic-puppet-environments -r REPO_URL -p path [-f] [-d] | |
-r: the git url of the repository you would like to clone | |
-p: the path you want all branches to be checked out into | |
-f: forcibly overwrite and delete all the things | |
-d: delete branches that do not exist in the repository" | |
exit 2 | |
} | |
[[ $1 ]] || usage_and_exit | |
GIT=`command -v git` || die "Git not installed, so, yeah, this will never work." | |
PWD=`pwd` | |
force=false | |
delete=false | |
while getopts "r:p:fd" OPTION | |
do | |
case $OPTION in | |
r) repo=$OPTARG;; | |
p) path=$OPTARG;; | |
f) force=true;; | |
d) delete=true;; | |
?) usage_and_exit;; | |
esac | |
done | |
# mandatory arguments | |
[[ $repo ]] || die "Repository required, specify with -r" | |
[[ $path ]] || die "Path required, specify with -p" | |
# validations | |
git ls-remote $repo &> /dev/null || die "Not a valid git repository." | |
[[ -d $path ]] || die "Specified directory does not exist, consider using -f to force creation" | |
if $force ; then | |
log "force flag set" | |
mkdir -p $path | |
log "forcibly creating directory ${path}" | |
fi | |
# get all of our branches | |
declare -a Branch=(`git ls-remote -h $repo | cut -f2 | cut -d '/' -f 3`) | |
declare -a Existing=(`ls -1 $path`) | |
log "We have ${#Branch[@]} branch(es) in our remote repository and ${#Existing[@]} branch(es) on our local disk" | |
# get all the current branches in the specified path | |
if $delete ; then | |
log "Delete flag set, removing any branches which don't exist in the repository." | |
for e in "${Existing[@]}" ; do | |
if contains "$e" "${Branch[@]}" ; then | |
log "$repo has the branch $e, leaving it alone" | |
else | |
log "$repo does not have the branch $e, which currently exists in $path, deleting" | |
rm -rf "$path/$e" | |
fi | |
done | |
else | |
log "Delete flag not set, local branch directories will remain on disk." | |
fi | |
for b in "${Branch[@]}" ; do | |
p="$path/$b" | |
if [[ -d $p ]] ; then | |
log "$p already exists..." | |
if [[ ! -d "$p/.git" ]] ; then | |
log "...but is not a git repository..." | |
if $force ; then | |
log "...and force flag is set, deleting $p and cloning branch $b" | |
rm -rf $p | |
$GIT clone -q $repo -b $b $p | |
else | |
die "...bailing out since we don't know what's in $p" | |
fi | |
else | |
# we should probably test if the existing git repo is the one we actually want | |
log "pulling latest files from origin $b" | |
cd $p && $GIT reset --hard -q && $GIT pull -q origin $b && cd $PWD | |
fi | |
else | |
mkdir -p $p | |
log "Cloning branch $b from $repo into $p" | |
$GIT clone -q $repo -b $b $p | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment