From b59d35c1466da9da47edf4d284bfa41844fcb2c3 Mon Sep 17 00:00:00 2001 From: trueold89 Date: Mon, 25 Mar 2024 02:51:48 +0300 Subject: [PATCH] Init yet another rewrite --- .gitignore | 4 ++ VoidServiceControl/__init__.py | 0 vsc.py | 81 ---------------------------------- 3 files changed, 4 insertions(+), 81 deletions(-) create mode 100644 .gitignore create mode 100644 VoidServiceControl/__init__.py delete mode 100755 vsc.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..20edcef --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +old/ +probe.py +__pycache__ +.idea diff --git a/VoidServiceControl/__init__.py b/VoidServiceControl/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/vsc.py b/vsc.py deleted file mode 100755 index a9c5ed3..0000000 --- a/vsc.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- - -# Import -import os, subprocess, sys - -# Const -SV_PATH = '/etc/sv' -ENABLED_PATH = '/var/service' - -# Access -def su(): - out = subprocess.run('whoami', shell=True, text=True, stdout=subprocess.PIPE) - user = out.stdout[:-1] - if user == 'root': - return True - else: - return False - -# Print help message -def helpmsg(): - print(''' -Usage: ---- -vsc {e/enable/on/up} - Run service and add it to autostart -vsc {d/disable/off/down - Stop service and remove it from autostart ---- -''') - sys.exit(1) - -# Check exec args -def args_check(): - args = sys.orig_argv[2:] - if len(args) == 2: - return args - elif len(args) == 1 and args[0] in ['--h','--help']: - helpmsg() - else: - return False - -# Check availability -def availability(service, action): - all = os.listdir(SV_PATH) - enabled = os.listdir(ENABLED_PATH) - all = service in all - if all == False: - raise Exception(f"Service '{service}' doesn't exist") - if service in enabled: - if action == 'on': - raise Exception(f"Service '{service}' already enabled") - elif action == 'off': - return True - else: - if action == 'off': - raise Exception(f"Service '{service}' already disabled") - elif action == 'on': - return True - -# Main -def main(): - try: - args = args_check() - if args == False: - raise Exception('Bad usage. See --help') - if su() == False: - raise Exception('Access denied') - action, service = args - if action in ['enable','e','on','up']: - if availability(service,'on'): - subprocess.run(f'ln -s {SV_PATH}/{service} {ENABLED_PATH}/', shell=True) - print(f"Service '{service}' successfully enabled") - if action in ['disable','d','off','down']: - if availability(service,'off'): - subprocess.run(f'rm {ENABLED_PATH}/{service}', shell=True) - print(f"Service '{service}' successfully disabled") - except Exception as ex: - print(ex) - sys.exit(0) - -if __name__ == '__main__': - main()