Skip to content

Instantly share code, notes, and snippets.

@gaahrdner
Created November 16, 2012 15:29

Revisions

  1. gaahrdner revised this gist Dec 4, 2012. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions dynamic-puppet-environments.sh
    Original file line number Diff line number Diff line change
    @@ -38,8 +38,8 @@ function usage_and_exit {
    }

    [[ $1 ]] || usage_and_exit
    GIT=`command -v git` || die "Git not installed, so, yeah, this will never work."
    PWD=`pwd`
    GIT=$(command -v git) || die "Git not installed, so, yeah, this will never work."
    PWD=$(pwd)
    force=false
    delete=false
    production=false
    @@ -61,12 +61,12 @@ done
    [[ $path ]] || die "Path required, specify with -p"

    # validations
    git ls-remote $repo &> /dev/null || die "Not a valid git repository."
    $GIT ls-remote $repo &> /dev/null || die "Not a valid git repository."


    # 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`)
    declare -a Branch=($($GIT ls-remote -h $repo | cut -f2 | cut -d '/' -f 3))
    declare -a Existing=($(find "$path" -maxdepth 1 -mindepth 1 -type d -printf "%f\n"))
    log "We have ${#Branch[@]} branch(es) in our remote repository and ${#Existing[@]} branch(es) on our local disk"

    # what to do when we force
    @@ -86,7 +86,7 @@ if $production ; then
    else
    if $force ; then
    log "removing the production branch from our list of branches"
    Branch=(`echo ${Branch[@]/production/}`)
    Branch=($(echo ${Branch[@]/production/}))
    else
    die "A branch named production already exists, use -f to ignore this branch"
    fi
  2. gaahrdner revised this gist Dec 4, 2012. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions dynamic-puppet-environments.sh
    Original file line number Diff line number Diff line change
    @@ -100,8 +100,16 @@ if $delete ; then
    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"
    log "$repo does not contain the branch $e..."
    if [[ ! -d "$e/.git" ]] ; then
    log "and $e is not a git repository..."
    if $force ; then
    log "...but the force flag is set, deleting $e"
    rm -rf "$path/$e"
    else
    log "...but the force flag is NOT set, leaving directory $e alone"
    fi
    fi
    fi
    done
    else
  3. gaahrdner revised this gist Nov 16, 2012. No changes.
  4. gaahrdner revised this gist Nov 16, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion dynamic-puppet-environments.sh
    Original file line number Diff line number Diff line change
    @@ -81,7 +81,9 @@ fi
    # mapping a branch to production? we got that
    if $production ; then
    log "production flag set"
    if contains "production" "${Branch[@]}" ; then
    if contains "production" "${Branch[@]}" && [[ $branch = "production" ]] ; then
    log "production branch specified as 'production', ignoring"
    else
    if $force ; then
    log "removing the production branch from our list of branches"
    Branch=(`echo ${Branch[@]/production/}`)
  5. gaahrdner revised this gist Nov 16, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion dynamic-puppet-environments.sh
    Original file line number Diff line number Diff line change
    @@ -62,7 +62,6 @@ done

    # 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"


    # get all of our branches
    @@ -77,6 +76,8 @@ if $force ; then
    log "forcibly creating directory ${path}"
    fi

    [[ -d $path ]] || die "Specified directory does not exist, consider using -f to force creation"

    # mapping a branch to production? we got that
    if $production ; then
    log "production flag set"
  6. gaahrdner revised this gist Nov 16, 2012. 1 changed file with 40 additions and 9 deletions.
    49 changes: 40 additions & 9 deletions dynamic-puppet-environments.sh
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,8 @@ function usage_and_exit {
    -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"
    -d: delete branches that do not exist in the repository
    -b: an existing branch name you want mapped to the directory production"
    exit 2
    }

    @@ -41,14 +42,16 @@ GIT=`command -v git` || die "Git not installed, so, yeah, this will never work.
    PWD=`pwd`
    force=false
    delete=false
    production=false

    while getopts "r:p:fd" OPTION
    while getopts "r:p:fdb:" OPTION
    do
    case $OPTION in
    r) repo=$OPTARG;;
    p) path=$OPTARG;;
    f) force=true;;
    d) delete=true;;
    b) branch=$OPTARG ; production=true;;
    ?) usage_and_exit;;
    esac
    done
    @@ -61,20 +64,35 @@ done
    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"


    # 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"

    # what to do when we force
    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"
    # mapping a branch to production? we got that
    if $production ; then
    log "production flag set"
    if contains "production" "${Branch[@]}" ; then
    if $force ; then
    log "removing the production branch from our list of branches"
    Branch=(`echo ${Branch[@]/production/}`)
    else
    die "A branch named production already exists, use -f to ignore this branch"
    fi
    fi
    fi

    # 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."
    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"
    @@ -84,9 +102,10 @@ if $delete ; then
    fi
    done
    else
    log "Delete flag not set, local branch directories will remain on disk."
    log "Delete flag not set, local branch directories will remain on disk"
    fi

    # map each branch to a directory with the same name
    for b in "${Branch[@]}" ; do
    p="$path/$b"
    if [[ -d $p ]] ; then
    @@ -102,12 +121,24 @@ for b in "${Branch[@]}" ; do
    fi
    else
    # we should probably test if the existing git repo is the one we actually want
    log "pulling latest files from origin $b"
    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

    if [[ $production && $b = $branch ]] ; then
    if [[ ! -d "$path/production" ]] ; then
    log "cloning $b into $path/production"
    $GIT clone -q $repo -b $b "$path/production"
    else
    log "$path/production already exists..."
    log "...pulling latest files from origin $b"
    cd "$path/production" && $GIT reset --hard -q && $GIT pull -q origin $b && cd $PWD
    fi
    fi

    done
  7. gaahrdner created this gist Nov 16, 2012.
    113 changes: 113 additions & 0 deletions dynamic-puppet-environments.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    #! /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