add install and uninstall commands for systemd service
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
include LICENSE README.md
|
||||
recursive-include pyinotifyd/docs *
|
||||
recursive-include pyinotifyd/misc *
|
||||
recursive-include docs *
|
||||
recursive-include misc *
|
||||
|
||||
@@ -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)
|
||||
|
||||
82
pyinotifyd/install.py
Executable file
82
pyinotifyd/install.py
Executable 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
|
||||
Reference in New Issue
Block a user