From d93597d0bc479421e8884fadd5e647b3eb458721 Mon Sep 17 00:00:00 2001 From: Konstantinos Natsakis <5933427+knatsakis@users.noreply.github.com> Date: Thu, 17 Oct 2019 16:30:49 +0300 Subject: [PATCH] Partial fix for #7039 (#7060) * Partial fix for #7039 Moved updater installation code from netdata-installer.sh to packaging/installer/functions.sh packaging/makeself/install-or-update.sh uses above code to install netdata updater - Moved updater installation code to packaging/installer/functions.sh - packaging/makeself/install-or-update.sh uses above code to install netdata updater * Split install_or_remove_netdata_updater() function * Improved netdata-updater related message output * Improved variable declaration location in install-or-update.sh --- netdata-installer.sh | 61 +++------------- packaging/installer/functions.sh | 93 +++++++++++++++++++++++++ packaging/makeself/install-or-update.sh | 35 +++++++--- 3 files changed, 127 insertions(+), 62 deletions(-) diff --git a/netdata-installer.sh b/netdata-installer.sh index 4c3aeee71d815d..bf7ac40489788a 100755 --- a/netdata-installer.sh +++ b/netdata-installer.sh @@ -1050,63 +1050,20 @@ END echo >&2 "Uninstall script copied to: ${TPUT_RED}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-uninstaller.sh${TPUT_RESET}" echo >&2 -progress "Install netdata updater tool" - -if [ -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh ]; then - echo >&2 "Removing updater from previous location" - rm -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh -fi +# ----------------------------------------------------------------------------- +progress "Install (but not enable) netdata updater tool" +cleanup_old_netdata_updater || run_failed "Cannot cleanup old netdata updater tool." +install_netdata_updater || run_failed "Cannot install netdata updater tool." -if [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then - sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || exit 1 +progress "Check if we must enable/disable the netdata updater tool" +if [ "${AUTOUPDATE}" = "1" ]; then + enable_netdata_updater || run_failed "Cannot enable netdata updater tool" else - sed "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || exit 1 + disable_netdata_updater || run_failed "Cannot disable netdata updater tool" fi -chmod 0755 ${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh -echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh${TPUT_RESET}" -echo >&2 - -# Figure out the cron directory for the distro -crondir= -[ -d "/etc/periodic/daily" ] && crondir="/etc/periodic/daily" -[ -d "/etc/cron.daily" ] && crondir="/etc/cron.daily" - -if [ -z "${crondir}" ]; then - echo >&2 "Cannot figure out the cron directory to handle netdata-updater.sh activation/deactivation" -elif [ "${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." -else - progress "Check if we must enable/disable the netdata updater" - if [ "${AUTOUPDATE}" = "1" ]; then - if [ -f "${crondir}/netdata-updater.sh" ]; then - progress "Removing incorrect netdata-updater filename in cron" - rm -f "${crondir}/netdata-updater.sh" - fi - - echo >&2 "Adding to cron" - - 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-update${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}" - else - 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" - rm -f "${crondir}/netdata-updater" - else - echo >&2 "Did not find any cron entries to remove" - fi - fi -fi +# ----------------------------------------------------------------------------- progress "Wrap up environment set up" # Save environment variables diff --git a/packaging/installer/functions.sh b/packaging/installer/functions.sh index 2e30443402365e..363998e96cd798 100644 --- a/packaging/installer/functions.sh +++ b/packaging/installer/functions.sh @@ -727,3 +727,96 @@ safe_sha256sum() { fatal "I could not find a suitable checksum binary to use" fi } + +get_crondir() { + crondir= + [ -d "/etc/periodic/daily" ] && crondir="/etc/periodic/daily" + [ -d "/etc/cron.daily" ] && crondir="/etc/cron.daily" + + echo "${crondir}" +} + +check_crondir_permissions() { + if [ -z "${1}" ]; then + echo >&2 "Cannot figure out the cron directory to handle netdata-updater.sh activation/deactivation" + return 1 + elif [ "${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 + + return 0 +} + +install_netdata_updater() { + if [ "${INSTALLER_DIR}" ] && [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then + cat "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1 + fi + + if [ "${NETDATA_SOURCE_DIR}" ] && [ -f "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" ]; then + cat "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1 + fi + + sed -e "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${NETDATA_USER_CONFIG_DIR}/.environment|" -i "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1 + + chmod 0755 ${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh + echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh${TPUT_RESET}" + echo >&2 + + return 0 +} + +cleanup_old_netdata_updater() { + if [ -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh ]; then + echo >&2 "Removing updater from deprecated location" + rm -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh + fi + + crondir="$(get_crondir)" + check_crondir_permissions "${crondir}" || return 1 + + if [ -f "${crondir}/netdata-updater.sh" ]; then + echo >&2 "Removing incorrect netdata-updater filename in cron" + rm -f "${crondir}/netdata-updater.sh" + fi + + return 0 +} + +enable_netdata_updater() { + crondir="$(get_crondir)" + check_crondir_permissions "${crondir}" || return 1 + + echo >&2 "Adding to cron" + + 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-update${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 + + return 0 +} + +disable_netdata_updater() { + crondir="$(get_crondir)" + 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 + fi + + return 0 +} diff --git a/packaging/makeself/install-or-update.sh b/packaging/makeself/install-or-update.sh index 9796eb08513b84..21b7bf2e08beeb 100755 --- a/packaging/makeself/install-or-update.sh +++ b/packaging/makeself/install-or-update.sh @@ -9,6 +9,9 @@ umask 002 # Be nice on production environments renice 19 $$ >/dev/null 2>/dev/null +NETDATA_PREFIX="/opt/netdata" +NETDATA_USER_CONFIG_DIR="${NETDATA_PREFIX}/etc/netdata" + # ----------------------------------------------------------------------------- if [ -d /opt/netdata/etc/netdata.old ]; then progress "Found old etc/netdata directory, reinstating this" @@ -21,16 +24,15 @@ if [ -d /opt/netdata/etc/netdata.old ]; then fi STARTIT=1 - -while [ ! -z "${1}" ] -do - if [ "${1}" = "--dont-start-it" ] - then - STARTIT=0 - else - echo >&2 "Unknown option '${1}'. Ignoring it." - fi - shift +AUTOUPDATE=0 + +while [ "${1}" ]; do + case "${1}" in + "--dont-start-it") STARTIT=0;; + "--auto-update"|"-u") AUTOUPDATE=1;; + *) echo >&2 "Unknown option '${1}'. Ignoring it.";; + esac + shift 1 done deleted_stock_configs=0 @@ -137,6 +139,19 @@ progress "Install netdata at system init" install_netdata_service || run_failed "Cannot install netdata init service." +# ----------------------------------------------------------------------------- +progress "Install (but not enable) netdata updater tool" +cleanup_old_netdata_updater || run_failed "Cannot cleanup old netdata updater tool." +install_netdata_updater || run_failed "Cannot install netdata updater tool." + +progress "Check if we must enable/disable the netdata updater tool" +if [ "${AUTOUPDATE}" = "1" ]; then + enable_netdata_updater || run_failed "Cannot enable netdata updater tool" +else + disable_netdata_updater || run_failed "Cannot disable netdata updater tool" +fi + + # ----------------------------------------------------------------------------- progress "creating quick links"