@cryptotaxi247 / netdata-1 / commits / 2a9c0b54b

Fix how we are handling system services in RPM packages. (#14781)

* Drop non-systemd init code in RPM spec file. All of our currently supported RPM-based platforms are systemd-based, so this is functionally dead code. * Fix how we handle systemd service installation. - Rely on the platform-provided macros, instead of doing our own thing. These do the right thing in 99.99% of cases, and behave how we actually need them to. - Don’t depend on systemd at runtime. The macros handle this correctly, and nothing we actually do _needs_ systemd at runtime. - Actually follow packaging guidelines for upgrades. In particular, only restart the agent if it’s already running (instead of unconditionally), and don’t enable it if it’s disabled. - Just put the macro invocations down in the actual scriptlets, instead of using complicated nested macro definitions to compute things ahead of time. This makes the code _much_ easier to understand. * Add a systemd preset file so the service is enabled by default.

Austin S. Hemmelgarn committed Mar 27, 2023 at 09:19 UTC 2a9c0b54b1759c31c99669498cabc5606ecea193
4 files changed +42 -75
netdata.spec.in
+29 -72
@@ -34,73 +34,21 @@ AutoReqProv: yes
34 %define _libexecdir /usr/libexec
35 %define _libdir /usr/lib
36
37 +# Fedora doesn’t define this, but other distros do
38 +%{!?_presetdir:%global _presetdir %{_libdir}/systemd/system-preset}
39 +
40 # Redefine centos_ver to standardize on a single macro
41 %{?rhel:%global centos_ver %rhel}
42
43 #
44 # Conditional build:
42 -%bcond_without systemd # systemd
45 %bcond_without netns # build with netns support (cgroup-network)
46
47 %if 0%{?fedora} || 0%{?rhel} >= 7 || 0%{?suse_version} >= 1140
48 %else
47 -%undefine with_systemd
49 %undefine with_netns
50 %endif
51
51 -%if %{with systemd}
52 -%if 0%{?suse_version}
53 -%global netdata_initd_buildrequires \
54 -BuildRequires: systemd-rpm-macros \
55 -%{nil}
56 -%global netdata_initd_requires \
57 -%{?systemd_requires} \
58 -%{nil}
59 -%global netdata_init_post %service_add_post netdata.service \
60 -/sbin/service netdata restart > /dev/null 2>&1 \
61 -%{nil}
62 -%global netdata_init_preun %service_del_preun netdata.service \
63 -/sbin/service netdata stop > /dev/null 2>&1 \
64 -%{nil}
65 -%global netdata_init_postun %service_del_postun netdata.service
66 -%else
67 -%global netdata_initd_buildrequires \
68 -BuildRequires: systemd
69 -%global netdata_initd_requires \
70 -Requires(preun): systemd-units \
71 -Requires(postun): systemd-units \
72 -Requires(post): systemd-units \
73 -%{nil}
74 -%global netdata_init_post %systemd_post netdata.service \
75 -/usr/bin/systemctl enable netdata.service \
76 -/usr/bin/systemctl daemon-reload \
77 -/usr/bin/systemctl restart netdata.service \
78 -%{nil}
79 -%global netdata_init_preun %systemd_preun netdata.service
80 -%global netdata_init_postun %systemd_postun_with_restart netdata.service
81 -%endif
82 -%else
83 -%global netdata_initd_buildrequires %{nil}
84 -%global netdata_initd_requires \
85 -Requires(post): chkconfig \
86 -%{nil}
87 -%global netdata_init_post \
88 -/sbin/chkconfig --add netdata \
89 -/sbin/service netdata restart > /dev/null 2>&1 \
90 -%{nil}
91 -%global netdata_init_preun %{nil} \
92 -if [ $1 = 0 ]; then \
93 - /sbin/service netdata stop > /dev/null 2>&1 \
94 - /sbin/chkconfig --del netdata \
95 -fi \
96 -%{nil}
97 -%global netdata_init_postun %{nil} \
98 -if [ $1 != 0 ]; then \
99 - /sbin/service netdata condrestart 2>&1 > /dev/null \
100 -fi \
101 -%{nil}
102 -%endif
103 -
52 Summary: Real-time performance monitoring, done right!
53 Name: netdata
54 Version: %{version}
@@ -165,7 +113,11 @@ BuildRequires: libyaml-devel
113 %endif
114
115 # Core build requirements for service install
168 -%{netdata_initd_buildrequires}
116 +%if 0%{?suse_version}
117 +BuildRequires: systemd-rpm-macros
118 +%else
119 +BuildRequires: systemd
120 +%endif
121
122 # Runtime dependencies
123 #
@@ -183,8 +135,6 @@ Requires: python3
135 Requires(pre): /usr/sbin/groupadd
136 Requires(pre): /usr/sbin/useradd
137
186 -%{netdata_initd_requires}
187 -
138 # #####################################################################
139 # Functionality-dependent package dependencies
140 # #####################################################################
@@ -341,15 +291,10 @@ install -m 755 -d "${RPM_BUILD_ROOT}%{_localstatedir}/lib/%{name}/registry"
291
292 # ###########################################################
293 # Install netdata service
344 -%if %{with systemd}
294 install -m 755 -d "${RPM_BUILD_ROOT}%{_unitdir}"
295 install -m 644 -p system/systemd/netdata.service "${RPM_BUILD_ROOT}%{_unitdir}/netdata.service"
347 -%else
348 -# install SYSV init stuff
349 -install -d "${RPM_BUILD_ROOT}/etc/rc.d/init.d"
350 -install -m 755 system/initd/init.d/netdata \
351 - "${RPM_BUILD_ROOT}/etc/rc.d/init.d/netdata"
352 -%endif
296 +install -m 755 -d "${RPM_BUILD_ROOT}%{_presetdir}"
297 +install -m 644 -p system/systemd/50-netdata.preset "${RPM_BUILD_ROOT}%{_presetdir}/50-netdata.preset"
298
299 # ############################################################
300 # Package Go within netdata (TBD: Package it separately)
@@ -467,13 +412,25 @@ for item in docker nginx varnish haproxy adm nsd proxy squid ceph nobody I2C; do
412 done
413
414 %post
470 -%{netdata_init_post}
415 +%if 0%{?suse_version}
416 +%service_add_post netdata.service
417 +%else
418 +%systemd_post netdata.service
419 +%endif
420
421 %preun
473 -%{netdata_init_preun}
422 +%if 0%{?suse_version}
423 +%service_del_preun netdata.service
424 +%else
425 +%systemd_preun netdata.service
426 +%endif
427
428 %postun
476 -%{netdata_init_postun}
429 +%if 0%{?suse_version}
430 +%service_del_postun netdata.service
431 +%else
432 +%systemd_postun_with_restart netdata.service
433 +%endif
434
435 %clean
436 rm -rf "${RPM_BUILD_ROOT}"
@@ -492,11 +449,8 @@ rm -rf "${RPM_BUILD_ROOT}"
449 %{_sbindir}/netdatacli
450 %{_sbindir}/netdata-claim.sh
451
495 -%if %{with systemd}
452 %{_unitdir}/netdata.service
497 -%else
498 -%{_sysconfdir}/rc.d/init.d/netdata
499 -%endif
453 +%{_presetdir}/50-netdata.preset
454
455 %defattr(0750,root,netdata,0750)
456
@@ -585,6 +539,9 @@ are sensor monitoring, system event monitoring, power control, and serial-over-L
539 %attr(4750,root,netdata) %{_libexecdir}/%{name}/plugins.d/freeipmi.plugin
540
541 %changelog
542 +* Tue Mar 21 2023 Austin Hemmelgarn <austin@netdata.cloud> 0.0.0-18
543 +- Fix systemd handling to follow BCP.
544 +- Drop pre-systemd init support.
545 * Thu Feb 16 2023 Konstantin Shalygin <k0ste@k0ste.ru> 0.0.0-17
546 - Added eBPF build dependency
547 * Fri Feb 03 2022 Austin Hemmelgarn <austin@netdata.cloud> 0.0.0-16
system/Makefile.am
+1
@@ -113,6 +113,7 @@ nodist_libsyssystemd_DATA = \
113
114 dist_libsyssystemd_DATA = \
115 systemd/netdata-updater.timer \
116 + systemd/50-netdata.preset
117 $(NULL)
118
119 dist_noinst_DATA = \
system/install-service.sh.in
+11 -3
@@ -237,6 +237,8 @@ get_systemd_service_dir() {
237
238 install_systemd_service() {
239 SRCFILE="${SVC_SOURCE}/systemd/netdata.service"
240 + PRESET_FILE="${SVC_SOURCE}/systemd/50-netdata.preset"
241 + SVCDIR="$(get_systemd_service_dir)"
242
243 if [ "$(systemctl --version | head -n 1 | cut -f 2 -d ' ')" -le 235 ]; then
244 SRCFILE="${SVC_SOURCE}/systemd/netdata.service.v235"
@@ -255,18 +257,24 @@ install_systemd_service() {
257 fi
258
259 info "Installing systemd service..."
258 - if ! install -p -m 0644 -o 0 -g 0 "${SRCFILE}" "$(get_systemd_service_dir)/netdata.service"; then
260 + if ! install -p -m 0644 -o 0 -g 0 "${SRCFILE}" "${SVCDIR}/netdata.service"; then
261 error "Failed to install systemd service file."
262 exit 4
263 fi
264
265 + if [ -f "${PRESET_FILE}" ]; then
266 + if ! install -p -m 0644 -o 0 -g 0 "${PRESET_FILE}" "${SVCDIR}-preset/50-netdata.preset"; then
267 + warning "Failed to install netdata preset file."
268 + fi
269 + fi
270 +
271 if [ "$(check_systemd)" = "YES" ]; then
272 if ! systemctl daemon-reload; then
265 - warning "Failed to reload systemd unit files."
273 + warning "Failed to reload systemd unit files."
274 fi
275
276 if ! systemctl "${ENABLE}" netdata; then
269 - warning "Failed to ${ENABLE} Netdata service."
277 + warning "Failed to ${ENABLE} Netdata service."
278 fi
279 fi
280 }
system/systemd/50-netdata.preset new
+1
@@ -0,0 +1 @@
1 +enable netdata.service