Added support for using `/etc/cron.d` for auto-updates. (#9598)

* Add crontab fragment for netdata updater.

This adds a crontab fragment to be put in `/etc/cron.d` on system swhich
do not support either `/etc/cron.daily` or `/etc/periodic/daily`,
allowing for proper auto-updater support on such systems.

The crontab will run upates at 02:57, which was chosen arbitrarily
because it does not conflict with th erun times for any major
distribution's daily scheduled tasks.

* Utilize the crontab fragment for updates on systems that need it.

This adds logic to utilize the crontab fragment added by the previous
commit on systems which need it for auto-updates to work. The installer
will preferentially use `/etc/cron.daily` or `/etc/periodic/daily`
instead if they exist, so this should result in no changes for most
users.

* Remove systemd scheduler detection.

We're not actually using it right now, and it breaks handling of
auto-updates on systemd systems.
This commit is contained in:
Austin S. Hemmelgarn 2020-09-15 07:40:05 -04:00 committed by GitHub
parent 0d8972d9c1
commit 59a2aec9c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 38 deletions

View File

@ -5,6 +5,7 @@
-e 's#[@]pluginsdir_POST@#$(pluginsdir)#g' \
-e 's#[@]configdir_POST@#$(configdir)#g' \
-e 's#[@]libconfigdir_POST@#$(libconfigdir)#g' \
-e 's#[@]pkglibexecdir_POST@#$(pkglibexecdir)#g' \
-e 's#[@]cachedir_POST@#$(cachedir)#g' \
-e 's#[@]registrydir_POST@#$(registrydir)#g' \
-e 's#[@]varlibdir_POST@#$(varlibdir)#g' \

View File

@ -898,23 +898,22 @@ safe_sha256sum() {
fi
}
_get_crondir() {
_get_scheduler_type() {
if _get_intervaldir > /dev/null ; then
echo 'interval'
elif [ -d /etc/cron.d ] ; then
echo 'crontab'
else
echo 'none'
fi
}
_get_intervaldir() {
if [ -d /etc/cron.daily ]; then
echo /etc/cron.daily
elif [ -d /etc/periodic/daily ]; then
echo /etc/periodic/daily
else
echo >&2 "Cannot figure out the cron directory to handle netdata-updater.sh activation/deactivation"
return 1
fi
return 0
}
_check_crondir_permissions() {
if [ "${UID}" -ne "0" ]; then
# We cant touch cron if we are not running as root
echo >&2 "You need to run the installer as root for auto-updating via cron"
return 1
fi
@ -945,49 +944,68 @@ cleanup_old_netdata_updater() {
rm -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh
fi
crondir="$(_get_crondir)" || return 1
_check_crondir_permissions "${crondir}" || return 1
if [ -d /etc/cron.daily ]; then
rm -f /etc/cron.daily/netdata-updater.sh
rm -f /etc/cron.daily/netdata-updater
fi
if [ -f "${crondir}/netdata-updater.sh" ]; then
echo >&2 "Removing incorrect netdata-updater filename in cron"
rm -f "${crondir}/netdata-updater.sh"
if [ -d /etc/periodic/daily ]; then
rm -f /etc/periodic/daily/netdata-updater.sh
rm -f /etc/periodic/daily/netdata-updater
fi
if [ -d /etc/cron.d ]; then
rm -f /etc/cron.d/netdata-updater
fi
return 0
}
enable_netdata_updater() {
crondir="$(_get_crondir)" || return 1
_check_crondir_permissions "${crondir}" || return 1
case "$(_get_scheduler_type)" in
"interval")
ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "$(_get_interval_dir)/netdata-updater"
echo >&2 "Adding to cron"
echo >&2 "Auto-updating has been enabled through cron, updater script linked to ${TPUT_RED}${TPUT_BOLD}$(_get_interval_dir)/netdata-updater${TPUT_RESET}"
echo >&2
echo >&2 "If the update process fails and you have email notifications set up correctly for cron on this system, you should receive an email notification of the failure."
echo >&2 "Successful updates will not send an email."
echo >&2
;;
"crontab")
cat "${NETDATA_SOURCE_DIR}/system/netdata.crontab" > "/etc/cron.d/netdata-updater"
rm -f "${crondir}/netdata-updater"
ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "${crondir}/netdata-updater"
echo >&2 "Auto-updating has been enabled. Updater script linked to: ${TPUT_RED}${TPUT_BOLD}${crondir}/netdata-updater${TPUT_RESET}"
echo >&2
echo >&2 "${TPUT_DIM}${TPUT_BOLD}netdata-updater.sh${TPUT_RESET}${TPUT_DIM} works from cron. It will trigger an email from cron"
echo >&2 "only if it fails (it should not print anything when it can update netdata).${TPUT_RESET}"
echo >&2
echo >&2 "Auto-updating has been enabled through cron, using a crontab at ${TPUT_RED}${TPUT_BOLD}/etc/cron.d/netdata${TPUT_RESET}"
echo >&2
echo >&2 "If the update process fails and you have email notifications set up correctly for cron on this system, you should receive an email notification of the failure."
echo >&2 "Successful updates will not send an email."
echo >&2
;;
*)
echo >&2 "Unable to determine what type of auto-update scheduling to use, not enabling auto-updates."
echo >&2
return 1
esac
return 0
}
disable_netdata_updater() {
crondir="$(_get_crondir)" || return 1
_check_crondir_permissions "${crondir}" || return 1
echo >&2 "You chose *NOT* to enable auto-update, removing any links to the updater from cron (it may have happened if you are reinstalling)"
echo >&2
if [ -f "${crondir}/netdata-updater" ]; then
echo >&2 "Removing cron reference: ${crondir}/netdata-updater"
echo >&2
rm -f "${crondir}/netdata-updater"
else
echo >&2 "Did not find any cron entries to remove"
echo >&2
if [ -d /etc/cron.daily ]; then
rm -f /etc/cron.daily/netdata-updater.sh
rm -f /etc/cron.daily/netdata-updater
fi
if [ -d /etc/periodic/daily ]; then
rm -f /etc/periodic/daily/netdata-updater.sh
rm -f /etc/periodic/daily/netdata-updater
fi
if [ -d /etc/cron.d ]; then
rm -f /etc/cron.d/netdata-updater
fi
return 0

View File

@ -456,6 +456,8 @@ rm_file /usr/lib/systemd/system/netdata.service
rm_file /etc/init.d/netdata
rm_file /etc/periodic/daily/netdata-updater
rm_file /etc/cron.daily/netdata-updater
rm_file /etc/cron.d/netdata-updater
if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}" ]; then
rm_dir "${NETDATA_PREFIX}"

View File

@ -12,6 +12,7 @@ CLEANFILES = \
netdata-lsb \
netdata-freebsd \
netdata.plist \
netdata.crontab \
$(NULL)
include $(top_srcdir)/build/subst.inc
@ -34,6 +35,7 @@ nodist_noinst_DATA = \
netdata-lsb \
netdata-freebsd \
netdata.plist \
netdata.crontab \
$(NULL)
dist_noinst_DATA = \
@ -47,4 +49,5 @@ dist_noinst_DATA = \
netdata-freebsd.in \
netdata.plist.in \
netdata.conf \
netdata.crontab.in \
$(NULL)

View File

@ -0,0 +1 @@
2 57 * * * root @pkglibexecdir_POST@/netdata-updater.sh