From 0cca75731c08abf11d0b9b24d75de26275633e6d Mon Sep 17 00:00:00 2001 From: Thomas Oettli Date: Thu, 5 Nov 2020 12:03:21 +0100 Subject: [PATCH] add openrc to the list of supported init system --- pyinotifyd/__init__.py | 10 ++ pyinotifyd/_install.py | 112 +++++++++++------- pyinotifyd/misc/openrc/pyinotifyd.confd | 8 ++ pyinotifyd/misc/openrc/pyinotifyd.initd | 46 +++++++ .../misc/{ => systemd}/pyinotifyd.service | 0 5 files changed, 132 insertions(+), 44 deletions(-) create mode 100644 pyinotifyd/misc/openrc/pyinotifyd.confd create mode 100755 pyinotifyd/misc/openrc/pyinotifyd.initd rename pyinotifyd/misc/{ => systemd}/pyinotifyd.service (100%) diff --git a/pyinotifyd/__init__.py b/pyinotifyd/__init__.py index b19af0a..e6831ab 100755 --- a/pyinotifyd/__init__.py +++ b/pyinotifyd/__init__.py @@ -135,6 +135,12 @@ def main(): "--uninstall", help="uninstall systemd service file", action="store_true") + exclusive.add_argument( + "-t", + "--configtest", + help="test config and exit", + action="store_true") + args = parser.parse_args() if args.version: @@ -179,6 +185,10 @@ def main(): logging.exception(f"config file '{args.config}': {e}") sys.exit(1) + if args.configtest: + logging.info("config file ok") + sys.exit(0) + if args.debug: root_logger.setLevel(loglevel) formatter = logging.Formatter( diff --git a/pyinotifyd/_install.py b/pyinotifyd/_install.py index 0a6ff94..dacfefc 100755 --- a/pyinotifyd/_install.py +++ b/pyinotifyd/_install.py @@ -20,6 +20,7 @@ import shutil import sys SYSTEMD_PATH = "/lib/systemd/system" +OPENRC = "/sbin/openrc" def _check_root(): @@ -31,7 +32,50 @@ def _check_root(): def _check_systemd(): - return os.path.isdir(SYSTEMD_PATH) + systemd = os.path.isdir(SYSTEMD_PATH) + if systemd: + logging.info("systemd detected") + + return systemd + + +def _check_openrc(): + openrc = os.path.isfile(OPENRC) and os.access(OPENRC, os.X_OK) + if openrc: + logging.info("openrc detected") + + return openrc + + +def _copy_missing_file(src, dst): + if os.path.exists(dst): + logging.info(f"=> file {dst} already installed") + else: + try: + logging.info(f"=> install file {dst}") + shutil.copy2(src, dst) + except Exception as e: + logging.error(f"=> unable to install file {dst}: {e}") + + +def _delete_present_file(f): + if os.path.isfile(f): + try: + logging.info(f"=> uninstall file {f}") + os.remove(f) + except Exception as e: + logging.error(f"=> unable to uninstall file {f}: {e}") + + +def _warn_exists(path): + if os.path.isdir(path): + logging.warning( + f"=> directory {path} is still present, " + f"you have to remove it manually") + else: + logging.warning( + f"=> file {path} is still present, " + f"you have to remove it manually") def install(name): @@ -40,44 +84,34 @@ def install(name): pkg_dir = os.path.dirname(__file__) - if not _check_systemd(): - logging.warning( - "systemd service file will not be installed," - "because systemd is not installed") - else: + if _check_systemd(): dst = f"{SYSTEMD_PATH}/{name}.service" - src = f"{pkg_dir}/misc/{name}.service" - try: - shutil.copy2(src, dst) - except Exception as e: - logging.error(f"unable to copy systemd service file: {e}") - else: - logging.info("systemd service file installed") + src = f"{pkg_dir}/misc/systemd/{name}.service" + _copy_missing_file(src, dst) + if _check_openrc(): + files = [ + (f"{pkg_dir}/misc/openrc/{name}.initd", f"/etc/init.d/{name}"), + (f"{pkg_dir}/misc/openrc/{name}.confd", f"/etc/conf.d/{name}")] + for src, dst in files: + _copy_missing_file(src, dst) + + logging.info("install configuration file") config_dir = f"/etc/{name}" if os.path.isdir(config_dir): - logging.info(f"config dir {config_dir} exists") + logging.info(f"=> directory {config_dir} exists already") else: try: + logging.info(f"=> create directory {config_dir}") os.mkdir(config_dir) except Exception as e: - logging.error(f"unable to create config dir {config_dir}: {e}") + logging.error(f"=> unable to create directory {config_dir}: {e}") sys.exit(3) - else: - logging.info(f"config dir {config_dir} created") - dst = f"{config_dir}/config.py" - src = f"{pkg_dir}/docs/config.py.example" - if os.path.exists(dst): - logging.info(f"config file {dst} exists") - else: - try: - shutil.copy2(src, dst) - except Exception as e: - logging.error(f"unable to copy config file to {dst}: {e}") - sys.exit(4) - else: - logging.info(f"example config file copied to {dst}") + files = [ + (f"{pkg_dir}/docs/config.py.example", f"{config_dir}/config.py")] + for src, dst in files: + _copy_missing_file(src, dst) logging.info(f"{name} successfully installed") @@ -87,22 +121,12 @@ def uninstall(name): sys.exit(2) if _check_systemd(): - path = f"{SYSTEMD_PATH}/{name}.service" + _delete_present_file(f"{SYSTEMD_PATH}/{name}.service") - if not os.path.exists(path): - logging.info("systemd service is not installed") - else: - try: - os.remove(path) - except Exception as e: - logging.error(f"unable to delete: {e}") - sys.exit(3) - else: - logging.info("systemd service file uninstalled") + if _check_openrc(): + _delete_present_file(f"/etc/init.d/{name}") + _warn_exists(f"/etc/conf.d/{name}") - config_dir = f"/etc/{name}" - if os.path.isdir(config_dir): - logging.warning( - f"config dir {config_dir} still exists, please delete manually") + _warn_exists(f"/etc/{name}") logging.info(f"{name} successfully uninstalled") diff --git a/pyinotifyd/misc/openrc/pyinotifyd.confd b/pyinotifyd/misc/openrc/pyinotifyd.confd new file mode 100644 index 0000000..ac23c46 --- /dev/null +++ b/pyinotifyd/misc/openrc/pyinotifyd.confd @@ -0,0 +1,8 @@ +# /etc/conf.d/pyinotifyd: config file for /etc/init.d/pyinotifyd + +# Start the daemon as the user. You can optionally append a group name here also. +# USER="daemon" +# USER="daemon:nobody" + +# Optional parameters for pymodmilter +# PYINOTIFYD_OPTS="" diff --git a/pyinotifyd/misc/openrc/pyinotifyd.initd b/pyinotifyd/misc/openrc/pyinotifyd.initd new file mode 100755 index 0000000..42dc372 --- /dev/null +++ b/pyinotifyd/misc/openrc/pyinotifyd.initd @@ -0,0 +1,46 @@ +#!/sbin/openrc-run + +user=${USER:-root} +pyinotifyd_opts="${PYINOTIFYD_OPTS:-}" + +pidfile="/run/${RC_SVCNAME}.pid" +command="/usr/bin/pyinotifyd" +command_args="${pyinotifyd_opts}" +command_background=true +start_stop_daemon_args="--user ${user}" + +extra_commands="configtest" + +depend() { + need net + before mta +} + +checkconfig() { + output=$(${command} ${command_args} -t 2>&1) + ret=$? + if [ $ret -ne 0 ]; then + eerror "${SVCNAME} has detected an error in your configuration:" + printf "%s\n" "${output}" + fi + + return $ret +} + +configtest() { + ebegin "Checking ${SVCNAME} configuration" + checkconfig + eend $? +} + +start_pre() { + if [ "${RC_CMD}" != "restart" ]; then + checkconfig || return $? + fi +} + +stop_pre() { + if [ "${RC_CMD}" == "restart" ]; then + checkconfig || return $? + fi +} diff --git a/pyinotifyd/misc/pyinotifyd.service b/pyinotifyd/misc/systemd/pyinotifyd.service similarity index 100% rename from pyinotifyd/misc/pyinotifyd.service rename to pyinotifyd/misc/systemd/pyinotifyd.service