New developer environment (script)

Written by Ger Apeldoorn on. Posted in Geen categorie

If you want to add a new developer-environment, it can be quite a hassle.

Here is a script to help you set it up, make sure that the user is known in Gerrit and that the SSH key is set up. Also, the repos must be present of course!

Make sure you understand what this script does BEFORE you run it!

BASE=/etc/puppet/dev
HIERABASE=/etc/puppet/hieradata
GERRITSERVER=gerrit
PORT=29418
REPO=ssh://$GERRITSERVER:$PORT/Puppet-Main
HIERAREPO=ssh://$GERRITSERVER:$PORT/Puppet-Hiera

if [ $# -eq 0 ]
then
        echo "No arguments supplied. Specify the user for whom the dev-env will be created."
        exit 1
fi

if [ -d "$BASE/$1" ]
then
        echo "Local directory already exists!"
        exit 1
fi
if [ -d "$HIERABASE/$1" ]
then
        echo "Hiera Directory already exists!"
        exit 1
fi
if ! id -u $1 >/dev/null 2>&1
then
        echo "user does not exist"
        exit 1
fi

sudo -u $1 ssh -p $PORT $GERRITSERVER
if [ $? -ne 127 ]
then
        echo ======================================
        echo  You need to do the following BEFORE running this script:
        echo    - Add user $1 to htpasswd file
        echo      htpasswd /etc/httpd/htpasswd_gerrit $1
        echo    - setup ssh pubkey in gerrit
        echo ======================================
        exit 1
fi

mkdir -p "$BASE/$1"
chown "$1." "$BASE/$1"

sudo -u $1 git clone --branch integration "$REPO" "$BASE/$1"
cd "$BASE/$1"
sudo -u $1 r10k puppetfile install

for i in role profile companymodule
do
        cd "$BASE/$1/modules/$i"
        scp -p -P $PORT $GERRITSERVER:hooks/commit-msg .git/hooks/
        git flow init -fd
        cd -
done

sudo -u $1 scp -p -P 29418 $GERRITSERVER:hooks/commit-msg "$BASE/$1/.git/hooks/"

mkdir -p "$HIERABASE/$1"
chown "$1." "$HIERABASE/$1"
sudo -u $1 git clone --branch master "$HIERAREPO" "$HIERABASE/$1"

chown -R "$1." "$BASE/$1"
chown -R "$1." "$HIERABASE/$1"
echo ======================================
echo    All done, enjoy!
echo ======================================
echo    location:  $BASE/$1
echo    Hieradata: $HIERABASE/$1
echo ======================================

Checkrepos script

With all those modules as seperate git-repos, it can be difficult to keep track of the changes you’ve made.

This script provides you with an overview of your repos.

#!/bin/bash
# checkrepos.sh -- Check the status of the underlying repositories.
# Loads the repo names from the Puppetfile and uses code from gitstaus.sh from Alan K. Stebbens

# helper functions
count_lines() { echo "$1" | egrep -c "^$2" ; }
all_lines() { echo "$1" | grep -v "^$" | wc -l ; }

show_git_status() {
        # gitstatus.sh -- produce the current git repo status on STDOUT
        # Functionally equivalent to 'gitstatus.py', but written in bash (not python).
        #
        # Alan K. Stebbens  [http://github.com/aks]
        if [ -z "${__GIT_PROMPT_DIR}" ]; then
          SOURCE="${BASH_SOURCE[0]}"
          while [ -h "${SOURCE}" ]; do
            DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
            SOURCE="$(readlink "${SOURCE}")"
            [[ $SOURCE != /* ]] && SOURCE="${DIR}/${SOURCE}"
          done
          __GIT_PROMPT_DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
        fi

        gitsym=`git symbolic-ref HEAD`

        # if "fatal: Not a git repo .., then exit
        case "$gitsym" in fatal*) exit 0 ;; esac

        # the current branch is the tail end of the symbolic reference
        branch="${gitsym##refs/heads/}"    # get the basename after "refs/heads/"

        gitstatus=`git diff --name-status 2>&1`

        # if the diff is fatal, exit now
        case "$gitstatus" in fatal*) exit 0 ;; esac


        staged_files=`git diff --staged --name-status`

        num_changed=$(( `all_lines "$gitstatus"` - `count_lines "$gitstatus" U` ))
        num_conflicts=`count_lines "$staged_files" U`
        num_staged=$(( `all_lines "$staged_files"` - num_conflicts ))
        num_untracked=`git ls-files --others --exclude-standard $(git rev-parse --show-cdup) | wc -l`
        if [[ "$__GIT_PROMPT_IGNORE_STASH" = "1" ]]; then
          num_stashed=0
        else
          num_stashed=`git stash list | wc -l`
        fi

        clean=0
        if (( num_changed == 0 && num_staged == 0 && num_U == 0 && num_untracked == 0 && num_stashed == 0 )) ; then
          clean=1
        fi

        remote=

        if [[ -z "$branch" ]]; then
          tag=`git describe --exact-match`
          if [[ -n "$tag" ]]; then
            branch="$tag"
          else
            branch="_PREHASH_`git rev-parse --short HEAD`"
          fi
        else
          remote_name=`git config branch.${branch}.remote`

          if [[ -n "$remote_name" ]]; then
            merge_name=`git config branch.${branch}.merge`
          else
            remote_name='origin'
            merge_name="refs/heads/${branch}"
          fi

          if [[ "$remote_name" == '.' ]]; then
            remote_ref="$merge_name"
          else
            remote_ref="refs/remotes/$remote_name/${merge_name##refs/heads/}"
          fi

          # detect if the local branch have a remote tracking branch
          cmd_output=$(git rev-parse --abbrev-ref ${branch}@{upstream} 2>&1 >/dev/null)

          if [ `count_lines "$cmd_output" "fatal: No upstream"` == 1 ] ; then
            has_remote_tracking=0
          else
            has_remote_tracking=1
          fi

          # get the revision list, and count the leading "<" and ">"
          revgit=`git rev-list --left-right $${remote_ref}...HEAD`
          num_revs=`all_lines "$revgit"`
          num_ahead=`count_lines "$revgit" "^>"`
          num_behind=$(( num_revs - num_ahead ))
          if (( num_behind > 0 )) ; then
            remote="${remote}_BEHIND_${num_behind}"
          fi
          if (( num_ahead > 0 )) ; then
            remote="${remote}_AHEAD_${num_ahead}"
          fi
        fi

        if [[ -z "$remote" ]] ; then
          remote='.'
        fi

        if [[ "$has_remote_tracking" == "0" ]] ; then
          remote='_NO_REMOTE_TRACKING_'
        fi
        echo Branch: $branch / $remote
        git status -s
}
echo "=== MAIN Puppet repo ==="
git branch | grep '*' | awk -F' ' '{ print "Currently on branch", $2 }'
show_git_status
echo " "

for dir in $(grep '^mod' Puppetfile | awk -F'"' '{ print $2}')
do
        cd modules/$dir > /dev/null
        echo "=== Module $dir: modules/$dir ==="
        show_git_status
        cd - > /dev/null
        echo " "
done