@cryptotaxi247 / netdata-1 / commits / e35130bcf

Added improved auto-update support. (#9966)

* Add systemd timer unit to handle auto updates. This adds a systemd timer unit and associated service for running the updaterscript. This allows better support for auto-updates on systems that use systemd, removing the need for cron or special shims to handle periodic tasks. This will be used in preference to our existing auto-update support on systems running systemd unless the user overrides this behavior. * Add an option to the installer to override auto-updater type detection. This allows users to specify what auto-updater scheduling mechanism to use. This wil persist the selection to updates as well. Supported values are: * systemd': Uses a systemd timer unit and service to handle automatic updates. * 'interval': Uses a script in /etc/cron.daily or /etc/periodic/daily. * `crontab`: Uses a crontab file in /etc/cron.d. * Change priority of systemd support. This way existing users won't get converted, and by default you'll still get emails on failures. * Case-normalize the value passed to --auto-update-type. * Fix incorrect naming of crontab file. * Fixed function naming.

Austin S. Hemmelgarn committed Sep 28, 2020 at 07:07 UTC e35130bcf099a82afbeaac5b807ebee712b4125d
6 files changed +117 -35
netdata-installer.sh
+15 -1
@@ -205,6 +205,8 @@ USAGE: ${PROGRAM} [options]
205 --dont-start-it Do not (re)start netdata after installation
206 --dont-wait Run installation in non-interactive mode
207 --auto-update or -u Install netdata-updater in cron to update netdata automatically once per day
208 + --auto-update-type Override the auto-update scheduling mechanism detection, currently supported types
209 + are: systemd, interval, crontab
210 --stable-channel Use packages from GitHub release pages instead of GCS (nightly updates).
211 This results in less frequent updates.
212 --nightly-channel Use most recent nightly udpates instead of GitHub releases.
@@ -277,6 +279,18 @@ while [ -n "${1}" ]; do
279 "--dont-start-it") DONOTSTART=1 ;;
280 "--dont-wait") DONOTWAIT=1 ;;
281 "--auto-update" | "-u") AUTOUPDATE=1 ;;
282 + "--auto-update-type")
283 + AUTO_UPDATE_TYPE="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
284 + case "${AUTO_UPDATE_TYPE}" in
285 + systemd|interval|crontab)
286 + shift 1
287 + ;;
288 + *)
289 + echo "Unrecognized value for --auto-update-type. Valid values are: systemd, interval, crontab"
290 + exit 1
291 + ;;
292 + esac
293 + ;;
294 "--stable-channel") RELEASE_CHANNEL="stable" ;;
295 "--nightly-channel") RELEASE_CHANNEL="nightly" ;;
296 "--enable-plugin-freeipmi") NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-plugin-freeipmi/} --enable-plugin-freeipmi" ;;
@@ -1796,7 +1810,7 @@ install_netdata_updater || run_failed "Cannot install netdata updater tool."
1810
1811 progress "Check if we must enable/disable the netdata updater tool"
1812 if [ "${AUTOUPDATE}" = "1" ]; then
1799 - enable_netdata_updater || run_failed "Cannot enable netdata updater tool"
1813 + enable_netdata_updater ${AUTO_UPDATE_TYPE} || run_failed "Cannot enable netdata updater tool"
1814 else
1815 disable_netdata_updater || run_failed "Cannot disable netdata updater tool"
1816 fi
packaging/installer/functions.sh
+72 -34
@@ -345,6 +345,19 @@ iscontainer() {
345 return 1
346 }
347
348 +get_os_key() {
349 + if [ -f /etc/os-release ]; then
350 + # shellcheck disable=SC1091
351 + source /etc/os-release || return 1
352 + echo "${ID}-${VERSION_ID}"
353 +
354 + elif [ -f /etc/redhat-release ]; then
355 + echo "$(< /etc/redhat-release)"
356 + else
357 + echo "unknown"
358 + fi
359 +}
360 +
361 issystemd() {
362 local pids p myns ns systemctl
363
@@ -379,18 +392,31 @@ issystemd() {
392 return 1
393 }
394
395 +get_systemd_service_dir() {
396 + local SYSTEMD_DIRECTORY=""
397 + local key
398 + key="$(get_os_key)"
399 +
400 + if [ -w "/lib/systemd/system" ]; then
401 + SYSTEMD_DIRECTORY="/lib/systemd/system"
402 + elif [ -w "/usr/lib/systemd/system" ]; then
403 + SYSTEMD_DIRECTORY="/usr/lib/systemd/system"
404 + elif [ -w "/etc/systemd/system" ]; then
405 + SYSTEMD_DIRECTORY="/etc/systemd/system"
406 + fi
407 +
408 + if [[ ${key} =~ ^devuan* ]] || [ "${key}" = "debian-7" ] || [ "${key}" = "ubuntu-12.04" ] || [ "${key}" = "ubuntu-14.04" ]; then
409 + SYSTEMD_DIRECTORY="/etc/systemd/system"
410 + fi
411 +
412 + echo "${SYSTEMD_DIRECTORY}"
413 +}
414 +
415 install_non_systemd_init() {
416 [ "${UID}" != 0 ] && return 1
417
385 - local key="unknown"
386 - if [ -f /etc/os-release ]; then
387 - # shellcheck disable=SC1091
388 - source /etc/os-release || return 1
389 - key="${ID}-${VERSION_ID}"
390 -
391 - elif [ -f /etc/redhat-release ]; then
392 - key=$(< /etc/redhat-release)
393 - fi
418 + local key
419 + key="$(get_os_key)"
420
421 if [ -d /etc/init.d ] && [ ! -f /etc/init.d/netdata ]; then
422 if [[ ${key} =~ ^(gentoo|alpine).* ]]; then
@@ -438,16 +464,6 @@ install_netdata_service() {
464 local uname
465 uname="$(uname 2> /dev/null)"
466
441 - local key="unknown"
442 - if [ -f /etc/os-release ]; then
443 - # shellcheck disable=SC1091
444 - source /etc/os-release || return 1
445 - key="${ID}-${VERSION_ID}"
446 -
447 - elif [ -f /etc/redhat-release ]; then
448 - key=$(< /etc/redhat-release)
449 - fi
450 -
467 if [ "${UID}" -eq 0 ]; then
468 if [ "${uname}" = "Darwin" ]; then
469
@@ -486,19 +502,7 @@ install_netdata_service() {
502 NETDATA_STOP_CMD="systemctl stop netdata"
503 NETDATA_INSTALLER_START_CMD="${NETDATA_START_CMD}"
504
489 - SYSTEMD_DIRECTORY=""
490 -
491 - if [ -w "/lib/systemd/system" ]; then
492 - SYSTEMD_DIRECTORY="/lib/systemd/system"
493 - elif [ -w "/usr/lib/systemd/system" ]; then
494 - SYSTEMD_DIRECTORY="/usr/lib/systemd/system"
495 - elif [ -w "/etc/systemd/system" ]; then
496 - SYSTEMD_DIRECTORY="/etc/systemd/system"
497 - fi
498 -
499 - if [[ ${key} =~ ^devuan* ]] || [ "${key}" = "debian-7" ] || [ "${key}" = "ubuntu-12.04" ] || [ "${key}" = "ubuntu-14.04" ]; then
500 - SYSTEMD_DIRECTORY="/etc/systemd/system"
501 - fi
505 + SYSTEMD_DIRECTORY="$(get_systemd_service_dir)"
506
507 if [ "${SYSTEMD_DIRECTORY}x" != "x" ]; then
508 ENABLE_NETDATA_IF_PREVIOUSLY_ENABLED="run systemctl enable netdata"
@@ -901,6 +905,8 @@ safe_sha256sum() {
905 _get_scheduler_type() {
906 if _get_intervaldir > /dev/null ; then
907 echo 'interval'
908 + elif issystemd ; then
909 + echo 'systemd'
910 elif [ -d /etc/cron.d ] ; then
911 echo 'crontab'
912 else
@@ -929,6 +935,11 @@ install_netdata_updater() {
935 cat "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
936 fi
937
938 + if issystemd && [ -n "$(get_systemd_service_dir)" ]; then
939 + cat "${NETDATA_SOURCE_DIR}/system/netdata-updater.timer" > "$(get_systemd_service_dir)/netdata-updater.timer"
940 + cat "${NETDATA_SOURCE_DIR}/system/netdata-updater.service" > "$(get_systemd_service_dir)/netdata-updater.service"
941 + fi
942 +
943 sed -i -e "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
944
945 chmod 0755 "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh"
@@ -944,6 +955,12 @@ cleanup_old_netdata_updater() {
955 rm -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh
956 fi
957
958 + if issystemd && [ -n "$(get_systemd_service_dir)" ] ; then
959 + systemctl disable netdata-updater.timer
960 + rm -f "$(get_systemd_service_dir)/netdata-updater.timer"
961 + rm -f "$(get_systemd_service_dir)/netdata-updater.service"
962 + fi
963 +
964 if [ -d /etc/cron.daily ]; then
965 rm -f /etc/cron.daily/netdata-updater.sh
966 rm -f /etc/cron.daily/netdata-updater
@@ -962,7 +979,24 @@ cleanup_old_netdata_updater() {
979 }
980
981 enable_netdata_updater() {
965 - case "$(_get_scheduler_type)" in
982 + local updater_type
983 +
984 + if [ -z "${1}" ] ; then
985 + updater_type="${1}"
986 + else
987 + updater_type="$(_get_scheduler_type)"
988 + fi
989 +
990 + case "${updater_type}" in
991 + "systemd")
992 + systemctl enable netdata-updater.timer
993 +
994 + echo >&2 "Auto-updating has been enabled using a systemd timer unit."
995 + echo >&2
996 + echo >&2 "If the update process fails, the failure will be logged to the systemd journal just like a regular service failure."
997 + echo >&2 "Successful updates should produce empty logs."
998 + echo >&2
999 + ;;
1000 "interval")
1001 ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "$(_get_intervaldir)/netdata-updater"
1002
@@ -975,7 +1009,7 @@ enable_netdata_updater() {
1009 "crontab")
1010 cat "${NETDATA_SOURCE_DIR}/system/netdata.crontab" > "/etc/cron.d/netdata-updater"
1011
978 - echo >&2 "Auto-updating has been enabled through cron, using a crontab at ${TPUT_RED}${TPUT_BOLD}/etc/cron.d/netdata${TPUT_RESET}"
1012 + echo >&2 "Auto-updating has been enabled through cron, using a crontab at ${TPUT_RED}${TPUT_BOLD}/etc/cron.d/netdata-updater${TPUT_RESET}"
1013 echo >&2
1014 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."
1015 echo >&2 "Successful updates will not send an email."
@@ -994,6 +1028,10 @@ disable_netdata_updater() {
1028 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)"
1029 echo >&2
1030
1031 + if issystemd && [ -n "$(get_systemd_service_dir)" ] ; then
1032 + systemctl disable netdata-updater.timer
1033 + fi
1034 +
1035 if [ -d /etc/cron.daily ]; then
1036 rm -f /etc/cron.daily/netdata-updater.sh
1037 rm -f /etc/cron.daily/netdata-updater
packaging/installer/netdata-uninstaller.sh
+6
@@ -453,6 +453,12 @@ rm_file /etc/logrotate.d/netdata
453 rm_file /etc/systemd/system/netdata.service
454 rm_file /lib/systemd/system/netdata.service
455 rm_file /usr/lib/systemd/system/netdata.service
456 +rm_file /etc/systemd/system/netdata-updater.service
457 +rm_file /lib/systemd/system/netdata-updater.service
458 +rm_file /usr/lib/systemd/system/netdata-updater.service
459 +rm_file /etc/systemd/system/netdata-updater.timer
460 +rm_file /lib/systemd/system/netdata-updater.timer
461 +rm_file /usr/lib/systemd/system/netdata-updater.timer
462 rm_file /etc/init.d/netdata
463 rm_file /etc/periodic/daily/netdata-updater
464 rm_file /etc/cron.daily/netdata-updater
system/Makefile.am
+4
@@ -13,6 +13,7 @@ CLEANFILES = \
13 netdata-freebsd \
14 netdata.plist \
15 netdata.crontab \
16 + netdata-updater.service \
17 $(NULL)
18
19 include $(top_srcdir)/build/subst.inc
@@ -36,6 +37,7 @@ nodist_noinst_DATA = \
37 netdata-freebsd \
38 netdata.plist \
39 netdata.crontab \
40 + netdata-updater.service \
41 $(NULL)
42
43 dist_noinst_DATA = \
@@ -50,4 +52,6 @@ dist_noinst_DATA = \
52 netdata.plist.in \
53 netdata.conf \
54 netdata.crontab.in \
55 + netdata-updater.service.in \
56 + netdata-updater.timer \
57 $(NULL)
system/netdata-updater.service.in new
+8
@@ -0,0 +1,8 @@
1 +[Unit]
2 +Description=Daily auto-updates for Netdata
3 +RefuseManualStart=no
4 +RefuseManualStop=yes
5 +
6 +[Service]
7 +Type=oneshot
8 +ExecStart=@pkglibexecdir_POST@/netdata-updater.sh
system/netdata-updater.timer new
+12
@@ -0,0 +1,12 @@
1 +[Unit]
2 +Description=Daily auto-updates for Netdata
3 +RefuseManualStart=no
4 +RefuseManualStop=no
5 +
6 +[Timer]
7 +Persistent=false
8 +OnCalendar=daily
9 +Unit=netdata-updater.service
10 +
11 +[Install]
12 +WantedBy=timers.target