From 73b6e8fa0bb5a740d0dff30e85617da9ad77f05d Mon Sep 17 00:00:00 2001 From: Thomas Oettli Date: Thu, 5 Nov 2020 01:03:07 +0100 Subject: [PATCH] add install and uninstall commands for systemd service --- MANIFEST.in | 4 +- {pyinotifyd/docs => docs}/config.py.example | 0 {pyinotifyd/misc => misc}/pyinotifyd.service | 0 pyinotifyd/__init__.py | 37 +++++++-- pyinotifyd/install.py | 82 ++++++++++++++++++++ 5 files changed, 113 insertions(+), 10 deletions(-) rename {pyinotifyd/docs => docs}/config.py.example (100%) rename {pyinotifyd/misc => misc}/pyinotifyd.service (100%) create mode 100755 pyinotifyd/install.py diff --git a/MANIFEST.in b/MANIFEST.in index 84821af..ca4758c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,3 @@ include LICENSE README.md -recursive-include pyinotifyd/docs * -recursive-include pyinotifyd/misc * +recursive-include docs * +recursive-include misc * diff --git a/pyinotifyd/docs/config.py.example b/docs/config.py.example similarity index 100% rename from pyinotifyd/docs/config.py.example rename to docs/config.py.example diff --git a/pyinotifyd/misc/pyinotifyd.service b/misc/pyinotifyd.service similarity index 100% rename from pyinotifyd/misc/pyinotifyd.service rename to misc/pyinotifyd.service diff --git a/pyinotifyd/__init__.py b/pyinotifyd/__init__.py index ec25cfc..f616465 100755 --- a/pyinotifyd/__init__.py +++ b/pyinotifyd/__init__.py @@ -30,6 +30,8 @@ import sys from pyinotifyd.version import __version__ as version from pyinotifyd.watch import Watch, EventMap +from pyinotifyd.install import install_systemd_service +from pyinotifyd.install import uninstall_systemd_service class Pyinotifyd: @@ -110,29 +112,42 @@ def main(): parser.add_argument( "-c", "--config", - help=f"path to config file (defaults to /etc/{myname}/config.py)", + help=f"path to config file (default: /etc/{myname}/config.py)", default=f"/etc/{myname}/config.py") parser.add_argument( "-d", "--debug", help="log debugging messages", action="store_true") - parser.add_argument( - "-e", - "--events", - help="show event types and exit", + + exclusive = parser.add_mutually_exclusive_group() + exclusive.add_argument( + "-l", + "--list", + help="show all usable event types and exit", action="store_true") - parser.add_argument( + exclusive.add_argument( "-v", "--version", help="show version and exit", action="store_true") + exclusive.add_argument( + "-i", + "--install", + help="install systemd service file", + action="store_true") + exclusive.add_argument( + "-u", + "--uninstall", + help="uninstall systemd service file", + action="store_true") args = parser.parse_args() if args.version: print(f"{myname} ({version})") sys.exit(0) - elif args.events: + + if args.list: types = "\n".join(EventMap.flags.keys()) print(types) sys.exit(0) @@ -146,10 +161,16 @@ def main(): root_logger.setLevel(loglevel) ch = logging.StreamHandler() - formatter = logging.Formatter("%(levelname)s - %(message)s") + formatter = logging.Formatter("%(levelname)s: %(message)s") ch.setFormatter(formatter) root_logger.addHandler(ch) + if args.install: + sys.exit(install_systemd_service(myname)) + + if args.uninstall: + sys.exit(uninstall_systemd_service(myname)) + try: config = {} exec(f"from {myname}.scheduler import *", config) diff --git a/pyinotifyd/install.py b/pyinotifyd/install.py new file mode 100755 index 0000000..7d82005 --- /dev/null +++ b/pyinotifyd/install.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 + +# pyinotifyd is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# pyinotifyd is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with pyinotifyd. If not, see . +# + +import logging +import os +import shutil + + +def check_root(): + if os.getuid() != 0: + logging.error("you need to have root privileges to install " + "the systemd service file, please try again") + return False + + return True + + +def check_systemd(): + if not check_root(): + return False + + if not os.path.isdir("/usr/lib/systemd/system"): + logging.error("systemd is not running on your system") + return False + + return True + + +def install_systemd_service(name): + if not check_systemd(): + return 1 + + dst_path = "/usr/lib/systemd/system/{name}.service" + + if os.path.exists(dst_path): + logging.error("systemd service file is already installed") + return 1 + + pkg_dir = os.path.dirname(__file__) + src_path = f"{pkg_dir}/docs/{name}.service" + + try: + shutil.copy2(src_path, dst_path) + except Exception as e: + logging.error(f"unable to copy: {e}") + return 1 + + logging.info("systemd service file successfully installed") + + return 0 + + +def uninstall_systemd_service(name): + if not check_systemd(): + return 1 + + path = "/usr/lib/systemd/system/{name}.service" + + if not os.path.exists(path): + logging.info("systemd service is not installed") + return 0 + + try: + os.remove(path) + except Exception as e: + logging.error(f"unable to delete: {e}") + return 1 + + return 0