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
Konstantinos Natsakis committed
Oct 17, 2019 at 16:30 UTC
4b9cc50adc5d3cd0a644ebbd2d5db4ae66e4208e
3 files changed
+127
-62
netdata-installer.sh
+9
-52
@@ -1050,63 +1050,20 @@ END
1050
echo >&2 "Uninstall script copied to: ${TPUT_RED}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-uninstaller.sh${TPUT_RESET}"
1051
echo >&2
1052
1053
-progress "Install netdata updater tool"
1054
-
1055
-if [ -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh ]; then
1056
- echo >&2 "Removing updater from previous location"
1057
- rm -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh
1058
-fi
1053
+# -----------------------------------------------------------------------------
1054
+progress "Install (but not enable) netdata updater tool"
1055
+cleanup_old_netdata_updater || run_failed "Cannot cleanup old netdata updater tool."
1056
+install_netdata_updater || run_failed "Cannot install netdata updater tool."
1057
1060
-if [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then
1061
- 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
1058
+progress "Check if we must enable/disable the netdata updater tool"
1059
+if [ "${AUTOUPDATE}" = "1" ]; then
1060
+ enable_netdata_updater || run_failed "Cannot enable netdata updater tool"
1061
else
1063
- 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
1062
+ disable_netdata_updater || run_failed "Cannot disable netdata updater tool"
1063
fi
1064
1066
-chmod 0755 ${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh
1067
-echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh${TPUT_RESET}"
1068
-echo >&2
1069
-
1070
-# Figure out the cron directory for the distro
1071
-crondir=
1072
-[ -d "/etc/periodic/daily" ] && crondir="/etc/periodic/daily"
1073
-[ -d "/etc/cron.daily" ] && crondir="/etc/cron.daily"
1074
-
1075
-if [ -z "${crondir}" ]; then
1076
- echo >&2 "Cannot figure out the cron directory to handle netdata-updater.sh activation/deactivation"
1077
-elif [ "${UID}" -ne "0" ]; then
1078
- # We cant touch cron if we are not running as root
1079
- echo >&2 "You need to run the installer as root for auto-updating via cron."
1080
-else
1081
- progress "Check if we must enable/disable the netdata updater"
1082
- if [ "${AUTOUPDATE}" = "1" ]; then
1083
- if [ -f "${crondir}/netdata-updater.sh" ]; then
1084
- progress "Removing incorrect netdata-updater filename in cron"
1085
- rm -f "${crondir}/netdata-updater.sh"
1086
- fi
1087
-
1088
- echo >&2 "Adding to cron"
1089
-
1090
- rm -f "${crondir}/netdata-updater"
1091
- ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "${crondir}/netdata-updater"
1092
-
1093
- echo >&2 "Auto-updating has been enabled. Updater script linked to: ${TPUT_RED}${TPUT_BOLD}${crondir}/netdata-update${TPUT_RESET}"
1094
- echo >&2
1095
- echo >&2 "${TPUT_DIM}${TPUT_BOLD}netdata-updater.sh${TPUT_RESET}${TPUT_DIM} works from cron. It will trigger an email from cron"
1096
- echo >&2 "only if it fails (it should not print anything when it can update netdata).${TPUT_RESET}"
1097
- else
1098
- 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)"
1099
- echo >&2
1100
-
1101
- if [ -f "${crondir}/netdata-updater" ]; then
1102
- echo >&2 "Removing cron reference: ${crondir}/netdata-updater"
1103
- rm -f "${crondir}/netdata-updater"
1104
- else
1105
- echo >&2 "Did not find any cron entries to remove"
1106
- fi
1107
- fi
1108
-fi
1065
1066
+# -----------------------------------------------------------------------------
1067
progress "Wrap up environment set up"
1068
1069
# Save environment variables
packaging/installer/functions.sh
+93
@@ -727,3 +727,96 @@ safe_sha256sum() {
727
fatal "I could not find a suitable checksum binary to use"
728
fi
729
}
730
+
731
+get_crondir() {
732
+ crondir=
733
+ [ -d "/etc/periodic/daily" ] && crondir="/etc/periodic/daily"
734
+ [ -d "/etc/cron.daily" ] && crondir="/etc/cron.daily"
735
+
736
+ echo "${crondir}"
737
+}
738
+
739
+check_crondir_permissions() {
740
+ if [ -z "${1}" ]; then
741
+ echo >&2 "Cannot figure out the cron directory to handle netdata-updater.sh activation/deactivation"
742
+ return 1
743
+ elif [ "${UID}" -ne "0" ]; then
744
+ # We cant touch cron if we are not running as root
745
+ echo >&2 "You need to run the installer as root for auto-updating via cron"
746
+ return 1
747
+ fi
748
+
749
+ return 0
750
+}
751
+
752
+install_netdata_updater() {
753
+ if [ "${INSTALLER_DIR}" ] && [ -f "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" ]; then
754
+ cat "${INSTALLER_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
755
+ fi
756
+
757
+ if [ "${NETDATA_SOURCE_DIR}" ] && [ -f "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" ]; then
758
+ cat "${NETDATA_SOURCE_DIR}/packaging/installer/netdata-updater.sh" > "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" || return 1
759
+ fi
760
+
761
+ 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
762
+
763
+ chmod 0755 ${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh
764
+ echo >&2 "Update script is located at ${TPUT_GREEN}${TPUT_BOLD}${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh${TPUT_RESET}"
765
+ echo >&2
766
+
767
+ return 0
768
+}
769
+
770
+cleanup_old_netdata_updater() {
771
+ if [ -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh ]; then
772
+ echo >&2 "Removing updater from deprecated location"
773
+ rm -f "${NETDATA_PREFIX}"/usr/libexec/netdata-updater.sh
774
+ fi
775
+
776
+ crondir="$(get_crondir)"
777
+ check_crondir_permissions "${crondir}" || return 1
778
+
779
+ if [ -f "${crondir}/netdata-updater.sh" ]; then
780
+ echo >&2 "Removing incorrect netdata-updater filename in cron"
781
+ rm -f "${crondir}/netdata-updater.sh"
782
+ fi
783
+
784
+ return 0
785
+}
786
+
787
+enable_netdata_updater() {
788
+ crondir="$(get_crondir)"
789
+ check_crondir_permissions "${crondir}" || return 1
790
+
791
+ echo >&2 "Adding to cron"
792
+
793
+ rm -f "${crondir}/netdata-updater"
794
+ ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "${crondir}/netdata-updater"
795
+
796
+ echo >&2 "Auto-updating has been enabled. Updater script linked to: ${TPUT_RED}${TPUT_BOLD}${crondir}/netdata-update${TPUT_RESET}"
797
+ echo >&2
798
+ echo >&2 "${TPUT_DIM}${TPUT_BOLD}netdata-updater.sh${TPUT_RESET}${TPUT_DIM} works from cron. It will trigger an email from cron"
799
+ echo >&2 "only if it fails (it should not print anything when it can update netdata).${TPUT_RESET}"
800
+ echo >&2
801
+
802
+ return 0
803
+}
804
+
805
+disable_netdata_updater() {
806
+ crondir="$(get_crondir)"
807
+ check_crondir_permissions "${crondir}" || return 1
808
+
809
+ 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)"
810
+ echo >&2
811
+
812
+ if [ -f "${crondir}/netdata-updater" ]; then
813
+ echo >&2 "Removing cron reference: ${crondir}/netdata-updater"
814
+ echo >&2
815
+ rm -f "${crondir}/netdata-updater"
816
+ else
817
+ echo >&2 "Did not find any cron entries to remove"
818
+ echo >&2
819
+ fi
820
+
821
+ return 0
822
+}
packaging/makeself/install-or-update.sh
+25
-10
@@ -9,6 +9,9 @@ umask 002
9
# Be nice on production environments
10
renice 19 $$ >/dev/null 2>/dev/null
11
12
+NETDATA_PREFIX="/opt/netdata"
13
+NETDATA_USER_CONFIG_DIR="${NETDATA_PREFIX}/etc/netdata"
14
+
15
# -----------------------------------------------------------------------------
16
if [ -d /opt/netdata/etc/netdata.old ]; then
17
progress "Found old etc/netdata directory, reinstating this"
@@ -21,16 +24,15 @@ if [ -d /opt/netdata/etc/netdata.old ]; then
24
fi
25
26
STARTIT=1
24
-
25
-while [ ! -z "${1}" ]
26
-do
27
- if [ "${1}" = "--dont-start-it" ]
28
- then
29
- STARTIT=0
30
- else
31
- echo >&2 "Unknown option '${1}'. Ignoring it."
32
- fi
33
- shift
27
+AUTOUPDATE=0
28
+
29
+while [ "${1}" ]; do
30
+ case "${1}" in
31
+ "--dont-start-it") STARTIT=0;;
32
+ "--auto-update"|"-u") AUTOUPDATE=1;;
33
+ *) echo >&2 "Unknown option '${1}'. Ignoring it.";;
34
+ esac
35
+ shift 1
36
done
37
38
deleted_stock_configs=0
@@ -137,6 +139,19 @@ progress "Install netdata at system init"
139
install_netdata_service || run_failed "Cannot install netdata init service."
140
141
142
+# -----------------------------------------------------------------------------
143
+progress "Install (but not enable) netdata updater tool"
144
+cleanup_old_netdata_updater || run_failed "Cannot cleanup old netdata updater tool."
145
+install_netdata_updater || run_failed "Cannot install netdata updater tool."
146
+
147
+progress "Check if we must enable/disable the netdata updater tool"
148
+if [ "${AUTOUPDATE}" = "1" ]; then
149
+ enable_netdata_updater || run_failed "Cannot enable netdata updater tool"
150
+else
151
+ disable_netdata_updater || run_failed "Cannot disable netdata updater tool"
152
+fi
153
+
154
+
155
# -----------------------------------------------------------------------------
156
progress "creating quick links"
157