2020-06-04 14:37:01 +02:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
|
|
# This script will check if any APT upgrade is available and
|
|
|
|
|
# will prepare the host in order to apply upgrade with another script
|
2020-06-18 08:45:28 +02:00
|
|
|
|
# 1. Create a temp file
|
|
|
|
|
# 2. Disable SGE queue
|
2020-06-04 14:37:01 +02:00
|
|
|
|
|
|
|
|
|
# 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=1
|
|
|
|
|
## Export DEBUG for sub-script
|
|
|
|
|
export DEBUG
|
|
|
|
|
|
2020-06-18 08:45:28 +02:00
|
|
|
|
readonly APT_TMP_FILE="/tmp/.apt.upgrade"
|
|
|
|
|
|
2020-06-04 14:37:01 +02:00
|
|
|
|
## Colors
|
|
|
|
|
readonly PURPLE='\033[1;35m'
|
|
|
|
|
readonly RED='\033[0;31m'
|
|
|
|
|
readonly RESET='\033[0m'
|
|
|
|
|
readonly COLOR_DEBUG="${PURPLE}"
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
Verify if any APT package upgrade is available and
|
|
|
|
|
try to prepare the host by :
|
|
|
|
|
* Disabling SGE queue
|
|
|
|
|
|
|
|
|
|
EXAMPLES :
|
|
|
|
|
- Verify 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}"
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
2020-06-15 13:49:25 +02:00
|
|
|
|
is_apt_upgrade_absent() { # {{{
|
2020-06-04 14:37:01 +02:00
|
|
|
|
|
|
|
|
|
## Count the number of upgradable packages and substract 1 for the header
|
2020-06-04 16:52:10 +02:00
|
|
|
|
local_apt_upgrade_number="$(apt list --upgradable \
|
|
|
|
|
| wc -l \
|
|
|
|
|
| awk '{print $1-1}')"
|
2020-06-04 14:37:01 +02:00
|
|
|
|
|
|
|
|
|
case "${local_apt_upgrade_number}" in
|
|
|
|
|
0 ) ## No available upgrade
|
|
|
|
|
return_apt_upgrade_absent="0"
|
|
|
|
|
;;
|
|
|
|
|
* ) ## Upgrade seems available
|
|
|
|
|
return_apt_upgrade_absent="1"
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
## Simple debug message to valid current variable
|
|
|
|
|
debug_message "is_apt_upgrade_absent − \
|
|
|
|
|
APT upgrade available for this system: ${RED}${local_apt_upgrade_number:=/dev/null}${COLOR_DEBUG}."
|
|
|
|
|
|
|
|
|
|
return "${return_apt_upgrade_absent}"
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
2020-06-15 13:49:25 +02:00
|
|
|
|
is_apt_upgrade_present() { # {{{
|
2020-06-04 14:37:01 +02:00
|
|
|
|
|
|
|
|
|
## Count the number of upgradable packages and substract 1 for the header
|
2020-06-04 16:52:10 +02:00
|
|
|
|
local_apt_upgrade_number="$(apt list --upgradable \
|
|
|
|
|
| wc -l \
|
|
|
|
|
| awk '{print $1-1}')"
|
2020-06-04 14:37:01 +02:00
|
|
|
|
|
|
|
|
|
case "${local_apt_upgrade_number}" in
|
|
|
|
|
0 ) ## No available upgrade
|
|
|
|
|
return_apt_upgrade_present="1"
|
|
|
|
|
;;
|
|
|
|
|
* ) ## Upgrade seems available
|
|
|
|
|
return_apt_upgrade_present="0"
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
## Simple debug message to valid current variable
|
|
|
|
|
debug_message "is_apt_upgrade_present − \
|
|
|
|
|
APT upgrade available for this system: ${RED}${local_apt_upgrade_number:=/dev/null}${COLOR_DEBUG}."
|
|
|
|
|
|
|
|
|
|
return "${return_apt_upgrade_present}"
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
main() { # {{{
|
|
|
|
|
|
|
|
|
|
manage_args "${ARGS}"
|
|
|
|
|
|
2020-06-22 11:25:11 +02:00
|
|
|
|
sge_disable_host_queue_script="${PROGDIR}/sge.disable.host.queue.sh"
|
2020-06-04 14:37:01 +02:00
|
|
|
|
|
|
|
|
|
## If NO APT package upgrade is available
|
2020-06-18 08:45:28 +02:00
|
|
|
|
### Ensure to remove any temp file related to APT upgrades
|
2020-06-04 14:37:01 +02:00
|
|
|
|
### Exit
|
|
|
|
|
is_apt_upgrade_absent \
|
2020-06-18 08:45:28 +02:00
|
|
|
|
&& rm -f -- "${APT_TMP_FILE}" \
|
2020-06-04 14:37:01 +02:00
|
|
|
|
&& exit 0
|
|
|
|
|
|
|
|
|
|
## If APT package upgrade is available
|
2020-06-18 08:45:28 +02:00
|
|
|
|
### Create a temp file
|
2020-06-04 14:37:01 +02:00
|
|
|
|
### Disable SGE queue
|
|
|
|
|
is_apt_upgrade_present \
|
2020-06-18 08:45:28 +02:00
|
|
|
|
&& touch "${APT_TMP_FILE}" && echo "APT upgrade is available." >> "${APT_TMP_FILE}" \
|
2020-06-22 11:25:11 +02:00
|
|
|
|
&& sh "${sge_disable_host_queue_script}" \
|
2020-06-15 13:49:25 +02:00
|
|
|
|
&& exit 0
|
2020-06-04 14:37:01 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
main
|
|
|
|
|
|
2020-06-15 13:49:25 +02:00
|
|
|
|
exit 255
|