|
|
|
@ -4,6 +4,7 @@ import sys |
|
|
|
|
import io |
|
|
|
|
import os |
|
|
|
|
import time |
|
|
|
|
import re |
|
|
|
|
|
|
|
|
|
sys.path.append(os.getcwd() + "/class/core") |
|
|
|
|
import mw |
|
|
|
@ -41,6 +42,10 @@ def getConfTpl(): |
|
|
|
|
return path |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getSubConfDir(): |
|
|
|
|
return getServerDir() + "/conf.d" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getInitDTpl(): |
|
|
|
|
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl" |
|
|
|
|
return path |
|
|
|
@ -63,10 +68,17 @@ def getArgs(): |
|
|
|
|
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')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def status(): |
|
|
|
|
data = mw.execShell( |
|
|
|
|
"ps -ef|grep supervisor |grep -v grep | awk '{print $2}'") |
|
|
|
|
|
|
|
|
|
data = mw.execShell( |
|
|
|
|
"ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'") |
|
|
|
|
if data[0] == '': |
|
|
|
|
return 'stop' |
|
|
|
|
return 'start' |
|
|
|
@ -109,26 +121,35 @@ def start(): |
|
|
|
|
return 'ok' |
|
|
|
|
return 'fail' |
|
|
|
|
|
|
|
|
|
#| awk '{print $2}'|xargs kill |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stop(): |
|
|
|
|
file = initDreplace() |
|
|
|
|
data = mw.execShell(file + ' stop') |
|
|
|
|
initDreplace() |
|
|
|
|
data = mw.execShell('supervisorctl shutdown') |
|
|
|
|
mw.execShell( |
|
|
|
|
"ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'|xargs kill") |
|
|
|
|
if data[1] == '': |
|
|
|
|
return 'ok' |
|
|
|
|
return 'fail' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def restart(): |
|
|
|
|
file = initDreplace() |
|
|
|
|
data = mw.execShell(file + ' restart') |
|
|
|
|
|
|
|
|
|
mw.execShell( |
|
|
|
|
"ps -ef|grep supervisor | grep -v grep | grep -v index.py | awk '{print $2}'|xargs kill") |
|
|
|
|
|
|
|
|
|
return start() |
|
|
|
|
initDreplace() |
|
|
|
|
data = mw.execShell('supervisorctl reload') |
|
|
|
|
if data[1] == '': |
|
|
|
|
return 'ok' |
|
|
|
|
return 'fail' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reload(): |
|
|
|
|
file = initDreplace() |
|
|
|
|
data = mw.execShell(file + ' reload') |
|
|
|
|
initDreplace() |
|
|
|
|
data = mw.execShell('supervisorctl reload') |
|
|
|
|
if data[1] == '': |
|
|
|
|
return 'ok' |
|
|
|
|
return 'fail' |
|
|
|
@ -199,8 +220,168 @@ def initdUinstall(): |
|
|
|
|
return 'ok' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getSupList(): |
|
|
|
|
data = {} |
|
|
|
|
|
|
|
|
|
statusFile = getServerDir() + "/status.txt" |
|
|
|
|
supCtl = 'supervisorctl' |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getUserList(): |
|
|
|
|
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) |
|
|
|
|
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" |
|
|
|
|
w_body += "process_name=%(program_name)s_%(process_num)02d" |
|
|
|
|
|
|
|
|
|
dstFile = getSubConfDir() + "/" + program + '.ini' |
|
|
|
|
|
|
|
|
|
mw.writeFile(dstFile, w_body) |
|
|
|
|
|
|
|
|
|
return mw.returnJson(True, '增加守护进程成功!') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
name = args['name'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getJobInfo(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
name = args['name'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def runLog(): |
|
|
|
|
return getServerDir() + '/data/redis.log' |
|
|
|
|
return getServerDir() + '/log/supervisor.log' |
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
func = sys.argv[1] |
|
|
|
@ -226,5 +407,17 @@ if __name__ == "__main__": |
|
|
|
|
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()) |
|
|
|
|
elif func == 'del_job': |
|
|
|
|
print(delJob()) |
|
|
|
|
elif func == 'update_job': |
|
|
|
|
print(updateJob()) |
|
|
|
|
elif func == 'get_job_info': |
|
|
|
|
print(getJobInfo()) |
|
|
|
|
else: |
|
|
|
|
print('error') |
|
|
|
|