Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated cron startup process for ALPINE and fixed bugs #32

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildutils/APKBUILD.templ.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions buildutils/k2hr3-get-resource.post-install
Original file line number Diff line number Diff line change
Expand Up @@ -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"

#
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions buildutils/k2hr3-get-resource.pre-deinstall
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
#
# 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
echo "[ERROR] Not found crontab file(${CRONTAB_ROOT_FILE}) for root." 1>&2
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

Expand Down
101 changes: 84 additions & 17 deletions src/k2hr3-get-resource-helper
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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
#---------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand All @@ -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
#---------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions src/k2hr3-get-resource-helper.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down