Skip to content

Instantly share code, notes, and snippets.

@mech422
Created March 15, 2017 18:27
Show Gist options
  • Save mech422/ae15dab1260fe5289bd637f759129634 to your computer and use it in GitHub Desktop.
Save mech422/ae15dab1260fe5289bd637f759129634 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
dev_inv='./inventory_dev'
prod_inv='./inventory_prod'
playbook_dir="./playbooks"
site_playbook_name="site"
bootstrap_playbook_name="bootstrap"
infra_playbook_name="infrastructure"
stack_playbook_name="stack"
playbook_name='site'
playbook_action='install'
all_tags='repos downloads pre pkgs setup logging config reset monitoring post '
SHINDLE=0
verbose=''
SKIP_LIST=()
TAG_LIST=()
print_only=0
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY
function join_by { local IFS="$1"; shift; echo "$*"; }
function join { join_by ',' "$@" ; }
function split_by { local IFS="$1"; shift; local TMP=($1) ; for i in "${TMP[@]}" ; do echo -ne "$i
" ; done }
function split { split_by ',' "$@" ; }
#function contains() { [ -z "${2##*$1*}" ] && [ -z "$1" -o -n "$2" ]; }
function contains() { local N="$1"; shift; local H="$@" ; [ -z "${H##*$N*}" ] && [ -z "$N" -o -n "$
H" ]; }
function sublist {
local OP="$1"; shift ;
local NEEDLE="$1"; shift;
local STACK="$@"
local RSLT=""
for HAYSTACK in $STACK;
do contains $NEEDLE $HAYSTACK ;
contains $NEEDLE $HAYSTACK ;
if [ $? -ne $OP ] ; then
RSLT="$RSLT $HAYSTACK"
fi
done
echo "$(join_by ' ' $RSLT)"
}
function remove_tag { local NEEDLE="$1"; shift; sublist 0 "$NEEDLE" $@ ; }
function matching_tags { local NEEDLE="$1"; shift; sublist 1 "$NEEDLE" $@ ; }
function ansible {
local playbook="$1"
local verbose="$2"
local inv="$3"
local extra_vars="playbook_action=$4"
local skip_list="$5"
local skip="--skip-tags=$(join $skip_list)"
local tag_list="$6"
local tags="--tags=$(join $tag_list)"
#CMD="ansible-playbook $verbose --become --extra-vars "$extra_vars" -i $inv $skip $tags $playboo
k"
CMD="ansible-playbook $verbose --extra-vars "$extra_vars" -i $inv $skip $tags $playbook"
echo " "
echo "$CMD"
RC=0
if [[ $print_only -eq 0 ]] ; then
$CMD
RC=$?
fi
if [ $RC -ne 0 ] ; then
echo " "
echo " Ansible exited with error: $RC"
echo " "
exit $RC
fi
echo " "
return $RC
}
function repos {
local playbook="$1"
local verbose="$2"
local inv="$3"
local extra_vars="$4"
local skip_list="$5"
#local skip="$(join $skip_list)"
local skip="ALL"
local tag_list="$(matching_tags 'repos' $6)"
local tags="$(join $tag_list)"
contains 'repos' $6
RC=$?
if [ $RC -ne 0 ] ; then
echo " NO REPOS tags found in: $6"
echo " SKIPPING"
else
ansible "$playbook" "$verbose" "$inv" "$extra_vars" "$skip" "$tags"
fi
}
function downloads {
local playbook="$1"
local verbose="$2"
local inv="$3"
local extra_vars="$4"
local skip_list="$5"
#local skip="$(join $skip_list)"
local skip="ALL"
local tag_list="$(matching_tags 'downloads' $6)"
local tags="$(join $tag_list)"
contains 'downloads' $6
RC=$?
if [ $RC -ne 0 ] ; then
echo " NO DOWNLOADS tags found in: $6"
echo " SKIPPING"
else
ansible "$playbook" "$verbose" "$inv" "$extra_vars" "$skip" "$tags"
fi
}
usage() {
cat >&2 <<EOM
Usage $0 [ -b | --bootstrap ] [ -C | --clean ] [ -d | --dev ] [ -i | --infra ] [ -o | --stack ]
[ -P | --purge ] [ -p | --prod ] [ -S | --shindle ]
[ -v | --verbose ] [ -t tag,tag,...] playbook
-b | --bootstrap Only run bootstrap plays
-C | --clean Run the 'clean' tags to remove configs
-d | --dev Use dev inventory file
-i | --infra Only run infrastructure setup
-o | --stack Only run OpenStack setup
-P | --purge Run the 'purge' tags to totally remove packages
-p | --prod Use prod inventory file
-S | --shindle Use configs specific to shindle's lab
-t | --tags Tags to be enforced during playbook run ( ONLY these tags will be enforced!
)
-v | --verbose Pass '-vvv' to ansible.
(LOTS of logging spam)
EOM
exit 1
}
if [[ $# -eq 0 ]]; then
usage
fi
getopt --test > /dev/null
if [[ $? -ne 4 ]]; then
echo "I’m sorry, `getopt --test` failed in this environment."
exit 1
fi
LONG=bootstrap,clean,dev,infra,stack,purge,prod,shindle,tags:,verbose,help
SHORT=bCdioPpSt:v
# -temporarily store output to be able to check for errors
# -activate advanced mode getopt quoting e.g. via “--options”
# -pass arguments only via -- "$@" to separate them correctly
PARSED=`getopt --options $SHORT --longoptions $LONG --name "$0" -- "$@"`
if [[ $? -ne 0 ]]; then
# e.g. $? == 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# use eval with "$PARSED" to properly handle the quoting
eval set -- "$PARSED"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
"-b"|"--bootstrap")
playbook_name="$bootstrap_playbook_name"
shift
;;
"-C"|"--clean")
playbook_action='uninstall'
TAG_LIST="clean"
shift
;;
"-d"|"--dev")
inv="$dev_inv"
shift
;;
"-i"|"--infra")
playbook_name="$infra_playbook_name"
shift
;;
"-o"|"--stack")
playbook_name="$stack_playbook_name"
shift
;;
"-P"|"--purge")
playbook_action="uninstall"
TAG_LIST="purge"
shift
;;
"-p"|"--prod")
inv="$prod_inv"
shift
;;
"-S"|"--shindle")
echo " "
echo " WARNING: -S: ONLY use this option on shindle's lab gear!"
echo " "
SHINDLE=1
shift
;;
"-v"|"--verbose")
verbose="-vvv"
shift;
;;
"-t"|"--tags")
TAG_LIST="$(split $2)"
shift 2
;;
--)
shift
break
;;
*)
usage
;;
esac
done
# handle non-option arguments
if [[ $# -gt 1 ]]; then
echo "$0: A single playbook name is required"
exit 4
fi
if [[ $# -eq 1 ]]; then
playbook_name="${1}"
fi
playbook_name="${playbook_name%'.yml'}.yml"
playbook="${playbook_dir}/${playbook_name}"
#if [ ${#TAG_LIST[@]} -eq 0 ] ; then
# TAG_LIST="$all_tags"
#fi
if [ "X$TAG_LIST" == 'X' ] ; then
TAG_LIST="$all_tags"
fi
if [ $SHINDLE -eq 1 ] ; then
TAG_LIST+=( "shindle" )
#TAG_LIST="${TAG_LIST} shindle"
fi
#echo "Cleaning cachedir: ./cachedir"
#rm -rf ./cachedir/*
rm -rf ./playbooks/*.retry
rm -rf ./playbooks/packages/*.retry
SKIP_TAGS="ALL"
#echo " TAG_LIST: $TAG_LIST"
repos "$playbook" "$verbose" "$inv" "$playbook_action" "$SKIP_TAGS" "$TAG_LIST"
downloads "$playbook" "$verbose" "$inv" "$playbook_action" "$SKIP_TAGS" "$TAG_LIST"
TAG_LIST="$(remove_tag repos $TAG_LIST)"
TAG_LIST="$(remove_tag downloads $TAG_LIST)"
# echo " TAG_LIST( no repos/downloads): $TAG_LIST"
ansible "$playbook" "$verbose" "$inv" "$playbook_action" "$SKIP_TAGS" "$TAG_LIST"
echo " "
echo " "
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment