-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor.py
More file actions
53 lines (43 loc) · 1.32 KB
/
supervisor.py
File metadata and controls
53 lines (43 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from errbot import BotPlugin, botcmd, arg_botcmd, webhook
ALLOWED_COMMANDS = [
'status',
'tail',
'version',
'avail',
'maintail',
'restart',
'stop',
]
class Supervisor(BotPlugin):
"""
Bot Plugin to control supervisord
"""
# Passing split_args_with=None will cause arguments to be split on any kind
# of whitespace, just like Python's split() does
@botcmd(split_args_with=None)
def supervisor(self, message, args):
"""A command which simply returns 'Example'"""
output = 'Unable to process!'
if len(args) < 1:
return 'Not enough arguments. Try !supervisor help'
scmd = args[0]
if scmd in ALLOWED_COMMANDS:
additional_params = ' '.join(args[1:]) if len(args) > 1 else ''
output = run_cmd('sudo supervisorctl {cmd} {params}'.format(cmd=scmd, params=additional_params))
return output
def run_cmd(cmd):
import subprocess
try:
output = subprocess.check_output(
cmd.split(),
stderr=subprocess.STDOUT
)
if len(output) > 0:
return output.decode()
else:
return "OK\n"
except subprocess.CalledProcessError as err:
if len(err.output):
return err.output.decode()
else:
return "Error"