add install and uninstall commands for systemd service

This commit is contained in:
2020-11-05 01:03:07 +01:00
parent 0b8e32fe53
commit 73b6e8fa0b
5 changed files with 113 additions and 10 deletions

View File

@@ -1,3 +1,3 @@
include LICENSE README.md include LICENSE README.md
recursive-include pyinotifyd/docs * recursive-include docs *
recursive-include pyinotifyd/misc * recursive-include misc *

View File

@@ -30,6 +30,8 @@ import sys
from pyinotifyd.version import __version__ as version from pyinotifyd.version import __version__ as version
from pyinotifyd.watch import Watch, EventMap from pyinotifyd.watch import Watch, EventMap
from pyinotifyd.install import install_systemd_service
from pyinotifyd.install import uninstall_systemd_service
class Pyinotifyd: class Pyinotifyd:
@@ -110,29 +112,42 @@ def main():
parser.add_argument( parser.add_argument(
"-c", "-c",
"--config", "--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") default=f"/etc/{myname}/config.py")
parser.add_argument( parser.add_argument(
"-d", "-d",
"--debug", "--debug",
help="log debugging messages", help="log debugging messages",
action="store_true") action="store_true")
parser.add_argument(
"-e", exclusive = parser.add_mutually_exclusive_group()
"--events", exclusive.add_argument(
help="show event types and exit", "-l",
"--list",
help="show all usable event types and exit",
action="store_true") action="store_true")
parser.add_argument( exclusive.add_argument(
"-v", "-v",
"--version", "--version",
help="show version and exit", help="show version and exit",
action="store_true") 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() args = parser.parse_args()
if args.version: if args.version:
print(f"{myname} ({version})") print(f"{myname} ({version})")
sys.exit(0) sys.exit(0)
elif args.events:
if args.list:
types = "\n".join(EventMap.flags.keys()) types = "\n".join(EventMap.flags.keys())
print(types) print(types)
sys.exit(0) sys.exit(0)
@@ -146,10 +161,16 @@ def main():
root_logger.setLevel(loglevel) root_logger.setLevel(loglevel)
ch = logging.StreamHandler() ch = logging.StreamHandler()
formatter = logging.Formatter("%(levelname)s - %(message)s") formatter = logging.Formatter("%(levelname)s: %(message)s")
ch.setFormatter(formatter) ch.setFormatter(formatter)
root_logger.addHandler(ch) root_logger.addHandler(ch)
if args.install:
sys.exit(install_systemd_service(myname))
if args.uninstall:
sys.exit(uninstall_systemd_service(myname))
try: try:
config = {} config = {}
exec(f"from {myname}.scheduler import *", config) exec(f"from {myname}.scheduler import *", config)

82
pyinotifyd/install.py Executable file
View File

@@ -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 <http://www.gnu.org/licenses/>.
#
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