Another rewrite
This commit is contained in:
parent
9ea77fe7d2
commit
a364a1c3c7
28
README.md
28
README.md
|
@ -1,28 +1,42 @@
|
||||||
# Void Service Control (VSC)
|
# Void Service Control (VSC)
|
||||||
## A simple script that will allow you to manage runit services in Void Linux
|
## A simple script that will allow you to manage runit services in Void Linux
|
||||||
|
|
||||||
|
### Install:
|
||||||
|
|
||||||
|
**You can install vsc using pip:**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ pip install void-service-control
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
or by downloading the pre-built binary from the **[releases](https://git.orudo.ru/trueold89/void-service-control/releases)** page
|
||||||
|
|
||||||
|
***
|
||||||
|
|
||||||
### Usage:
|
### Usage:
|
||||||
|
|
||||||
**Enbale service:**
|
**Enbale service:**
|
||||||
|
|
||||||
```
|
```bash
|
||||||
vsc e <service_name>
|
$ vsc e <service_name>
|
||||||
```
|
```
|
||||||
|
|
||||||
**Disable service:**
|
**Disable service:**
|
||||||
|
|
||||||
```
|
```bash
|
||||||
vsc d <service_name>
|
$ vsc d <service_name>
|
||||||
```
|
```
|
||||||
|
|
||||||
**Print help**
|
**Print help**
|
||||||
|
|
||||||
```
|
```bash
|
||||||
vsc --help
|
$ vsc --help
|
||||||
```
|
```
|
||||||
*All commands require root privileges*
|
*All commands require root privileges*
|
||||||
|
|
||||||
---
|
***
|
||||||
|
|
||||||
## Example:
|
## Example:
|
||||||
|
|
||||||
|
|
133
vsc.py
133
vsc.py
|
@ -1,82 +1,81 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Modules
|
# Import
|
||||||
import os
|
import os, subprocess, sys
|
||||||
import subprocess
|
|
||||||
import sys
|
|
||||||
|
|
||||||
# Vars
|
# Const
|
||||||
allsv_path = '/etc/sv/'
|
SV_PATH = '/etc/sv'
|
||||||
enabledsv_path = '/var/service/'
|
ENABLED_PATH = '/var/service'
|
||||||
allsv = os.listdir(allsv_path)
|
|
||||||
enabledsv = os.listdir(enabledsv_path)
|
|
||||||
|
|
||||||
# Functions
|
# 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
|
||||||
|
|
||||||
## Help
|
# Print help message
|
||||||
def helpmsg():
|
def helpmsg():
|
||||||
msg = '''
|
print('''
|
||||||
Usage:
|
Usage:
|
||||||
---
|
---
|
||||||
vsc {e/enable/on/up} <service_name> - Run service and add it to autostart
|
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
|
vsc {d/disable/off/down <service_name> - Stop service and remove it from autostart
|
||||||
---'''
|
---
|
||||||
return msg
|
''')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
## Access
|
# Check exec args
|
||||||
def su():
|
def args_check():
|
||||||
user = subprocess.run('whoami', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
args = sys.orig_argv[2:]
|
||||||
if user.stdout[:-1] == 'root':
|
if len(args) == 2:
|
||||||
return args()
|
return args
|
||||||
else:
|
elif len(args) == 1 and args[0] in ['--h','--help']:
|
||||||
return 'Access denied'
|
helpmsg()
|
||||||
|
|
||||||
## 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:
|
else:
|
||||||
return 'Invalid usage, see --help'
|
return False
|
||||||
|
|
||||||
def options(option):
|
# Check availability
|
||||||
options = {
|
def availability(service, action):
|
||||||
'on': ['enable','e','on','up'],
|
all = os.listdir(SV_PATH)
|
||||||
'off': ['disable','d','off','down']
|
enabled = os.listdir(ENABLED_PATH)
|
||||||
}
|
all = service in all
|
||||||
for i in options.keys():
|
if all == False:
|
||||||
if option in options.get(i):
|
raise Exception(f"Service '{service}' doesn't exist")
|
||||||
return exec(i,service)
|
if service in enabled:
|
||||||
return f'Invalid option: {option}, see --help'
|
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
|
||||||
|
|
||||||
print(su())
|
# 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()
|
||||||
|
|
Loading…
Reference in New Issue