235 lines
5.9 KiB
Bash
Executable File
235 lines
5.9 KiB
Bash
Executable File
#!/bin/sh
|
||
|
||
# This script will try to apply APT upgrades if all conditions are satisfied
|
||
# 1. SGE queue is disable
|
||
# 2. No SGE jobs are running
|
||
# 3. No process related to Maco are running
|
||
|
||
# This script can be call by a cronjob (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}"
|
||
# }}}
|
||
|
||
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
|
||
|
||
Apply any APT package upgrade if the host is free :
|
||
* SGE queue is disable
|
||
* No SGE jobs are running
|
||
|
||
EXAMPLES :
|
||
- Apply upgrade on 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_apt_upgrade_absent() { # {{{
|
||
|
||
## Count the number of upgradable packages and substract 1 for the header
|
||
#local_apt_upgrade_number="$(apt list --upgradable \
|
||
#| wc -l \
|
||
#| awk '{print $1-1}')"
|
||
local_apt_upgrade_number="5"
|
||
|
||
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}"
|
||
|
||
}
|
||
# }}}
|
||
is_apt_upgrade_present() { # {{{
|
||
|
||
## Count the number of upgradable packages and substract 1 for the header
|
||
local_apt_upgrade_number="$(apt list --upgradable \
|
||
| wc -l \
|
||
| awk '{print $1-1}')"
|
||
|
||
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}"
|
||
|
||
}
|
||
# }}}
|
||
is_queue_enable() { # {{{
|
||
|
||
local_sge_hostname="${1}"
|
||
local_sge_queue_name="${2}"
|
||
## List all queues with 'disable' state and filter to the expected queue name
|
||
## with a fake_user to avoid pending jobs for this queue
|
||
### And count returned lines
|
||
local_sge_queue_test=$(qstat -f -qs d -q "${local_sge_queue_name:=/dev/null}@${local_sge_hostname:=/dev/null}" -u fake_user \
|
||
| wc -l)
|
||
|
||
case "${local_sge_queue_test}" in
|
||
0 ) ## No result so the queue is enable
|
||
local_sge_queue_state="enable"
|
||
return_queue_enable="0"
|
||
;;
|
||
3 ) ## Results (header + queue name) so the queue is disable
|
||
local_sge_queue_state="disable"
|
||
return_queue_enable="1"
|
||
;;
|
||
* ) ## Unexpected result
|
||
printf '%b\n' "${RED}Not able to determine the state of ${local_sge_queue_name:=/dev/null}@${local_sge_hostname:=/dev/null} queue (command return ${local_sge_queue_test} lines).${RESET}"
|
||
exit 2
|
||
;;
|
||
esac
|
||
|
||
## Simple debug message to valid current variables
|
||
debug_message "is_queue_enable − \
|
||
SGE queue: ${RED}${local_sge_queue_name:=/dev/null}${COLOR_DEBUG} \
|
||
state is: ${RED}${local_sge_queue_state:=/dev/null}${COLOR_DEBUG}."
|
||
|
||
return "${return_queue_enable}"
|
||
|
||
}
|
||
# }}}
|
||
is_job_running() { # {{{
|
||
|
||
local_sge_hostname="${1}"
|
||
## List SGE informations about the host
|
||
### And get the number of used slots
|
||
local_sge_slots_used=$(qhost -h "${local_sge_hostname:=/dev/null}" -q -xml \
|
||
| grep slots_used \
|
||
| sed 's;.*<queuevalue.*>\(.*\)</queuevalue>;\1;')
|
||
|
||
case "${local_sge_slots_used}" in
|
||
0 ) ## No jobs are running
|
||
return_job_running="1"
|
||
;;
|
||
* ) ## Results (header + queue name) so the queue is disable
|
||
return_job_running="0"
|
||
;;
|
||
esac
|
||
|
||
## Simple debug message to valid current variables
|
||
debug_message "is_job_running − \
|
||
jobs running on ${local_sge_hostname} host : ${RED}${local_sge_slots_used}${COLOR_DEBUG}."
|
||
|
||
return "${return_job_running}"
|
||
|
||
}
|
||
# }}}
|
||
is_proc_running() { # {{{
|
||
|
||
local_proc_pattern="${1}"
|
||
|
||
local_count_proc_pattern="$(pgrep -f -- "${local_proc_pattern}" | wc -l)"
|
||
|
||
case "${local_count_proc_pattern}" in
|
||
0 ) ## No procs related to this pattern are running
|
||
return_proc_running="1"
|
||
;;
|
||
* ) ## At least one proc seems running
|
||
return_proc_running="0"
|
||
;;
|
||
esac
|
||
|
||
## Simple debug message to valid current variables
|
||
debug_message "is_proc_running − \
|
||
${local_count_proc_pattern} procs seems running according to the pattern: ${RED}${local_proc_pattern}${COLOR_DEBUG}."
|
||
|
||
return "${return_proc_running}"
|
||
|
||
}
|
||
# }}}
|
||
main() { # {{{
|
||
|
||
manage_args "${ARGS}"
|
||
|
||
sge_hostname="$(hostname -f)"
|
||
sge_queue_name="$(qhost -h "${sge_hostname:=/dev/null}" -q -xml \
|
||
| grep "queue name" \
|
||
| cut -d"'" -f2 )"
|
||
|
||
maco_proc_pattern="(/opt/maco/bin/maco.autoupdate.sh)"
|
||
|
||
## If NO APT package upgrade is available
|
||
### Exit
|
||
is_apt_upgrade_absent \
|
||
&& exit 0
|
||
|
||
## If SGE queue is enable
|
||
### Exit
|
||
is_queue_enable "${sge_hostname}" "${sge_queue_name}" \
|
||
&& exit 0
|
||
|
||
## If any SGE job runs
|
||
### Exit
|
||
is_job_running "${sge_hostname}" \
|
||
&& exit 0
|
||
|
||
## If anything to maco is currently running
|
||
### Exit
|
||
is_proc_running "${maco_proc_pattern}" \
|
||
&& exit 0
|
||
|
||
}
|
||
# }}}
|
||
|
||
main
|
||
|
||
exit 0
|