From 2597f4f0354d8b7b746c8095dbf1c0a6e9c7b872 Mon Sep 17 00:00:00 2001 From: "Austin S. Hemmelgarn" <austin@netdata.cloud> Date: Tue, 14 Mar 2023 11:40:24 -0400 Subject: [PATCH] Add experimental support for running under runit. (#13841) * Add basic runit service script for Netdata. * Integrate runit handling in install-service script. * Add new runit script to .gitignore. * Fix service file installation. We need to ensure the service directory actually gets created, and thus need to use `-D` with the `install` command. * Sync with system restructure. * Fix botched update to Makefile.am. --- .gitignore | 1 + netdata-installer.sh | 2 +- system/Makefile.am | 8 +++++ system/install-service.sh.in | 58 ++++++++++++++++++++++++++++++------ system/runit/run.in | 16 ++++++++++ 5 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 system/runit/run.in diff --git a/.gitignore b/.gitignore index bb59c8dfee..14ba1c61c7 100644 --- a/.gitignore +++ b/.gitignore @@ -123,6 +123,7 @@ system/initd/init.d/netdata system/launchd/netdata.plist system/lsb/init.d/netdata system/openrc/init.d/netdata +system/runit/run system/systemd/netdata.service system/systemd/netdata.service.* system/systemd/netdata-updater.service diff --git a/netdata-installer.sh b/netdata-installer.sh index e928c71092..4ffe2925eb 100755 --- a/netdata-installer.sh +++ b/netdata-installer.sh @@ -1060,7 +1060,7 @@ fi # ----------------------------------------------------------------------------- progress "Fix generated files permissions" -run chmod 755 ./system/*/init.d/netdata ./system/*/rc.d/netdata ./system/install-service.sh +run chmod 755 ./system/*/init.d/netdata ./system/*/rc.d/netdata ./system/runit/run ./system/install-service.sh # ----------------------------------------------------------------------------- progress "Creating standard user and groups for netdata" diff --git a/system/Makefile.am b/system/Makefile.am index b6efe45009..747c6d4e43 100644 --- a/system/Makefile.am +++ b/system/Makefile.am @@ -11,6 +11,7 @@ CLEANFILES = \ lsb/init.d/netdata \ openrc/conf.d/netdata \ openrc/init.d/netdata \ + runit/run \ systemd/netdata.service \ systemd/netdata.service.v235 \ systemd/netdata-updater.service \ @@ -40,6 +41,7 @@ libsyslsbinitddir=$(libsyslsbdir)/init.d libsysopenrcdir=$(libsysdir)/openrc libsysopenrcinitddir=$(libsysopenrcdir)/init.d libsysopenrcconfddir=$(libsysopenrcdir)/conf.d +libsysrunitdir=$(libsysdir)/runit libsyssystemddir=$(libsysdir)/systemd # Explicitly install directories to avoid permission issues due to umask @@ -55,6 +57,7 @@ install-exec-local: $(INSTALL) -d $(DESTDIR)$(libsyssystemddir) $(INSTALL) -d $(DESTDIR)$(libsysopenrcinitddir) $(INSTALL) -d $(DESTDIR)$(libsysopenrcconfddir) + $(INSTALL) -d $(DESTDIR)$(libsysrunitdir) $(INSTALL) -d $(DESTDIR)$(libconfigvnodesdir) dist_libconfigvnodes_DATA = \ @@ -98,6 +101,10 @@ nodist_libsysopenrcconfd_DATA = \ openrc/conf.d/netdata \ $(NULL) +nodist_libsysrunit_DATA = \ + runit/run \ + $(NULL) + nodist_libsyssystemd_DATA = \ systemd/netdata.service \ systemd/netdata.service.v235 \ @@ -119,6 +126,7 @@ dist_noinst_DATA = \ lsb/init.d/netdata.in \ openrc/conf.d/netdata.in \ openrc/init.d/netdata.in \ + runit/run.in \ systemd/netdata.service.in \ systemd/netdata.service.v235.in \ systemd/netdata-updater.service.in \ diff --git a/system/install-service.sh.in b/system/install-service.sh.in index 3784cd1fd9..9a37528c54 100755 --- a/system/install-service.sh.in +++ b/system/install-service.sh.in @@ -87,7 +87,7 @@ get_os_key() { valid_types() { case "${PLATFORM}" in Linux) - echo "detect systemd openrc lsb initd" + echo "detect ${LINUX_INIT_TYPES}" ;; FreeBSD) echo "detect freebsd" @@ -475,8 +475,6 @@ initd_cmds() { # ===================================================================== # runit support functions -# -# Currently not supported, this exists to provide useful error messages. _check_runit() { # if there is no runsvdir command, then it's not runit @@ -503,19 +501,61 @@ check_runit() { } install_runit_service() { - error "Detected runit, which we do not currently support." - exit 3 + if [ -d /etc/sv ]; then + svc_dir="/etc/sv/netdata" + elif [ -d /etc/runit/sv ]; then + svc_dir="/etc/runit/sv/netdata" + else + error "Failed to locate service directory" + exit 4 + fi + + if [ -d /service ]; then + live_svc_dir="/service" + elif [ -d /var/service ]; then + live_svc_dir="/var/service" + elif [ -d /run/runit/service ]; then + live_svc_dir="/run/runit/service" + elif [ -d /etc/runit/runsvdir/default ]; then + live_svc_dir="/etc/runit/runsvdir/default" + else + error "Failed to locate live service directory" + exit 4 + fi + + svc_file="${svc_dir}/run" + + info "Installing runit service file." + if [ ! -f "${svc_file}" ] && [ "${ENABLE}" = "auto" ]; then + ENABLE="enable" + fi + + if ! install -D -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/runit/run" "${svc_file}"; then + error "Failed to install service file." + exit 4 + fi + + case ${ENABLE} in + enable) + if ! ln -s "${svc_dir}" "${live_svc_dir}"; then + warning "Failed to enable the Netdata service." + fi + ;; + disable) + if ! rm "${live_svc_dir}/netdata"; then + warning "Failed to disable the Netdata service." + fi + ;; + esac } runit_cmds() { - error "Detected runit, which we do not currently support." - exit 3 + NETDATA_START_CMD="sv start netdata" + NETDATA_STOP_CMD="sv stop netdata" } # ===================================================================== # WSL support functions -# -# Cannot be supported, this exists to provide useful error messages. _check_wsl() { # If uname -r contains the string WSL, then it's WSL. diff --git a/system/runit/run.in b/system/runit/run.in new file mode 100644 index 0000000000..4ea783cc6c --- /dev/null +++ b/system/runit/run.in @@ -0,0 +1,16 @@ +#!/bin/sh + +piddir="@localstatedir_POST@/run/netdata/netdata.pid" +pidfile="${piddir}/netdata.pid" + +cachedir="@localstatedir_POST@/cache/netdata" + +command="@sbindir_POST@/netdata" +command_args="-P ${pidfile} -D" + +[ ! -d "${piddir}" ] && mkdir -p "${piddir}" +[ ! -d "${cachedir}" ] && mkdir -p "${cachedir}" +chown -R @netdata_user_POST@ "${piddir}" +chown -R @netdata_user_POST@ "${cachedir}" + +exec ${command} ${command_args}