Add support to the updater to toggle auto-updates on and off. (#12202)
* Add support to the updater to toggle auto-updates on and off. This also adds the updater script to our native packages. * Move argument handling logic to updater enabler function. * Properly handle crontab case.
Austin S. Hemmelgarn committed
Mar 2, 2022 at 09:21 UTC
3dc314432e22086648b515de8bacce19e8cc481e
4 files changed
+228
-24
contrib/debian/rules
+8
@@ -75,6 +75,10 @@ override_dh_install:
75
mkdir -p "$(TOP)/var/cache/netdata"
76
mkdir -p "$(TOP)/var/run/netdata"
77
78
+ # Copy the updater script
79
+ #
80
+ cp -v packaging/installer/netdata-updater.sh $(TOP)/usr/libexec/netdata/netdata-updater.sh
81
+
82
# Move files that local user shouldn't be editing to /usr/share/netdata
83
#
84
mkdir -p "$(TOP)/usr/share/netdata/www"
@@ -108,6 +112,10 @@ override_dh_installdocs:
112
override_dh_fixperms:
113
dh_fixperms
114
115
+ # Updater script should be executable
116
+ #
117
+ chmod 0755 $(TOP)/usr/libexec/netdata/netdata-updater.sh
118
+
119
# apps.plugin should only be runnable by the netdata user. It will be
120
# given extra capabilities in the postinst script.
121
#
netdata.spec.in
+6
@@ -283,6 +283,10 @@ rm -rf "${RPM_BUILD_ROOT}"
283
284
install -m 644 -p system/netdata.conf "${RPM_BUILD_ROOT}%{_sysconfdir}/%{name}"
285
286
+# ###########################################################
287
+# Install updater script
288
+install -m 755 -p packaging/installer/netdata-updater.sh "${RPM_BUILD_ROOT}%{_libexecdir}/%{name}/netdata-updater.sh"
289
+
290
# ###########################################################
291
# logrotate settings
292
install -m 755 -d "${RPM_BUILD_ROOT}%{_sysconfdir}/logrotate.d"
@@ -557,6 +561,8 @@ are sensor monitoring, system event monitoring, power control, and serial-over-L
561
%attr(4750,root,netdata) %{_libexecdir}/%{name}/plugins.d/freeipmi.plugin
562
563
%changelog
564
+* Wed Feb 03 2022 Austin Hemmelgarn <austin@netdata.cloud> 0.0.0-16
565
+- Bundle updater script in native packages.
566
* Mon Oct 11 2021 Austin Hemmelgarn <austin@netdata.cloud> 0.0.0-15
567
- Remove support code for legacy ACLK implementation.
568
* Wed Sep 16 2020 Austin Hemmelgarn <austin@netdata.cloud> 0.0.0-14
packaging/installer/UPDATE.md
+53
-15
@@ -6,8 +6,9 @@ custom_edit_url: https://github.com/netdata/netdata/edit/master/packaging/instal
6
7
# Update the Netdata Agent
8
9
-By default, the Netdata Agent automatically updates with the latest nightly version. If you opted out of automatic
10
-updates, you need to update your Netdata Agent to the latest nightly or stable version.
9
+By default, the Netdata Agent automatically updates with the latest nightly or stable version depending on which
10
+you installed. If you opted out of automatic updates, you need to update your Netdata Agent to the latest nightly
11
+or stable version. You can also [enable or disable automatic updates on an existing install](#control-automatic-updates).
12
13
> 💡 Looking to reinstall the Netdata Agent to enable a feature, update an Agent that cannot update automatically, or
14
> troubleshoot an error during the installation process? See our [reinstallation doc](/packaging/installer/REINSTALL.md)
@@ -20,28 +21,35 @@ icon in the local Agent dashboard's top navigation. This modal informs you wheth
21
22
## Determine which installation method you used
23
24
+Starting with netdata v1.33.0, you can use Netdata itself to determine the installation type by running:
25
+
26
+```bash
27
+netdata -W buildinfo | grep 'Install type:'
28
+```
29
+
30
+If this produces no output, you have an older install and will have to manually look in the Netdata config directory.
31
If you are not sure where your Netdata config directory is, see the [configuration doc](/docs/configure/nodes.md). In
32
most installations, this is `/etc/netdata`.
33
34
Use `cd` to navigate to the Netdata config directory, then use `ls -a` to look for a file called `.install-type`.
35
36
- If the `.install-type` file doex not exist, look for a file in the same directory called `.environment`.
29
- - If the `.environment` file does not exist, you probably installed Netdata using your system package manager
30
- and should update it the same way you would run updates on the system itself.
31
- - If the `.environment` file does exist, then our [regular update method](#updates-for-most-systems) should
32
- work correctly.
37
+ - If the `.environment` file does not exist, then you have a ‘custom’ install.
38
+ - If the `.environment` file does exist, then you have either a ‘static’ or ‘build’ install.
39
- If the `.install-type` file does exist, check it’s contents with `cat .install-type`.
34
- - If the `INSTALL_TYPE` key has a value of `custom`, you probably installed Netdata using your system
35
- package manager and should update it the same way you would run updates on the system itself.
36
- - If the `INSTALL_TYPE` key has a value of `oci`, the install is from a Docker image.
37
- - Otherwise, the install should work with our [regular update method](#updates-for-most-systems).
40
+ - The value of the `INSTALL_TYPE` key indicates what type of install you have.
41
39
-Next, use the appropriate method to update the Netdata Agent:
42
+The exact update method to use depends on the install type:
43
41
-- [Updates for most systems](#updates-for-most-systems)
42
-- [Docker](#docker)
43
-- [macOS](#macos)
44
-- [Manual installation from Git](#manual-installation-from-git)
44
+- Installs with an install type of 'custom' usually indicate installing a third-party package through the system
45
+ package manager. To update these installs, you should update the package just like you would any other package
46
+ on your system.
47
+- Installs with an install type starting with `binpkg` or ending with `build` or `static` can be updated using
48
+ our [regular update method](#updates-for-most-systems).
49
+- Installs with an install type of 'oci' were created from our official Docker images, and should be updated
50
+ using our [Docker](#docker) update procedure.
51
+- macOS users should check [our update instructions for macOS](#macos).
52
+- Manually built installs should check [our update instructions for manual builds](#manual-installation-from-git).
53
54
## Updates for most systems
55
@@ -95,6 +103,9 @@ Homebrew downloads the latest Netdata via the
103
[formulae](https://github.com/Homebrew/homebrew-core/blob/master/Formula/netdata.rb), ensures all dependencies are met,
104
and updates Netdata via reinstallation.
105
106
+If you instead installed Netdata using our one-line installation script, you can use our [regular update
107
+instructions](#updates-for-most-systems) to update Netdata.
108
+
109
## Manual installation from Git
110
111
If you installed [Netdata manually from Git](/packaging/installer/methods/manual.md), you can run that installer again
@@ -117,4 +128,31 @@ sudo ./netdata-installer.sh
128
> ⚠️ If you installed Netdata with any optional parameters, such as `--no-updates` to disable automatic updates, and
129
> want to retain those settings, you need to set them again during this process.
130
131
+## Control automatic updates
132
+
133
+Starting with Netdata v1.34.0, you can easily enable or disable automatic updates on an existing installation
134
+using the updater script.
135
+
136
+For most installs on Linux, you can enable auto-updates with:
137
+
138
+```bash
139
+/usr/libexec/netdata/netdata-updater.sh --enable-auto-updates
140
+```
141
+
142
+and disable them with:
143
144
+```bash
145
+/usr/libexec/netdata/netdata-updater.sh --disable-auto-updates
146
+```
147
+
148
+For static installs, instead use:
149
+
150
+```bash
151
+/opt/netdata/usr/libexec/netdata/netdata-updater.sh --enable-auto-updates
152
+```
153
+
154
+and:
155
+
156
+```bash
157
+/opt/netdata/usr/libexec/netdata/netdata-updater.sh --disable-auto-updates
158
+```
packaging/installer/netdata-updater.sh
+161
-9
@@ -73,6 +73,152 @@ if [ "${ENVIRONMENT_FILE}" = "THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT" ]; th
73
fi
74
fi
75
76
+issystemd() {
77
+ # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
78
+ if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
79
+ return 1
80
+ fi
81
+
82
+ # if there is no systemctl command, it is not systemd
83
+ systemctl=$(command -v systemctl 2> /dev/null)
84
+ if [ -z "${systemctl}" ] || [ ! -x "${systemctl}" ]; then
85
+ return 1
86
+ fi
87
+
88
+ # if pid 1 is systemd, it is systemd
89
+ [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
90
+
91
+ # if systemd is not running, it is not systemd
92
+ pids=$(safe_pidof systemd 2> /dev/null)
93
+ [ -z "${pids}" ] && return 1
94
+
95
+ # check if the running systemd processes are not in our namespace
96
+ myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
97
+ for p in ${pids}; do
98
+ ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
99
+
100
+ # if pid of systemd is in our namespace, it is systemd
101
+ [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && return 0
102
+ done
103
+
104
+ # else, it is not systemd
105
+ return 1
106
+}
107
+
108
+_get_scheduler_type() {
109
+ if _get_intervaldir > /dev/null ; then
110
+ echo 'interval'
111
+ elif issystemd ; then
112
+ echo 'systemd'
113
+ elif [ -d /etc/cron.d ] ; then
114
+ echo 'crontab'
115
+ else
116
+ echo 'none'
117
+ fi
118
+}
119
+
120
+_get_intervaldir() {
121
+ if [ -d /etc/cron.daily ]; then
122
+ echo /etc/cron.daily
123
+ elif [ -d /etc/periodic/daily ]; then
124
+ echo /etc/periodic/daily
125
+ else
126
+ return 1
127
+ fi
128
+
129
+ return 0
130
+}
131
+
132
+enable_netdata_updater() {
133
+ updater_type="$(echo "${1}" | tr '[:upper:]' '[:lower:]')"
134
+ case "${updater_type}" in
135
+ systemd|interval|crontab)
136
+ updater_type="${1}"
137
+ ;;
138
+ "")
139
+ updater_type="$(_get_scheduler_type)"
140
+ ;;
141
+ *)
142
+ error "Unrecognized updater type ${updater_type} requested. Supported types are 'systemd', 'interval', and 'crontab'."
143
+ exit 1
144
+ ;;
145
+ esac
146
+
147
+ case "${updater_type}" in
148
+ "systemd")
149
+ if issystemd; then
150
+ systemctl enable netdata-updater.timer
151
+
152
+ info "Auto-updating has been ENABLED using a systemd timer unit.\n"
153
+ info "If the update process fails, the failure will be logged to the systemd journal just like a regular service failure."
154
+ info "Successful updates should produce empty logs."
155
+ else
156
+ error "Systemd-based auto-update scheduling requested, but this does not appear to be a systemd system."
157
+ error "Auto-updates have NOT been enabled."
158
+ return 1
159
+ fi
160
+ ;;
161
+ "interval")
162
+ if _get_intervaldir > /dev/null; then
163
+ ln -sf "${NETDATA_PREFIX}/usr/libexec/netdata/netdata-updater.sh" "$(_get_intervaldir)/netdata-updater"
164
+
165
+ info "Auto-updating has been ENABLED through cron, updater script linked to $(_get_intervaldir)/netdata-updater\n"
166
+ info "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."
167
+ info "Successful updates will not send an email."
168
+ else
169
+ error "Interval-based auto-update scheduling requested, but I could not find an interval scheduling directory."
170
+ error "Auto-updates have NOT been enabled."
171
+ return 1
172
+ fi
173
+ ;;
174
+ "crontab")
175
+ if [ -d "/etc/cron.d" ]; then
176
+ cat > "/etc/cron.d/netdata-updater" <<-EOF
177
+ 2 57 * * * root ${NETDATA_PREFIX}/netdata-updater.sh
178
+ EOF
179
+
180
+ info "Auto-updating has been ENABLED through cron, using a crontab at /etc/cron.d/netdata-updater\n"
181
+ info "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."
182
+ info "Successful updates will not send an email."
183
+ else
184
+ error "Crontab-based auto-update scheduling requested, but there is no '/etc/cron.d'."
185
+ error "Auto-updates have NOT been enabled."
186
+ return 1
187
+ fi
188
+ ;;
189
+ *)
190
+ error "Unable to determine what type of auto-update scheduling to use."
191
+ error "Auto-updates have NOT been enabled."
192
+ return 1
193
+ esac
194
+
195
+ return 0
196
+}
197
+
198
+disable_netdata_updater() {
199
+ if issystemd && ( systemctl list-units --full -all | grep -Fq "netdata-updater.timer" ) ; then
200
+ systemctl disable netdata-updater.timer
201
+ fi
202
+
203
+ if [ -d /etc/cron.daily ]; then
204
+ rm -f /etc/cron.daily/netdata-updater.sh
205
+ rm -f /etc/cron.daily/netdata-updater
206
+ fi
207
+
208
+ if [ -d /etc/periodic/daily ]; then
209
+ rm -f /etc/periodic/daily/netdata-updater.sh
210
+ rm -f /etc/periodic/daily/netdata-updater
211
+ fi
212
+
213
+ if [ -d /etc/cron.d ]; then
214
+ rm -f /etc/cron.d/netdata-updater
215
+ fi
216
+
217
+ info "Auto-updates have been DISABLED."
218
+
219
+ return 0
220
+}
221
+
222
str_in_list() {
223
printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
224
return $?
@@ -574,6 +720,14 @@ ndtmpdir=
720
721
trap cleanup EXIT
722
723
+# shellcheck source=/dev/null
724
+. "${ENVIRONMENT_FILE}" || exit 1
725
+
726
+if [ -f "$(dirname "${ENVIRONMENT_FILE}")/.install-type" ]; then
727
+ # shellcheck source=/dev/null
728
+ . "$(dirname "${ENVIRONMENT_FILE}")/.install-type" || exit 1
729
+fi
730
+
731
while [ -n "${1}" ]; do
732
if [ "${1}" = "--not-running-from-cron" ]; then
733
NETDATA_NOT_RUNNING_FROM_CRON=1
@@ -584,6 +738,12 @@ while [ -n "${1}" ]; do
738
elif [ "${1}" = "--tmpdir-path" ]; then
739
NETDATA_TMPDIR_PATH="${2}"
740
shift 2
741
+ elif [ "${1}" = "--enable-auto-updates" ]; then
742
+ enable_netdata_updater "${2}"
743
+ exit $?
744
+ elif [ "${1}" = "--disable-auto-updates" ]; then
745
+ disable_netdata_updater
746
+ exit $?
747
else
748
break
749
fi
@@ -601,14 +761,6 @@ if [ ! -t 1 ] && [ -z "${NETDATA_NOT_RUNNING_FROM_CRON}" ]; then
761
sleep $(((rnd % 3600) + 1))
762
fi
763
604
-# shellcheck source=/dev/null
605
-. "${ENVIRONMENT_FILE}" || exit 1
606
-
607
-if [ -f "$(dirname "${ENVIRONMENT_FILE}")/.install-type" ]; then
608
- # shellcheck source=/dev/null
609
- . "$(dirname "${ENVIRONMENT_FILE}")/.install-type" || exit 1
610
-fi
611
-
764
# We dont expect to find lib dir variable on older installations, so load this path if none found
765
export NETDATA_LIB_DIR="${NETDATA_LIB_DIR:-${NETDATA_PREFIX}/var/lib/netdata}"
766
@@ -653,7 +805,7 @@ case "${INSTALL_TYPE}" in
805
update_legacy
806
;;
807
custom)
656
- # At this point, we _should_ have a valid `.environment` file, but it0s best to just check.
808
+ # At this point, we _should_ have a valid `.environment` file, but it's best to just check.
809
# If we do, then behave like the legacy updater.
810
if [ -n "${RELEASE_CHANNEL}" ] && [ -n "${NETDATA_PREFIX}" ] && [ -n "${REINSTALL_OPTIONS}" ]; then
811
update_legacy