Skip to content

Commit

Permalink
Partial fix for netdata#7039 (netdata#7060)
Browse files Browse the repository at this point in the history
* Partial fix for netdata#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
  • Loading branch information
knatsakis authored and Saruspete committed May 21, 2020
1 parent 842cee2 commit d93597d
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 62 deletions.
61 changes: 9 additions & 52 deletions netdata-installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions packaging/installer/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
35 changes: 25 additions & 10 deletions packaging/makeself/install-or-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit d93597d

Please sign in to comment.