@cryptotaxi247 / netdata-1 / commits / 59a2aec9c

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.

Austin S. Hemmelgarn committed Sep 15, 2020 at 07:40 UTC 59a2aec9c8fb2dfbcfe80668571c39cc08af770c
5 files changed +65 -40
build/subst.inc
+1
@@ -5,6 +5,7 @@
5 -e 's#[@]pluginsdir_POST@#$(pluginsdir)#g' \
6 -e 's#[@]configdir_POST@#$(configdir)#g' \
7 -e 's#[@]libconfigdir_POST@#$(libconfigdir)#g' \
8 + -e 's#[@]pkglibexecdir_POST@#$(pkglibexecdir)#g' \
9 -e 's#[@]cachedir_POST@#$(cachedir)#g' \
10 -e 's#[@]registrydir_POST@#$(registrydir)#g' \
11 -e 's#[@]varlibdir_POST@#$(varlibdir)#g' \
packaging/installer/functions.sh
+58 -40
@@ -898,23 +898,22 @@ safe_sha256sum() {
898 fi
899 }
900
901 -_get_crondir() {
901 +_get_scheduler_type() {
902 + if _get_intervaldir > /dev/null ; then
903 + echo 'interval'
904 + elif [ -d /etc/cron.d ] ; then
905 + echo 'crontab'
906 + else
907 + echo 'none'
908 + fi
909 +}
910 +
911 +_get_intervaldir() {
912 if [ -d /etc/cron.daily ]; then
913 echo /etc/cron.daily
914 elif [ -d /etc/periodic/daily ]; then
915 echo /etc/periodic/daily
916 else
907 - echo >&2 "Cannot figure out the cron directory to handle netdata-updater.sh activation/deactivation"
908 - return 1
909 - fi
910 -
911 - return 0
912 -}
913 -
914 -_check_crondir_permissions() {
915 - if [ "${UID}" -ne "0" ]; then
916 - # We cant touch cron if we are not running as root
917 - echo >&2 "You need to run the installer as root for auto-updating via cron"
917 return 1
918 fi
919
@@ -945,49 +944,68 @@ cleanup_old_netdata_updater() {
944 rm -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh
945 fi
946
948 - crondir="$(_get_crondir)" || return 1
949 - _check_crondir_permissions "${crondir}" || return 1
947 + if [ -d /etc/cron.daily ]; then
948 + rm -f /etc/cron.daily/netdata-updater.sh
949 + rm -f /etc/cron.daily/netdata-updater
950 + fi
951
951 - if [ -f "${crondir}/netdata-updater.sh" ]; then
952 - echo >&2 "Removing incorrect netdata-updater filename in cron"
953 - rm -f "${crondir}/netdata-updater.sh"
952 + if [ -d /etc/periodic/daily ]; then
953 + rm -f /etc/periodic/daily/netdata-updater.sh
954 + rm -f /etc/periodic/daily/netdata-updater
955 + fi
956 +
957 + if [ -d /etc/cron.d ]; then
958 + rm -f /etc/cron.d/netdata-updater
959 fi
960
961 return 0
962 }
963
964 enable_netdata_updater() {
960 - crondir="$(_get_crondir)" || return 1
961 - _check_crondir_permissions "${crondir}" || return 1
962 -
963 - echo >&2 "Adding to cron"
964 -
965 - rm -f "${crondir}/netdata-updater"
966 - ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "${crondir}/netdata-updater"
967 -
968 - echo >&2 "Auto-updating has been enabled. Updater script linked to: ${TPUT_RED}${TPUT_BOLD}${crondir}/netdata-updater${TPUT_RESET}"
969 - echo >&2
970 - echo >&2 "${TPUT_DIM}${TPUT_BOLD}netdata-updater.sh${TPUT_RESET}${TPUT_DIM} works from cron. It will trigger an email from cron"
971 - echo >&2 "only if it fails (it should not print anything when it can update netdata).${TPUT_RESET}"
972 - echo >&2
965 + case "$(_get_scheduler_type)" in
966 + "interval")
967 + ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "$(_get_interval_dir)/netdata-updater"
968 +
969 + echo >&2 "Auto-updating has been enabled through cron, updater script linked to ${TPUT_RED}${TPUT_BOLD}$(_get_interval_dir)/netdata-updater${TPUT_RESET}"
970 + echo >&2
971 + 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."
972 + echo >&2 "Successful updates will not send an email."
973 + echo >&2
974 + ;;
975 + "crontab")
976 + cat "${NETDATA_SOURCE_DIR}/system/netdata.crontab" > "/etc/cron.d/netdata-updater"
977 +
978 + echo >&2 "Auto-updating has been enabled through cron, using a crontab at ${TPUT_RED}${TPUT_BOLD}/etc/cron.d/netdata${TPUT_RESET}"
979 + echo >&2
980 + 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."
981 + echo >&2 "Successful updates will not send an email."
982 + echo >&2
983 + ;;
984 + *)
985 + echo >&2 "Unable to determine what type of auto-update scheduling to use, not enabling auto-updates."
986 + echo >&2
987 + return 1
988 + esac
989
990 return 0
991 }
992
993 disable_netdata_updater() {
978 - crondir="$(_get_crondir)" || return 1
979 - _check_crondir_permissions "${crondir}" || return 1
980 -
994 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)"
995 echo >&2
996
984 - if [ -f "${crondir}/netdata-updater" ]; then
985 - echo >&2 "Removing cron reference: ${crondir}/netdata-updater"
986 - echo >&2
987 - rm -f "${crondir}/netdata-updater"
988 - else
989 - echo >&2 "Did not find any cron entries to remove"
990 - echo >&2
997 + if [ -d /etc/cron.daily ]; then
998 + rm -f /etc/cron.daily/netdata-updater.sh
999 + rm -f /etc/cron.daily/netdata-updater
1000 + fi
1001 +
1002 + if [ -d /etc/periodic/daily ]; then
1003 + rm -f /etc/periodic/daily/netdata-updater.sh
1004 + rm -f /etc/periodic/daily/netdata-updater
1005 + fi
1006 +
1007 + if [ -d /etc/cron.d ]; then
1008 + rm -f /etc/cron.d/netdata-updater
1009 fi
1010
1011 return 0
packaging/installer/netdata-uninstaller.sh
+2
@@ -456,6 +456,8 @@ rm_file /usr/lib/systemd/system/netdata.service
456 rm_file /etc/init.d/netdata
457 rm_file /etc/periodic/daily/netdata-updater
458 rm_file /etc/cron.daily/netdata-updater
459 +rm_file /etc/cron.d/netdata-updater
460 +
461
462 if [ -n "${NETDATA_PREFIX}" ] && [ -d "${NETDATA_PREFIX}" ]; then
463 rm_dir "${NETDATA_PREFIX}"
system/Makefile.am
+3
@@ -12,6 +12,7 @@ CLEANFILES = \
12 netdata-lsb \
13 netdata-freebsd \
14 netdata.plist \
15 + netdata.crontab \
16 $(NULL)
17
18 include $(top_srcdir)/build/subst.inc
@@ -34,6 +35,7 @@ nodist_noinst_DATA = \
35 netdata-lsb \
36 netdata-freebsd \
37 netdata.plist \
38 + netdata.crontab \
39 $(NULL)
40
41 dist_noinst_DATA = \
@@ -47,4 +49,5 @@ dist_noinst_DATA = \
49 netdata-freebsd.in \
50 netdata.plist.in \
51 netdata.conf \
52 + netdata.crontab.in \
53 $(NULL)
system/netdata.crontab.in new
+1
@@ -0,0 +1 @@
1 +2 57 * * * root @pkglibexecdir_POST@/netdata-updater.sh