From 14eab0e6eeccd2ca1dacacfc0f1a19ec73d51403 Mon Sep 17 00:00:00 2001 From: Takeshi Nakatani Date: Fri, 15 Nov 2024 14:38:46 +0900 Subject: [PATCH] Updated cron startup process for ALPINE and fixed bugs --- buildutils/APKBUILD.templ.in | 2 +- buildutils/k2hr3-get-resource.post-install | 5 +- buildutils/k2hr3-get-resource.pre-deinstall | 4 +- src/k2hr3-get-resource-helper | 101 ++++++++++++++++---- src/k2hr3-get-resource-helper.conf | 9 ++ 5 files changed, 99 insertions(+), 22 deletions(-) diff --git a/buildutils/APKBUILD.templ.in b/buildutils/APKBUILD.templ.in index 3022492..2eb2030 100644 --- a/buildutils/APKBUILD.templ.in +++ b/buildutils/APKBUILD.templ.in @@ -113,7 +113,7 @@ package() { # # Install helper files # - install -Dm755 src/"$pkgname"-helper "$pkgdir"/usr/bin/"$pkgname"-helper + install -Dm755 src/"$pkgname"-helper "$pkgdir"/usr/libexec/"$pkgname"-helper install -Dm644 src/"$pkgname"-helper.conf "$pkgdir"/etc/antpickax/"$pkgname"-helper.conf install -m 0777 -d "$pkgdir"/var/lib/antpickax diff --git a/buildutils/k2hr3-get-resource.post-install b/buildutils/k2hr3-get-resource.post-install index 4d4e28d..1964e10 100644 --- a/buildutils/k2hr3-get-resource.post-install +++ b/buildutils/k2hr3-get-resource.post-install @@ -24,7 +24,8 @@ # # Common variables # -K2HR3_GET_RESOURCE_HELPER_FILE="/usr/bin/k2hr3-get-resource-helper" +K2HR3_GET_RESOURCE_HELPER_FILE="/usr/libexec/k2hr3-get-resource-helper" +K2HR3_GET_RESOURCE_HELPER_OPTION="--cron" CRONTAB_ROOT_FILE="/etc/crontabs/root" # @@ -43,7 +44,7 @@ fi # # Setup crontab for root # -if ! echo "* * * * * /bin/sh ${K2HR3_GET_RESOURCE_HELPER_FILE}" >> "${CRONTAB_ROOT_FILE}" 2>/dev/null; then +if ! echo "*/15 * * * * /bin/sh ${K2HR3_GET_RESOURCE_HELPER_FILE} ${K2HR3_GET_RESOURCE_HELPER_OPTION}" >> "${CRONTAB_ROOT_FILE}" 2>/dev/null; then echo "[ERROR] Failed to set root crontab." 1>&2 exit 1 fi diff --git a/buildutils/k2hr3-get-resource.pre-deinstall b/buildutils/k2hr3-get-resource.pre-deinstall index 5bfaffd..89da332 100644 --- a/buildutils/k2hr3-get-resource.pre-deinstall +++ b/buildutils/k2hr3-get-resource.pre-deinstall @@ -24,7 +24,7 @@ # # Common variables # -K2HR3_GET_RESOURCE_HELPER_FILE="/usr/bin/k2hr3-get-resource-helper" +K2HR3_GET_RESOURCE_HELPER_FILE="/usr/libexec/k2hr3-get-resource-helper" CRONTAB_ROOT_FILE="/etc/crontabs/root" if [ ! -f "${CRONTAB_ROOT_FILE}" ]; then @@ -32,7 +32,7 @@ if [ ! -f "${CRONTAB_ROOT_FILE}" ]; then exit 1 fi -if ! sed -i -e "s|^.*${K2HR3_GET_RESOURCE_HELPER_FILE}[[:space:]]*$||g" "${CRONTAB_ROOT_FILE}"; then +if ! sed -i -e "s|^.*${K2HR3_GET_RESOURCE_HELPER_FILE}[[:space:]]*.*$||g" "${CRONTAB_ROOT_FILE}"; then echo "[WARNING] Failed to unset root crontab, but continue..." 1>&2 fi diff --git a/src/k2hr3-get-resource-helper b/src/k2hr3-get-resource-helper index 3c0e06b..5d5ed7b 100755 --- a/src/k2hr3-get-resource-helper +++ b/src/k2hr3-get-resource-helper @@ -26,23 +26,9 @@ BASE_PRGNAME=$(echo "${PRGNAME}" | sed -e 's/\.*$//g') SCRIPTDIR=$(dirname "${0}") SCRIPTDIR=$(cd "${SCRIPTDIR}" || exit 1; pwd) -# -# Options -# -# [NOTE] -# The option only accepts "-daemon". -# If it is passed "-daemon" option, it acts as a daemon that waits -# until the systemd k2hr3-get-resource.timer is disabled. -# When k2hr3-get-resource.timer is disabled, the file that output the -# resource is deleted and the process is terminated. -# -if [ -n "$1" ] && [ "$1" = "-daemon" ]; then - SCRIPT_MODE="daemon" -else - SCRIPT_MODE="normal" -fi -USE_DAEMON=1 - +#--------------------------------------------------------------------- +# Variables +#--------------------------------------------------------------------- # # systemd timer name # @@ -356,6 +342,55 @@ get_value() return 0 } +#--------------------------------------------------------------------- +# Parse Option +#--------------------------------------------------------------------- +OPT_DAEMON=0 +OPT_CRON=0 + +while [ $# -ne 0 ]; do + if [ -z "$1" ]; then + break; + + elif echo "$1" | grep -q -i -e "^--daemon$" -e "^-daemon$"; then + if [ "${OPT_DAEMON}" -ne 0 ]; then + log_err "Already specified --daemon option." + exit 1 + fi + OPT_DAEMON=1 + + elif echo "$1" | grep -q -i -e "^--cron" -e "^-cron"; then + if [ "${OPT_CRON}" -ne 0 ]; then + log_err "Already specified --cron option." + exit 1 + fi + OPT_CRON=1 + + else + log_err "Unknown option : \"$1\"" + exit 1 + fi + shift +done + +# [NOTE] +# The option only accepts "-daemon". +# If it is passed "-daemon" option, it acts as a daemon that waits +# until the systemd k2hr3-get-resource.timer is disabled. +# When k2hr3-get-resource.timer is disabled, the file that output the +# resource is deleted and the process is terminated. +# +if [ "${OPT_DAEMON}" -eq 1 ]; then + SCRIPT_MODE="daemon" +else + SCRIPT_MODE="normal" +fi + +# +# Default +# +USE_DAEMON=1 + #--------------------------------------------------------------------- # Load configuration and check #--------------------------------------------------------------------- @@ -499,6 +534,27 @@ else fi fi +# +# USE_CRON +# +if ! _TMP_VALUE=$(get_value "USE_CRON" "${CONFDIR}/${SCRIPT_CONF_FILE}" "${CONFDIR}/${OVERRIDE_FILE}"); then + if [ -z "${USE_CRON}" ]; then + log_err "There is no configuration value(USE_CRON) in configuration files and default value is empty." + exit 1 + fi + log_info "There is no configuration value(USE_CRON) in configuration files, thus use default value(${USE_CRON})" +else + if [ -n "${_TMP_VALUE}" ]; then + if [ "${_TMP_VALUE}" = "true" ] || [ "${_TMP_VALUE}" = "TRUE" ] || [ "${_TMP_VALUE}" = "1" ]; then + USE_CRON=1 + elif [ "${_TMP_VALUE}" = "false" ] || [ "${_TMP_VALUE}" = "FALSE" ] || [ "${_TMP_VALUE}" = "0" ]; then + USE_CRON=0 + else + log_warn "USE_CRON value in configuration is wrong value(${_TMP_VALUE}), then use default(${USE_CRON})." + fi + fi +fi + # # Check K2HR3 variables # K2HR3_ROLE_TOKEN @@ -518,6 +574,17 @@ else exit 1 fi +# +# Check launching from cron +# +if [ "${OPT_CRON}" -eq 1 ] && [ "${USE_CRON}" -eq 0 ]; then + # + # Nothing to do + # + log_info "The process was started via cron, but since cron startup is disabled, the execution will be canceled." + exit 0 +fi + #--------------------------------------------------------------------- # Main #--------------------------------------------------------------------- diff --git a/src/k2hr3-get-resource-helper.conf b/src/k2hr3-get-resource-helper.conf index 6975b8d..4df02d2 100644 --- a/src/k2hr3-get-resource-helper.conf +++ b/src/k2hr3-get-resource-helper.conf @@ -108,6 +108,15 @@ # # USE_DAEMON = true +#--------------------------------------------------------------------- +# USE_CRON +#--------------------------------------------------------------------- +# Spacify launching process from cron to start the monitoring. +# If true or 1 is specified, it will be valid. +# If false or 0 is specified, it will be invalid.(default) +# +# USE_CRON = false + # # Local variables: # tab-width: 4