scripts/cluster/maco.remove.jobs.sh

166 lines
3.7 KiB
Bash
Raw Permalink Normal View History

#!/bin/sh
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}"
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [--help] [-d|-h]
Try to disable default Maco's cron (upgrades,…).
EXAMPLES:
- Disable Maco's cron on the current host
${PROGNAME}
OPTIONS:
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
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}"
return 0
2020-06-24 11:51:24 +02:00
}
# }}}
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 \
procs running (with the pattern: ${RED}${local_proc_pattern}${COLOR_DEBUG}) on the current host: ${RED}${local_count_proc_pattern}${COLOR_DEBUG}."
return "${return_proc_running}"
}
# }}}
remove_cron() { # {{{
debug_message "remove_cron \
2021-02-15 12:06:16 +01:00
Try to remove maco's cron jobs (autoupdate,…)."
2020-06-24 12:24:12 +02:00
## Remove default Maco cron that check updates
rm -f /etc/cron.daily/ipr_autoupdate
2020-06-24 12:24:12 +02:00
}
# }}}
manage_systemd_service() { # {{{
maco_service_name="maco.service"
## If maco.service is enable
## Disable it at boot
systemctl --quiet is-enabled "${maco_service_name}" \
&& debug_message "manage_systemd_service Try to disable ${maco_service_name}." \
&& systemctl --quiet disable "${maco_service_name}" > /dev/null 2>&1
## After a Maco's upgrade, the service should be set.
## If this upgrade require reboot, then the service should be run
## at the next boot
## It will be re-disable by this script after the upgrade.
}
# }}}
main() { # {{{
2020-06-24 11:51:24 +02:00
maco_proc_pattern="(/opt/maco/bin/maco.autoupdate.sh)"
## If anything related to maco is currently running
### Exit
is_proc_running "${maco_proc_pattern}" \
&& exit 0
remove_cron
2020-06-24 12:24:12 +02:00
manage_systemd_service
}
# }}}
# Manage arguments # {{{
# 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
-d|--debug ) ## debug
DEBUG=0
;;
-h,--help ) ## help
usage
## Exit after help informations
exit 0
;;
-- ) ## End of options list
## End the while loop
break
;;
* ) ## unknow option
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main
exit 0