From: Jenkins Date: Thu, 31 Jul 2014 15:45:36 +0000 (+0000) Subject: Added deploy-storage-image X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=dde42a6e403008b6399bfb4926e8392170ab5a39;p=profitbricks%2Fjenkins-build-scripts.git Added deploy-storage-image --- diff --git a/deploy-storage-image b/deploy-storage-image new file mode 100755 index 0000000..2125d6f --- /dev/null +++ b/deploy-storage-image @@ -0,0 +1,251 @@ +#!/bin/bash +# +# Script for deploying storage install images to all management servers +# +# Author: Frank Brehm /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 <&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