From 4c20aca875a0784ff807c980a8fae2f7fb7d51a0 Mon Sep 17 00:00:00 2001
From: "Austin S. Hemmelgarn" <austin@netdata.cloud>
Date: Wed, 1 Mar 2023 07:27:32 -0500
Subject: [PATCH] Assorted improvements to OpenRC support. (#14620)

* Add OpenRC conf.d file for Netdata.

* Tidy-up handling of retry schedule.

It should be computed in stop_pre so it gets evaluated after the conf
file, and it also needs to handle variables not being defined properly
(which it currently does not).

* Add a service description and command descriptions.

* Only install `/etc/conf.d/netdata` if it does not already exist.

This prevents overwriting user configuration.
---
 system/Makefile.am              |  8 +++++++
 system/install-service.sh.in    | 11 ++++++++++
 system/openrc/conf.d/netdata.in | 24 +++++++++++++++++++++
 system/openrc/init.d/netdata.in | 38 +++++++++++++--------------------
 4 files changed, 58 insertions(+), 23 deletions(-)
 create mode 100644 system/openrc/conf.d/netdata.in

diff --git a/system/Makefile.am b/system/Makefile.am
index a8836b2b5f..abcd8cd845 100644
--- a/system/Makefile.am
+++ b/system/Makefile.am
@@ -9,6 +9,7 @@ CLEANFILES = \
     launchd/netdata.plist \
     logrotate/netdata \
     lsb/init.d/netdata \
+    openrc/conf.d/netdata \
     openrc/init.d/netdata \
     systemd/netdata.service \
     systemd/netdata.service.v235 \
@@ -37,6 +38,7 @@ libsyslsbdir=$(libsysdir)/lsb
 libsyslsbinitddir=$(libsyslsbdir)/init.d
 libsysopenrcdir=$(libsysdir)/openrc
 libsysopenrcinitddir=$(libsysopenrcdir)/init.d
+libsysopenrcconfddir=$(libsysopenrcdir)/conf.d
 libsyssystemddir=$(libsysdir)/systemd
 
 # Explicitly install directories to avoid permission issues due to umask
@@ -51,6 +53,7 @@ install-exec-local:
 	$(INSTALL) -d $(DESTDIR)$(libsyslsbinitddir)
 	$(INSTALL) -d $(DESTDIR)$(libsyssystemddir)
 	$(INSTALL) -d $(DESTDIR)$(libsysopenrcinitddir)
+	$(INSTALL) -d $(DESTDIR)$(libsysopenrcconfddir)
 
 libexecnetdatadir=$(libexecdir)/netdata
 nodist_libexecnetdata_SCRIPTS = \
@@ -85,6 +88,10 @@ nodist_libsysopenrcinitd_DATA = \
     openrc/init.d/netdata \
     $(NULL)
 
+nodist_libsysopenrcconfd_DATA = \
+    openrc/conf.d/netdata \
+    $(NULL)
+
 nodist_libsyssystemd_DATA = \
     systemd/netdata.service \
     systemd/netdata.service.v235 \
@@ -104,6 +111,7 @@ dist_noinst_DATA = \
     launchd/netdata.plist.in \
     logrotate/netdata.in \
     lsb/init.d/netdata.in \
+    openrc/conf.d/netdata.in \
     openrc/init.d/netdata.in \
     systemd/netdata.service.in \
     systemd/netdata.service.v235.in \
diff --git a/system/install-service.sh.in b/system/install-service.sh.in
index a85f15bd0f..e76ac826e5 100755
--- a/system/install-service.sh.in
+++ b/system/install-service.sh.in
@@ -290,6 +290,9 @@ _check_openrc() {
   # if there is no /etc/init.d, it's not OpenRC
   [ ! -d /etc/init.d ] && echo "NO" && return 0
 
+  # if there is no /etc/conf.d, it's not OpenRC
+  [ ! -d /etc/conf.d ] && echo "NO" && return 0
+
   # if there is no rc-update command, it's not OpenRC
   [ -z "$(command -v rc-update 2>/dev/null || true)" ] && echo "NO" && return 0
 
@@ -342,6 +345,14 @@ disable_openrc() {
 
 install_openrc_service() {
   install_generic_service openrc/init.d OpenRC /etc/init.d/netdata enable_openrc disable_openrc
+
+  if [ ! -f /etc/conf.d/netdata ]; then
+    info "Installing OpenRC configuration file."
+
+    if ! install -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/openrc/conf.d/netdata" "/etc/conf.d/netdata"; then
+        warning "Failed to install configuration file, however the service will still work."
+    fi
+  fi
 }
 
 openrc_cmds() {
diff --git a/system/openrc/conf.d/netdata.in b/system/openrc/conf.d/netdata.in
new file mode 100644
index 0000000000..92f8826080
--- /dev/null
+++ b/system/openrc/conf.d/netdata.in
@@ -0,0 +1,24 @@
+# The user netdata is configured to run as.
+# If you edit its configuration file to set a different user, set it
+# here too, to have its files switch ownership
+NETDATA_OWNER="@netdata_user_POST@:@netdata_user_POST@"
+
+# How long to wait for the agent to save it's database during shutdown.
+NETDATA_WAIT_EXIT_TIMEOUT=60
+
+# If set to 1, force an exit if we time out waiting for the agent to
+# save it's database during shutdown.
+NETDATA_FORCE_EXIT=0
+
+# If set to 1, use netdatacli when sending commands to the agent.
+# This should not be needed in most cases, but it can sometimes help
+# work around issues.
+#NETDATA_USE_NETDATACLI=1
+
+# Specify the path to the pidfile to be used when running in the
+# background.
+NETDATA_PIDFILE="@localstatedir_POST@/run/netdata/netdata.pid"
+
+# Uncomment the below line to run Netdata under OpenRC's native process
+# supervision.
+#supervisor="supervise-daemon"
diff --git a/system/openrc/init.d/netdata.in b/system/openrc/init.d/netdata.in
index 1588789294..74242b3cbd 100644
--- a/system/openrc/init.d/netdata.in
+++ b/system/openrc/init.d/netdata.in
@@ -1,37 +1,21 @@
 #!/sbin/openrc-run
 # SPDX-License-Identifier: GPL-3.0-or-later
 
-# The user netdata is configured to run as.
-# If you edit its configuration file to set a different
-# user, set it here too, to have its files switch ownership
-: "${NETDATA_OWNER:=@netdata_user_POST@:@netdata_user_POST@}"
+NETDATA_OWNER="@netdata_user_POST@:@netdata_user_POST@"
+NETDATA_PIDFILE="@localstatedir_POST@/run/netdata/netdata.pid"
 
-# The timeout in seconds to wait for netdata
-# to save its database on disk and exit.
-: "${NETDATA_WAIT_EXIT_TIMEOUT:=60}"
-
-# When set to 1, if netdata does not exit in
-# NETDATA_WAIT_EXIT_TIMEOUT, we will force it
-# to exit.
-: "${NETDATA_FORCE_EXIT:=0}"
-
-# When set to 1, we use netdatacli for reload/rotate/save commands instead of s-s-d.
-: "${NETDATA_USE_NETDATACLI:=0}"
-
-# Specifies the pidfile to use when running in the background.
-: "${NETDATA_PIDFILE:=@localstatedir_POST@/run/netdata/netdata.pid}"
+description="Run the Netdata system monitoring agent."
 
 extra_started_commands="reload rotate save"
+description_reload="Reload health configuration."
+description_rotate="Reopen log files."
+description_save="Force sync of database to disk."
+
 command_prefix="@sbindir_POST@"
 command="${command_prefix}/netdata"
 command_args="-P ${NETDATA_PIDFILE} ${NETDATA_EXTRA_ARGS}"
 command_args_foreground="-D"
 start_stop_daemon_args="-u ${NETDATA_OWNER}"
-if [ "${NETDATA_FORCE_EXIT}" -eq 1 ]; then
-    retry="TERM/${NETDATA_WAIT_EXIT_TIMEOUT}/KILL/1"
-else
-    retry="TERM/${NETDATA_WAIT_EXIT_TIMEOUT}"
-fi
 
 depend() {
     use logger
@@ -47,6 +31,14 @@ start_pre() {
     fi
 }
 
+stop_pre() {
+    if [ "0${NETDATA_FORCE_EXIT}" -eq 1 ]; then
+        retry="TERM/${NETDATA_WAIT_EXIT_TIMEOUT:-60}/KILL/1"
+    else
+        retry="TERM/${NETDATA_WAIT_EXIT_TIMEOUT:-60}"
+    fi
+}
+
 run_cmd() {
     cmd="${1}"
     msg="${2}"