From f51f20789044b32069f16491d4999ebd5265145d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Fri, 12 Jun 2020 12:34:23 +0200 Subject: [PATCH] New script to check maco's upgrade --- cluster/maco.check.update.sh | 154 +++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100755 cluster/maco.check.update.sh diff --git a/cluster/maco.check.update.sh b/cluster/maco.check.update.sh new file mode 100755 index 0000000..dbfd3b5 --- /dev/null +++ b/cluster/maco.check.update.sh @@ -0,0 +1,154 @@ +#!/bin/sh + +# This script will check if Maco require an upgrade and +# will prepare the host in order to apply upgrade : +# 1. Disable SGE queue + +# This script can be call by a cronjob (eg. weekly) +# Another script should try to apply upgrades also with cron (eg. hourly) + +# Vars {{{ +readonly PROGNAME=$(basename "${0}") +readonly PROGDIR=$(readlink -m $(dirname "${0}")) +readonly ARGS="${*}" +readonly NBARGS="${#}" +[ -z "${DEBUG}" ] && readonly DEBUG=0 +## Export DEBUG for sub-script +export DEBUG + +## Colors +readonly PURPLE='\033[1;35m' +readonly RED='\033[0;31m' +readonly RESET='\033[0m' +readonly COLOR_DEBUG="${PURPLE}" + +## Maco +readonly MACO_LOCAL_DIR="/opt/maco" +readonly MACO_INSTALL_DIR="/mnt/store.ipr/InstallProgs/ipr/maco" +# }}} + +manage_args() { # {{{ + + case "${NBARGS}" in + 0 ) ## Nothing to do + ;; + * ) + printf '%b\n' "${RED}Don't expect any arguments.${RESET}" + printf '%b\n' "---" + usage + exit 1 + ;; + esac + +} +# }}} +usage() { # {{{ + + cat <<- EOF +usage: $PROGNAME + +Compare current version of Maco script with the latest and + the urgent versions then try to prepare the host by : + * Disabling SGE queue + +EXAMPLES : + - Verify Maco's upgrade and prepare the current host + ${PROGNAME} + EOF + +} +# }}} +debug_message() { # {{{ + + local_message="${1}" + + ## Print message if DEBUG is enable (=0) + [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_message}" + +} +# }}} +is_maco_urgent_upgrade_require() { # {{{ + + return_urgent_upgrade="" + + if [ "${URGENT_TIMESTAMP}" -gt "${CURRENT_TIMESTAMP}" ]; then + debug_message "is_maco_urgent_upgrade_require − \ +Urgent Maco upgrade is available (from version ${CURRENT_MACO_VERSION} to ${URGENT_MACO_VERSION})." + return_urgent_upgrade="0" + else + debug_message "is_maco_urgent_upgrade_require − \ +Local Maco (${CURRENT_MACO_VERSION}) is newer than urgent upgrade (${URGENT_MACO_VERSION})." + return_urgent_upgrade="1" + fi + + return "${return_urgent_upgrade}" + +} +# }}} +is_maco_upgrade_require() { # {{{ + + return_upgrade="" + + if [ "${LATEST_TIMESTAMP}" -gt "${CURRENT_TIMESTAMP}" ]; then + debug_message "is_maco_upgrade_require − \ +Latest Maco upgrade is available (from version ${CURRENT_MACO_VERSION} to ${LATEST_MACO_VERSION})." + return_upgrade="0" + else + debug_message "is_maco_upgrade_require − \ +Local Maco (${CURRENT_MACO_VERSION}) seems up to date (latest : ${LATEST_MACO_VERSION})." + return_upgrade="1" + fi + + return "${return_upgrade}" + +} +# }}} +is_maco_uptodate() { # {{{ + + return_uptodate="" + + if [ "${CURRENT_TIMESTAMP}" = "${LATEST_TIMESTAMP}" ]; then + debug_message "is_maco_uptodate − \ +Local Maco (${CURRENT_MACO_VERSION}) is up to date (latest : ${LATEST_MACO_VERSION})." + return_uptodate="0" + else + debug_message "is_maco_uptodate − \ +Local Maco version (${CURRENT_MACO_VERSION}) is different from latest version (${LATEST_MACO_VERSION})." + return_uptodate="1" + fi + + return "${return_uptodate}" + +} +# }}} +main() { # {{{ + + manage_args "${ARGS}" + + ## Get all Maco's versions (date) + readonly CURRENT_MACO_VERSION=$(< "${MACO_LOCAL_DIR}/maco-version.txt" awk -v FS=. '{ print $1 "-" $2 "-" $3 "T" $4 ":" $5 ":" $6 }' \ + || exit 1) + readonly LATEST_MACO_VERSION=$(< "${MACO_INSTALL_DIR}/maco-version.txt" awk -v FS=. '{ print $1 "-" $2 "-" $3 "T" $4 ":" $5 ":" $6 }' \ + || exit 2) + readonly URGENT_MACO_VERSION=$(< "${MACO_INSTALL_DIR}/urgent-maco-version.txt" awk -v FS=. '{ print $1 "-" $2 "-" $3 "T" $4 ":" $5 ":" $6 }' \ + || exit 3) + ## Convert version to timestamp + readonly CURRENT_TIMESTAMP=$(date -d "${CURRENT_MACO_VERSION}" "+%s") + readonly LATEST_TIMESTAMP=$(date -d "${LATEST_MACO_VERSION}" "+%s") + readonly URGENT_TIMESTAMP=$(date -d "${URGENT_MACO_VERSION}" "+%s") + + is_maco_urgent_upgrade_require \ + && exit 0 + + is_maco_upgrade_require \ + && exit 0 + + is_maco_uptodate \ + && exit 0 + +} +# }}} + +main + +exit 255