Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/supervisor/index.py

582 lines
15 KiB

3 years ago
# coding:utf-8
import sys
import io
import os
import time
import re
3 years ago
sys.path.append(os.getcwd() + "/class/core")
import mw
app_debug = False
if mw.isAppleSystem():
app_debug = True
def getPluginName():
return 'supervisor'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
def getConf():
path = getServerDir() + "/supervisor.conf"
return path
def getConfTpl():
path = getPluginDir() + "/conf/supervisor.conf"
return path
def getSubConfDir():
return getServerDir() + "/conf.d"
3 years ago
def getInitDTpl():
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
return path
def getArgs():
args = sys.argv[2:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
def checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
3 years ago
def status():
data = mw.execShell(
"ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'")
3 years ago
if data[0] == '':
return 'stop'
return 'start'
def initDreplace():
3 years ago
confD = getServerDir() + "/conf.d"
conf = getServerDir() + "/supervisor.conf"
3 years ago
systemDir = '/lib/systemd/system'
systemService = systemDir + '/supervisor.service'
systemServiceTpl = getPluginDir() + '/init.d/supervisor.service'
3 years ago
3 years ago
service_path = os.path.dirname(os.getcwd())
3 years ago
3 years ago
if not os.path.exists(confD):
os.mkdir(confD)
3 years ago
3 years ago
if not os.path.exists(conf):
3 years ago
# config replace
3 years ago
user = 'root'
if mw.isAppleSystem():
cmd = "who | sed -n '2, 1p' |awk '{print $1}'"
user = mw.execShell(cmd)[0].strip()
3 years ago
conf_content = mw.readFile(getConfTpl())
conf_content = conf_content.replace('{$SERVER_PATH}', service_path)
3 years ago
conf_content = conf_content.replace('{$OS_USER}', user)
mw.writeFile(conf, conf_content)
if os.path.exists(systemDir) and not os.path.exists(systemService):
supervisord_bin = mw.execShell('which supervisord')[0].strip()
3 years ago
se_content = mw.readFile(systemServiceTpl)
3 years ago
se_content = se_content.replace('{$SERVER_PATH}', service_path)
se_content = se_content.replace('{$SUP_BIN}', supervisord_bin)
3 years ago
mw.writeFile(systemService, se_content)
mw.execShell('systemctl daemon-reload')
3 years ago
return True
def supOp(method):
pass
3 years ago
def start():
initDreplace()
3 years ago
systemDir = '/lib/systemd/system'
systemService = systemDir + '/supervisor.service'
if os.path.exists(systemService):
data = mw.execShell('systemctl start supervisor')
if data[1] == '':
return 'ok'
return 'fail'
3 years ago
cmd = 'supervisord -c ' + getServerDir() + '/supervisor.conf'
# print(cmd)
data = mw.execShell(cmd)
# print(data)
if data[1] == '':
return 'ok'
return 'fail'
def stop():
initDreplace()
3 years ago
systemDir = '/lib/systemd/system'
systemService = systemDir + '/supervisor.service'
if os.path.exists(systemService):
data = mw.execShell('systemctl stop supervisor')
if data[1] == '':
return 'ok'
return 'fail'
mw.execShell(
"ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'|xargs kill")
3 years ago
if data[1] == '':
return 'ok'
return 'fail'
def restart():
3 years ago
systemDir = '/lib/systemd/system'
systemService = systemDir + '/supervisor.service'
if os.path.exists(systemService):
data = mw.execShell('systemctl restart supervisor')
if data[1] == '':
return 'ok'
return 'fail'
mw.execShell(
"ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'|xargs kill")
return start()
3 years ago
def reload():
3 years ago
3 years ago
systemDir = '/usr/lib/systemd/system'
3 years ago
systemService = systemDir + '/supervisor.service'
if os.path.exists(systemService):
data = mw.execShell('systemctl reload supervisor')
if data[1] == '':
return 'ok'
return 'fail'
mw.execShell(
"ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'|xargs kill")
return start()
3 years ago
def initdStatus():
if mw.isAppleSystem():
return "Apple Computer does not support"
3 years ago
shell_cmd = 'systemctl status supervisor | grep loaded | grep "enabled;"'
data = mw.execShell(shell_cmd)
if data[0] == '':
return 'fail'
return 'ok'
3 years ago
def initdInstall():
3 years ago
if mw.isAppleSystem():
return "Apple Computer does not support"
3 years ago
3 years ago
mw.execShell('systemctl enable supervisor')
3 years ago
return 'ok'
def initdUinstall():
if not app_debug:
if mw.isAppleSystem():
return "Apple Computer does not support"
3 years ago
mw.execShell('systemctl diable supervisor')
3 years ago
return 'ok'
def getSupList():
data = {}
statusFile = getServerDir() + "/status.txt"
3 years ago
supCtl = 'supervisorctl -c ' + getServerDir() + "/supervisor.conf"
cmd = "%s update; %s status > %s" % (supCtl, supCtl, statusFile)
mw.execShell(cmd)
with open(statusFile, "r") as fr:
lines = fr.readlines()
array_list = []
process_list = []
for r in lines:
array = r.split()
if array:
d = dict()
program = array[0].split(':')[0]
if program in process_list:
continue
process_list.append(program)
d["program"] = program
d["runStatus"] = array[1]
if array[1] == "RUNNING":
d["status"] = "1"
d["pid"] = array[3][:-1]
else:
d["status"] = "0"
d["pid"] = ""
file = getServerDir() + '/conf.d/' + program + ".ini"
if not os.path.exists(file):
continue
with open(file, "r") as fr:
infos = fr.readlines()
for line in infos:
if "command=" in line.strip():
d["command"] = line.strip().split('=')[1]
if "user=" in line.strip():
d["user"] = line.strip().split('=')[1]
if "priority=" in line.strip():
d["priority"] = line.strip().split('=')[1]
if "numprocs=" in line.strip():
d["numprocs"] = line.strip().split('=')[1]
array_list.append(d)
# print(array_list)
data = {}
data['data'] = array_list
return mw.getJson(data)
3 years ago
def getUserListData():
user = getServerDir() + "/user.txt"
if not os.path.isfile(user):
os.system(r"touch {}".format(user))
res = mw.execShell("cat /etc/passwd > " + user)
with open(user, "r") as fr:
users = fr.readlines()
fr.close()
os.remove(user)
user_list = []
special = ["bin", "daemon", "adm", "lp", "shutdown", "halt", "mail", "operator", "games",
"avahi-autoipd", "systemd-bus-proxy", "systemd-network", "dbus", "polkitd", "tss", "ntp"]
for u in users:
user = re.split(':', u)[0]
if user[0] == '#':
continue
if user in special:
continue
user_list.append(user)
3 years ago
if mw.isAppleSystem():
cmd = "who | sed -n '2, 1p' |awk '{print $1}'"
user = mw.execShell(cmd)[0].strip()
user_list.append(user)
3 years ago
return user_list
def getUserList():
user_list = getUserListData()
return mw.getJson(user_list)
def addJob():
args = getArgs()
data = checkArgs(args, ['name', 'user', 'path', 'command', 'numprocs'])
if not data[0]:
return data[1]
program = args['name']
command = args['command']
path = args['path']
numprocs = args['numprocs']
user = args['user']
log_dir = getServerDir() + '/log/'
w_body = ""
w_body += "[program:" + program + "]" + "\n"
w_body += "command=" + command + "\n"
w_body += "directory=" + path + "\n"
w_body += "autorestart=true" + "\n"
w_body += "startsecs=3" + "\n"
w_body += "startretries=3" + "\n"
w_body += "stdout_logfile=" + log_dir + program + ".out.log" + "\n"
w_body += "stderr_logfile=" + log_dir + program + ".err.log" + "\n"
w_body += "stdout_logfile_maxbytes=2MB" + "\n"
w_body += "stderr_logfile_maxbytes=2MB" + "\n"
w_body += "user=" + user + "\n"
w_body += "priority=999" + "\n"
w_body += "numprocs={0}".format(numprocs) + "\n"
3 years ago
w_body += "process_name=%(program_name)s"
dstFile = getSubConfDir() + "/" + program + '.ini'
mw.writeFile(dstFile, w_body)
return mw.returnJson(True, '增加守护进程成功!')
3 years ago
def startJob():
args = getArgs()
data = checkArgs(args, ['name', 'status'])
if not data[0]:
return data[1]
supCtl = 'supervisorctl -c ' + getServerDir() + "/supervisor.conf"
name = args['name']
status = args['status']
action = "启动"
cmd = supCtl + " start " + name
3 years ago
if status == 'start':
3 years ago
action = "停止"
cmd = supCtl + " stop " + name
data = mw.execShell(cmd)
if data[0] != '':
return mw.returnJson(False, action + '[' + name + ']失败!')
return mw.returnJson(True, action + '[' + name + ']成功!')
def delJob():
args = getArgs()
data = checkArgs(args, ['name'])
if not data[0]:
return data[1]
name = args['name']
supCtl = 'supervisorctl'
log_dir = getServerDir() + '/log/'
result = mw.execShell("{0} stop ".format(supCtl) + name)
program = getServerDir() + "/conf.d/" + name + ".ini"
# 删除日志文件
outlog = log_dir + name + ".out.log"
if os.path.isfile(outlog):
os.remove(outlog)
errlog = log_dir + name + ".err.log"
if os.path.isfile(errlog):
os.remove(errlog)
# 删除ini文件
if os.path.isfile(program):
os.remove(program)
result = mw.execShell(
"{0} update".format(supCtl))
return mw.returnJson(True, '删除守护进程成功!')
else:
result = mw.execShell(
"{0} update".format(supCtl))
return mw.returnJson(False, '该守护进程不存在!')
def updateJob():
args = getArgs()
3 years ago
data = checkArgs(args, ["name", 'user', 'numprocs', 'priority'])
if not data[0]:
return data[1]
3 years ago
user = args['user']
numprocs = args['numprocs']
priority = args['priority']
name = args['name']
3 years ago
programFile = getServerDir() + "/conf.d/" + name + ".ini"
3 years ago
mess = {}
infos = []
3 years ago
with open(programFile, "r") as fr:
infos = fr.readlines()
for line in infos:
if "command=" in line.strip():
mess["command"] = line.strip().split('=')[1]
3 years ago
if "directory=" in line.strip():
3 years ago
mess["path"] = line.strip().split('=')[1]
3 years ago
# print(mess)
3 years ago
log_file_name = getServerDir() + '/log/' + name
3 years ago
w_body = ""
w_body += "[program:" + name + "]" + "\n"
w_body += "command=" + mess["command"] + "\n"
w_body += "directory=" + mess["path"] + "\n"
w_body += "autorestart=true" + "\n"
w_body += "startsecs=3" + "\n"
w_body += "startretries=3" + "\n"
3 years ago
w_body += "stdout_logfile=" + log_file_name + ".out.log" + "\n"
w_body += "stderr_logfile=" + log_file_name + ".err.log" + "\n"
3 years ago
w_body += "stdout_logfile_maxbytes=2MB" + "\n"
w_body += "stderr_logfile_maxbytes=2MB" + "\n"
w_body += "user=" + user + "\n"
3 years ago
w_body += "priority=" + priority + "\n"
3 years ago
w_body += "numprocs={0}".format(numprocs) + "\n"
3 years ago
w_body += "process_name=%(program_name)s%"
3 years ago
mw.writeFile(programFile, w_body)
return mw.returnJson(True, '修改守护进程成功!')
def getJobInfo():
args = getArgs()
data = checkArgs(args, ['name'])
if not data[0]:
return data[1]
name = args['name']
3 years ago
mess = {}
infos = []
info = {}
3 years ago
program = getServerDir() + "/conf.d/" + name + ".ini"
with open(program, "r") as fr:
infos = fr.readlines()
3 years ago
mess = {}
3 years ago
for line in infos:
if "user=" in line.strip():
mess["user"] = line.strip().split('=')[1]
if "numprocs=" in line.strip():
mess["numprocs"] = line.strip().split('=')[1]
if "priority=" in line.strip():
mess["priority"] = line.strip().split('=')[1]
3 years ago
userlist = getUserListData()
3 years ago
info["userlist"] = userlist
info["daemoninfo"] = mess
return mw.getJson(info)
3 years ago
def configTpl():
path = getServerDir() + '/conf.d'
pathFile = os.listdir(path)
tmp = []
for one in pathFile:
file = path + '/' + one
tmp.append(file)
return mw.getJson(tmp)
def readConfigTpl():
args = getArgs()
data = checkArgs(args, ['file'])
if not data[0]:
return data[1]
content = mw.readFile(args['file'])
return mw.returnJson(True, 'ok', content)
3 years ago
def readConfigLogTpl():
args = getArgs()
data = checkArgs(args, ['file'])
if not data[0]:
return data[1]
file_log = args['file']
line_log = args['line']
with open(file_log, "r") as fr:
infos = fr.readlines()
stdout_logfile = ''
for line in infos:
if "stdout_logfile=" in line.strip():
stdout_logfile = line.strip().split('=')[1]
if stdout_logfile != '':
data = mw.getNumLines(stdout_logfile, int(line_log))
return mw.returnJson(True, 'OK', data)
return mw.returnJson(False, 'OK', '')
def supClearLog():
args = getArgs()
data = checkArgs(args, ['file'])
if not data[0]:
return data[1]
file_log = args['file']
with open(file_log, "r") as fr:
infos = fr.readlines()
stdout_logfile = ''
stderr_logfile = ''
for line in infos:
if "stdout_logfile=" in line.strip():
stdout_logfile = line.strip().split('=')[1]
if "stderr_logfile=" in line.strip():
stderr_logfile = line.strip().split('=')[1]
cmd_stdout = "echo '' > " + stdout_logfile
cmd_stderr = "echo '' > " + stderr_logfile
mw.execShell(cmd_stdout)
mw.execShell(cmd_stderr)
return mw.returnJson(True, '清空成功')
3 years ago
def runLog():
return getServerDir() + '/log/supervisor.log'
3 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
print(status())
elif func == 'start':
print(start())
elif func == 'stop':
print(stop())
elif func == 'restart':
print(restart())
elif func == 'reload':
print(reload())
elif func == 'initd_status':
print(initdStatus())
elif func == 'initd_install':
print(initdInstall())
elif func == 'initd_uninstall':
print(initdUinstall())
3 years ago
elif func == 'config_tpl':
print(configTpl())
elif func == 'read_config_tpl':
print(readConfigTpl())
3 years ago
elif func == 'read_config_log_tpl':
print(readConfigLogTpl())
elif func == 'sup_clear_log':
print(supClearLog())
3 years ago
elif func == 'conf':
print(getConf())
elif func == 'run_log':
print(runLog())
elif func == 'get_user_list':
print(getUserList())
elif func == 'get_sup_list':
print(getSupList())
elif func == 'add_job':
print(addJob())
3 years ago
elif func == 'start_job':
print(startJob())
elif func == 'del_job':
print(delJob())
elif func == 'update_job':
print(updateJob())
elif func == 'get_job_info':
print(getJobInfo())
3 years ago
else:
print('error')