diff --git a/svc.py b/svc.py deleted file mode 100755 index c374c98..0000000 --- a/svc.py +++ /dev/null @@ -1,89 +0,0 @@ -#!/bin/python3 - -########### -# Modules # -########### - -import os -import subprocess -import sys - -############# -# Functions # -############# - -# Access check -def sucheck(): - user = subprocess.run('whoami', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) - if user.stdout[:-1] == 'root': - return True - else: - return 'Run script as root user' - -#Args check -def argcheck(): - if len(sys.orig_argv[1:]) == 3: - return True - else: - return 'Invalid usage' - -#Options check -def optcheck(option): - path = '' - allopts_list = [] - allopts = { 'enable': ['enable', 'e', 'on'], 'disable': ['disable','d','off'], 'down': ['down','dt']} - for i in allopts.keys(): - allopts_list = allopts_list + allopts[i] - if not(option in allopts_list): - print(f'Invalid option: {option}') - sys.exit() - while path == '': - if option in allopts['enable']: - option = 'enable' - path = '/etc/sv' - if option in allopts['disable']: - option = 'disable' - path = '/var/service' - if option in allopts['down']: - option = 'down' - path = '/etc/sv' - return path, option - -#Service check -def svcheck(service,path): - list = os.listdir(path) - if service in list: - return True - else: - return f'Service not found: {service}' - -#Control service -def action(option,service): - if option == 'enable': - subprocess.run(f'ln -s /etc/sv/{service} /var/service', shell=True) - return f'{service} enabled' - if option == 'disable': - subprocess.run(f'rm /var/service/{service}', shell=True) - return f'{service} disabled' - if option == 'down': - subprocess.run(f'touch /etc/sv/{service}/down', shell=True) - return f'Down-file created for {service}' - -######## -# Exec # -######## - -ac = argcheck() -if ac == True: - suc = sucheck() - if suc == True: - oc = optcheck(sys.argv[1]) - sc = svcheck(sys.argv[2],oc[0]) - if sc == True: - print(action(oc[1],sys.argv[2])) - else: - print(sc) - else: - print(suc) -else: - print(ac) diff --git a/vsc.py b/vsc.py new file mode 100755 index 0000000..db9bb7d --- /dev/null +++ b/vsc.py @@ -0,0 +1,82 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +# Modules +import os +import subprocess +import sys + +# Vars +allsv_path = '/etc/sv/' +enabledsv_path = '/var/service/' +allsv = os.listdir(allsv_path) +enabledsv = os.listdir(enabledsv_path) + +# Functions + +## Help +def helpmsg(): + msg = ''' +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 +---''' + return msg + +## Access +def su(): + user = subprocess.run('whoami', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + if user.stdout[:-1] == 'root': + return args() + else: + return 'Access denied' + +## Service +def exec(option, service): + if option == 'on': + if service in enabledsv: + return 'Service alredy enabled' + if service in allsv: + subprocess.run(f'ln -s {allsv_path}{service} {enabledsv_path}', shell=True) + return f'Service {service} successfully enabled' + if not(service in allsv): + return f"Service {service} doesn't exist" + if option == 'off': + if not(service in allsv): + return f"Service {service} doesn't exist" + if not(service in enabledsv): + return f'Service {service} has already been disabled' + if service in enabledsv: + subprocess.run(f'rm {enabledsv_path}{service}', shell=True) + return f'Service {service} successfully disabled' + +# Get argv +def getargs(): + global service + global option + option = sys.argv[1] + service = sys.argv[2] + return options(option) + +# Args +def args(): + if len(sys.orig_argv[1:]) == 2: + if sys.orig_argv[2] == '--help': + return helpmsg() + elif len(sys.orig_argv[1:]) == 3: + return getargs() + else: + return 'Invalid usage, see --help' + +def options(option): + options = { + 'on': ['enable','e','on','up'], + 'off': ['disable','d','off','down'] + } + for i in options.keys(): + if option in options.get(i): + return exec(i,service) + return f'Invalid option: {option}, see --help' + +print(su())