--- /dev/null
+#!/bin/bash
+#
+# Script for deploying storage install images to all management servers
+#
+# Author: Frank Brehm <frank.brehm@profitbricks.com
+#
+
+set -e
+set -u
+
+#########################################
+# base variables
+
+# The source directory on Sagunt
+SRC_DIR="/srv/gentoo/storage-images/production"
+
+# The target directory on the management servers
+# Note the trailing slash
+TARGET_DIR="/var/www/gentoo/installer/"
+
+#KEYFILE_GENTOO="/etc/profitbricks/rsync-gentoo.secret"
+MAX_IMAGES=4
+host="mgmt1.dc1.de.profitbricks.net"
+
+mgmt_servers="\
+ mgmt1.dc1.de.profitbricks.net
+ mgmt2.dc1.de.profitbricks.net
+ mgmt3.dc2.us.profitbricks.net
+ mgmt4.dc2.us.profitbricks.net
+ mgmt5.dc3.de.profitbricks.net
+ mgmt6.dc3.de.profitbricks.net
+"
+
+debug="n"
+verbose="n"
+quiet="n"
+
+safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
+match_lhs=""
+use_color="false"
+[[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
+[[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
+[[ -z ${match_lhs} ]] \
+ && type -P dircolors >/dev/null \
+ && match_lhs=$(dircolors --print-database)
+[[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true"
+
+# console colors:
+if [ "${use_color}" = "true" ] ; then
+ RED="\033[38;5;196m"
+ YELLOW="\033[38;5;226m"
+ GREEN="\033[38;5;46m"
+ BLUE="\033[38;5;27m"
+ NORMAL="\033[39m"
+else
+ RED=""
+ YELLOW=""
+ GREEN=""
+ BLUE=""
+ NORMAL=""
+fi
+
+MY_BASE=$(basename $0 )
+MY_DIR=$( readlink -f $( dirname $0 ) )
+
+# set locales to default values
+export LANG=
+export LANGUAGE=
+export LC_CTYPE="POSIX"
+export LC_NUMERIC="POSIX"
+export LC_TIME="POSIX"
+export LC_COLLATE="POSIX"
+export LC_MONETARY="POSIX"
+export LC_MESSAGES="POSIX"
+export LC_PAPER="POSIX"
+export LC_NAME="POSIX"
+export LC_ADDRESS="POSIX"
+export LC_TELEPHONE="POSIX"
+export LC_MEASUREMENT="POSIX"
+export LC_IDENTIFICATION="POSIX"
+export LC_ALL=
+
+#########################################
+# Some often used funktions
+
+debug() {
+ if [ "${verbose}" != "y" ] ; then
+ return
+ fi
+ echo -e " * [$(date +'%Y-%m-%d %H:%M:%S')]: $@" >&2
+}
+
+info() {
+ if [ "${quiet}" = "y" ] ; then
+ return
+ fi
+ echo -e " ${GREEN}*${NORMAL} [$(date +'%Y-%m-%d %H:%M:%S')]: $@" >&2
+}
+
+warn() {
+ echo -e " ${YELLOW}*${NORMAL} [$(date +'%Y-%m-%d %H:%M:%S')]: $@" >&2
+}
+
+error() {
+ echo -e " ${RED}*${NORMAL} [$(date +'%Y-%m-%d %H:%M:%S')]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+usage() {
+ cat <<EOF
+Usage: ${MY_BASE} [Options ...]
+ ${MY_BASE} [-h|--help]
+
+ Options:
+ -d|--debug debug output (sh -x)
+ -v|--verbose more verbose output
+ --nocolor don't use colors on display
+ -q|--quiet silent execution (excludes --verbose)
+ -h|--help show this output and exit
+EOF
+}
+
+#######################################
+# Reading command line stuff
+
+TEMP=$( getopt -o advqh \
+ --long andbd,debug,verbose,nocolor,quiet,help \
+ -n "${MY_BASE}" -- "$@" )
+if [ $? != 0 ] ; then
+ error "Terminating..." >&2
+ exit 1
+fi
+
+# Note the quotes around `$TEMP': they are essential!
+eval set -- "$TEMP"
+
+while true; do
+ case "$1" in
+ -d|--debug)
+ debug="y"
+ shift
+ ;;
+ -v|--verbose)
+ verbose="y"
+ shift
+ ;;
+ --nocolor)
+ RED=""
+ YELLOW=""
+ GREEN=""
+ BLUE=""
+ NORMAL=""
+ shift
+ ;;
+ -q|--quiet)
+ quiet="y"
+ shift
+ ;;
+ -h|--help)
+ usage
+ exit 0
+ ;;
+ --) shift
+ break
+ ;;
+ *) error "Invalid option '$1'."
+ usage >&2
+ exit 1
+ ;;
+ esac
+done
+
+if [ "${verbose}" = "y" -a "${quiet}" = "y" ]; then
+ error "Options --verbose and --quiet may not be used together."
+ echo >&2
+ usage >&2
+ exit 1
+fi
+
+if [ "${debug}" = "y" ] ; then
+ set -x
+fi
+
+###########################################################
+# Starting here ...
+info "Syncing Gentoo install images."
+
+LINK=${SRC_DIR}/storage.last.tar.xz
+if [[ ! -e "${LINK}" ]] ; then
+ error "Link or file '${LINK}' doesn't exists."
+ exit 5
+fi
+if [[ ! -L "${LINK}" ]] ; then
+ error "File '${LINK}' is not a symlink." >&2
+ exit 5
+fi
+
+LINK_TARGET=$(readlink "${LINK}")
+IMG=${SRC_DIR}/${LINK_TARGET}
+if [[ ! -f "${IMG}" ]] ; then
+ error "Image '${IMG}' doesn't exists."
+ exit 5
+fi
+
+echo
+echo "################################################################"
+info "Syncing to ${GREEN}${host}${NORMAL} ..."
+echo
+info "Syncing '${IMG}*' ..."
+debug "rsync -aHv --stats --rsync-path=\"sudo /usr/bin/rsync\" ${IMG}* ${host}:${TARGET_DIR}"
+rsync -aHv --stats --rsync-path="sudo /usr/bin/rsync" ${IMG}* ${host}:${TARGET_DIR}
+echo
+info "Syncing '${LINK}*' ..."
+debug "rsync -aHv --rsync-path=\"sudo /usr/bin/rsync\" ${LINK}* ${host}:${TARGET_DIR}"
+rsync -aHv --rsync-path="sudo /usr/bin/rsync" ${LINK}* ${host}:${TARGET_DIR}
+info " Finished ${host}."
+echo
+
+info "Checking for ${GREEN}max. ${MAX_IMAGES} install images${NORMAL} on ${host} ..."
+cur_images=$( ssh ${host} "ls -1 ${TARGET_DIR}storage.2*.xz | wc -l" )
+debug "Found current ${cur_images} install images."
+if [[ "${cur_images}" -gt "${MAX_IMAGES}" ]] ; then
+ diff=$(( ${cur_images} - ${MAX_IMAGES} ))
+ debug "Found ${diff} images to remove on ${host}."
+ for img in $( ssh ${host} "ls -1 ${TARGET_DIR}storage.2*.xz | head -n ${diff} " ) ; do
+ info "Removing ${img}* on ${host} ..."
+ ssh ${host} "sudo /bin/rm -v ${img}*"
+ done
+fi
+
+for thost in $mgmt_servers ; do
+
+ if [[ "${thost}" = "${host}" ]] ; then
+ continue
+ fi
+
+ echo
+ echo "################################################################"
+ info "Syncing to ${GREEN}${thost}${NORMAL} ..."
+ debug "ssh ${host} \"sudo /usr/bin/rsync -rlptgoDHvAX --delete --delete-after --exclude=old/ --password-file=/etc/profitbricks/rsync-gentoo.secret ${TARGET_DIR} gentoo@${thost}::gentoo-installer\""
+ ssh "${host}" "sudo /usr/bin/rsync -rlptgoDHvAX --delete --delete-after --exclude=old/ --password-file=/etc/profitbricks/rsync-gentoo.secret ${TARGET_DIR} gentoo@${thost}::gentoo-installer"
+ info "Finished ${thost} ..."
+
+done
+
+echo
+info "Finished deploying storage install images."
+
+exit 0
+
+# vim: ts=4 expandtab filetype=sh