Compare commits

...

2 Commits

Author SHA1 Message Date
trueold89 9ea77fe7d2
Add README.md 2023-08-28 11:26:11 +03:00
trueold89 91c541711e
Rewrite 2023-08-28 10:19:27 +03:00
4 changed files with 111 additions and 89 deletions

BIN
.ex.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 202 KiB

29
README.md Normal file
View File

@ -0,0 +1,29 @@
# Void Service Control (VSC)
## A simple script that will allow you to manage runit services in Void Linux
### Usage:
**Enbale service:**
```
vsc e <service_name>
```
**Disable service:**
```
vsc d <service_name>
```
**Print help**
```
vsc --help
```
*All commands require root privileges*
---
## Example:
![](.ex.png)

89
svc.py
View File

@ -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)

82
vsc.py Executable file
View File

@ -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} <service_name> - Run service and add it to autostart
vsc {d/disable/off/down <service_name> - 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())