2020-06-12 12:34:23 +02:00
|
|
|
|
#!/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="${#}"
|
2020-06-12 15:23:28 +02:00
|
|
|
|
|
2020-06-12 15:13:53 +02:00
|
|
|
|
[ -z "${DEBUG}" ] && DEBUG=1
|
2020-06-12 12:34:23 +02:00
|
|
|
|
## Export DEBUG for sub-script
|
|
|
|
|
export DEBUG
|
|
|
|
|
|
2020-06-15 13:48:52 +02:00
|
|
|
|
# If output message should be displayed
|
2020-06-12 15:23:28 +02:00
|
|
|
|
[ -z "${OUTPUT_MESSAGE}" ] && OUTPUT_MESSAGE=0
|
|
|
|
|
## Export OUTPUT_MESSAGE for sub-script
|
|
|
|
|
export OUTPUT_MESSAGE
|
|
|
|
|
|
2020-06-15 13:48:52 +02:00
|
|
|
|
# If modifications should be applied
|
|
|
|
|
[ -z "${SIMULATE_MODE}" ] && SIMULATE_MODE=1
|
|
|
|
|
|
2020-06-12 12:34:23 +02:00
|
|
|
|
## 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"
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
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() { # {{{
|
|
|
|
|
|
2020-06-12 15:23:28 +02:00
|
|
|
|
local_debug_message="${1}"
|
2020-06-12 12:34:23 +02:00
|
|
|
|
|
|
|
|
|
## Print message if DEBUG is enable (=0)
|
2020-06-12 15:23:28 +02:00
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_debug_message}"
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
2020-06-12 17:24:34 +02:00
|
|
|
|
message() { # {{{
|
2020-06-12 15:23:28 +02:00
|
|
|
|
|
|
|
|
|
local_message="${1}"
|
|
|
|
|
|
|
|
|
|
## Print message if OUTPUT_MESSAGE is enable (=0)
|
2020-06-12 17:24:34 +02:00
|
|
|
|
[ "${OUTPUT_MESSAGE}" -eq "0" ] && printf '%b\n' "${local_message}"
|
2020-06-12 15:23:28 +02:00
|
|
|
|
|
|
|
|
|
return 0
|
2020-06-12 12:34:23 +02:00
|
|
|
|
|
2020-06-15 09:23:50 +02:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
is_var_empty() { # {{{
|
|
|
|
|
|
|
|
|
|
## Return False by default
|
|
|
|
|
return_var_empty="1"
|
|
|
|
|
## Total number of variables to test
|
|
|
|
|
local_total_var="${#}"
|
|
|
|
|
|
|
|
|
|
loop_count_var_empty="0"
|
|
|
|
|
|
|
|
|
|
## While it remains a variable to test
|
|
|
|
|
while [ "${local_total_var}" -gt "${loop_count_var_empty}" ]; do
|
|
|
|
|
debug_message "is_var_empty − \
|
|
|
|
|
Test var: ${1}."
|
|
|
|
|
### Test if this is empty and set return value to True
|
|
|
|
|
[ -z "${1}" ] && return_var_empty="0"
|
|
|
|
|
|
|
|
|
|
### Increase the number of tested variables
|
|
|
|
|
loop_count_var_empty=$((loop_count_var_empty+1))
|
|
|
|
|
|
|
|
|
|
### Shift to the next variable
|
|
|
|
|
shift
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
return "${return_var_empty}"
|
|
|
|
|
|
2020-06-12 12:34:23 +02:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
2020-06-12 17:24:34 +02:00
|
|
|
|
is_maco_urgent_upgrade_require() { # {{{
|
2020-06-12 12:34:23 +02:00
|
|
|
|
|
|
|
|
|
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})."
|
2020-06-12 15:23:28 +02:00
|
|
|
|
message "Urgent Maco upgrade is available (from version ${CURRENT_MACO_VERSION} to ${URGENT_MACO_VERSION})."
|
2020-06-12 12:34:23 +02:00
|
|
|
|
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}"
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
2020-06-12 17:24:34 +02:00
|
|
|
|
is_maco_upgrade_require() { # {{{
|
2020-06-12 12:34:23 +02:00
|
|
|
|
|
|
|
|
|
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})."
|
2020-06-12 15:23:28 +02:00
|
|
|
|
message "Latest Maco upgrade is available (from version ${CURRENT_MACO_VERSION} to ${LATEST_MACO_VERSION})."
|
2020-06-12 12:34:23 +02:00
|
|
|
|
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}"
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
2020-06-12 17:24:34 +02:00
|
|
|
|
is_maco_uptodate() { # {{{
|
2020-06-12 12:34:23 +02:00
|
|
|
|
|
|
|
|
|
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})."
|
2020-06-12 15:23:28 +02:00
|
|
|
|
message "Local Maco (${CURRENT_MACO_VERSION}) is up to date (latest : ${LATEST_MACO_VERSION})."
|
2020-06-12 12:34:23 +02:00
|
|
|
|
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}"
|
|
|
|
|
|
2020-06-12 17:24:34 +02:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
prepare_host_for_upgrade() { # {{{
|
|
|
|
|
|
|
|
|
|
disable_host_queue_script="${PROGDIR}/disable.host.queue.sh"
|
|
|
|
|
|
2020-06-15 13:48:52 +02:00
|
|
|
|
if [ "${SIMULATE_MODE}" -eq 0 ]; then
|
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
debug_message "prepare_host_for_upgrade − \
|
2020-06-12 17:24:34 +02:00
|
|
|
|
Disable SGE queue."
|
2020-06-15 13:48:52 +02:00
|
|
|
|
sh "${disable_host_queue_script}"
|
2020-06-12 17:24:34 +02:00
|
|
|
|
|
2020-06-15 13:48:52 +02:00
|
|
|
|
return "${?}"
|
|
|
|
|
fi
|
2020-06-12 17:24:34 +02:00
|
|
|
|
|
2020-06-12 12:34:23 +02:00
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
main() { # {{{
|
|
|
|
|
|
|
|
|
|
## Get all Maco's versions (date)
|
2020-06-15 09:23:50 +02:00
|
|
|
|
readonly CURRENT_MACO_VERSION=$(< "${MACO_LOCAL_DIR}/maco-version.txt" awk -v FS=. '{ print $1 "-" $2 "-" $3 "T" $4 ":" $5 ":" $6 }' )
|
|
|
|
|
readonly LATEST_MACO_VERSION=$(< "${MACO_INSTALL_DIR}/maco-version.txt" awk -v FS=. '{ print $1 "-" $2 "-" $3 "T" $4 ":" $5 ":" $6 }' )
|
|
|
|
|
readonly URGENT_MACO_VERSION=$(< "${MACO_INSTALL_DIR}/urgent-maco-version.txt" awk -v FS=. '{ print $1 "-" $2 "-" $3 "T" $4 ":" $5 ":" $6 }' )
|
2020-06-12 12:34:23 +02:00
|
|
|
|
## 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")
|
|
|
|
|
|
2020-06-15 09:23:50 +02:00
|
|
|
|
## If a variable is empty
|
|
|
|
|
## Exit with error code
|
|
|
|
|
is_var_empty "${CURRENT_MACO_VERSION}" "${LATEST_MACO_VERSION}" "${URGENT_MACO_VERSION}" "${CURRENT_TIMESTAMP}" "${LATEST_TIMESTAMP}" "${URGENT_TIMESTAMP}" \
|
|
|
|
|
&& exit 1
|
|
|
|
|
|
2020-06-12 16:21:20 +02:00
|
|
|
|
is_maco_uptodate \
|
2020-06-12 12:34:23 +02:00
|
|
|
|
&& exit 0
|
|
|
|
|
|
|
|
|
|
is_maco_upgrade_require \
|
2020-06-12 17:24:34 +02:00
|
|
|
|
&& prepare_host_for_upgrade \
|
2020-06-12 12:34:23 +02:00
|
|
|
|
&& exit 0
|
|
|
|
|
|
2020-06-12 16:21:20 +02:00
|
|
|
|
is_maco_urgent_upgrade_require \
|
2020-06-12 17:24:34 +02:00
|
|
|
|
&& prepare_host_for_upgrade \
|
2020-06-12 12:34:23 +02:00
|
|
|
|
&& exit 0
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
|
2020-06-12 17:24:34 +02:00
|
|
|
|
# Manage arguments # {{{
|
2020-06-12 15:13:53 +02:00
|
|
|
|
# This code can't be in a function due to arguments
|
|
|
|
|
|
|
|
|
|
if [ ! "${NBARGS}" -eq "0" ]; then
|
|
|
|
|
|
|
|
|
|
manage_arg="0"
|
|
|
|
|
# Parse all options (start with a "-") one by one
|
|
|
|
|
while printf -- '%s' "${1}" | grep -q -E -- "^-*"; do
|
|
|
|
|
|
|
|
|
|
case "${1}" in
|
|
|
|
|
-h|--help ) ## help
|
|
|
|
|
usage
|
|
|
|
|
## Exit after help informations
|
|
|
|
|
exit 0
|
|
|
|
|
;;
|
|
|
|
|
-d|--debug ) ## debug
|
|
|
|
|
DEBUG=0
|
|
|
|
|
## Re-export new DEBUG value
|
|
|
|
|
export DEBUG
|
|
|
|
|
;;
|
2020-06-12 15:23:28 +02:00
|
|
|
|
-q|--quiet ) ## Silent mode
|
|
|
|
|
## Avoid to display any message on standard output
|
|
|
|
|
OUTPUT_MESSAGE=1
|
|
|
|
|
;;
|
2020-06-15 13:48:52 +02:00
|
|
|
|
-s|--simulate ) ## Simulate mode
|
|
|
|
|
## Only display messages
|
|
|
|
|
SIMULATE_MODE=0
|
2020-06-12 15:23:28 +02:00
|
|
|
|
;;
|
2020-06-12 15:13:53 +02:00
|
|
|
|
-- ) ## End of options list
|
|
|
|
|
## End the while loop
|
|
|
|
|
break
|
|
|
|
|
;;
|
|
|
|
|
* )
|
|
|
|
|
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
|
|
|
|
|
printf '%b\n' "---"
|
|
|
|
|
usage
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
debug_message "Arguments management − \
|
|
|
|
|
${1} option managed."
|
|
|
|
|
|
|
|
|
|
## Next arg
|
|
|
|
|
shift
|
|
|
|
|
manage_arg=$((manage_arg+1))
|
|
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
debug_message "Arguments management − \
|
|
|
|
|
${manage_arg} argument(s) successfully managed."
|
|
|
|
|
else
|
|
|
|
|
debug_message "Arguments management − \
|
|
|
|
|
No arguments/options to manage."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# }}}
|
|
|
|
|
|
2020-06-12 12:34:23 +02:00
|
|
|
|
main
|
|
|
|
|
|
|
|
|
|
exit 255
|