mdserver-web/plugins/openresty/index.py

310 lines
7.1 KiB
Python
Raw Normal View History

2018-12-05 01:00:31 -05:00
# coding:utf-8
import sys
import io
import os
import time
2018-12-16 10:29:35 -05:00
import subprocess
2018-12-05 01:00:31 -05:00
sys.path.append(os.getcwd() + "/class/core")
2020-07-10 03:57:25 -04:00
import mw
2018-12-05 01:00:31 -05:00
2018-12-07 06:12:18 -05:00
app_debug = False
2018-12-18 06:37:46 -05:00
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2018-12-07 06:12:18 -05:00
app_debug = True
def getPluginName():
return 'openresty'
def getPluginDir():
2020-07-10 03:57:25 -04:00
return mw.getPluginDir() + '/' + getPluginName()
2018-12-07 06:12:18 -05:00
def getServerDir():
2020-07-10 03:57:25 -04:00
return mw.getServerDir() + '/' + getPluginName()
2018-12-07 06:12:18 -05:00
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
2018-12-16 10:29:35 -05:00
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
2018-12-07 23:42:02 -05:00
def clearTemp():
path_bin = getServerDir() + "/nginx"
2020-07-10 03:57:25 -04:00
mw.execShell('rm -rf ' + path_bin + '/client_body_temp')
mw.execShell('rm -rf ' + path_bin + '/fastcgi_temp')
mw.execShell('rm -rf ' + path_bin + '/proxy_temp')
mw.execShell('rm -rf ' + path_bin + '/scgi_temp')
mw.execShell('rm -rf ' + path_bin + '/uwsgi_temp')
2018-12-07 23:42:02 -05:00
2018-12-07 06:12:18 -05:00
def getConf():
2019-02-09 23:43:24 -05:00
path = getServerDir() + "/nginx/conf/nginx.conf"
2018-12-07 06:12:18 -05:00
return path
2019-02-25 02:18:08 -05:00
def getConfTpl():
path = getPluginDir() + '/conf/nginx.conf'
return path
2018-12-16 22:06:58 -05:00
def getOs():
data = {}
2020-07-10 03:57:25 -04:00
data['os'] = mw.getOs()
2018-12-16 22:06:58 -05:00
ng_exe_bin = getServerDir() + "/nginx/sbin/nginx"
if checkAuthEq(ng_exe_bin, 'root'):
data['auth'] = True
else:
data['auth'] = False
2020-07-10 03:57:25 -04:00
return mw.getJson(data)
2018-12-16 22:06:58 -05:00
2018-12-07 06:12:18 -05:00
def getInitDTpl():
path = getPluginDir() + "/init.d/nginx.tpl"
return path
2018-12-08 01:43:32 -05:00
def makeConf():
vhost = getServerDir() + '/nginx/conf/vhost'
if not os.path.exists(vhost):
os.mkdir(vhost)
2018-12-18 06:37:46 -05:00
php_status = getServerDir() + '/nginx/conf/php_status'
if not os.path.exists(php_status):
os.mkdir(php_status)
2018-12-08 01:43:32 -05:00
2018-12-16 22:06:58 -05:00
def getFileOwner(filename):
import pwd
stat = os.lstat(filename)
uid = stat.st_uid
pw = pwd.getpwuid(uid)
return pw.pw_name
def checkAuthEq(file, owner='root'):
fowner = getFileOwner(file)
if (fowner == owner):
return True
return False
2018-12-07 06:12:18 -05:00
def confReplace():
service_path = os.path.dirname(os.getcwd())
2020-07-10 03:57:25 -04:00
content = mw.readFile(getConfTpl())
2018-12-07 06:12:18 -05:00
content = content.replace('{$SERVER_PATH}', service_path)
2018-12-08 02:47:40 -05:00
user = 'www'
user_group = 'www'
2018-12-16 22:06:58 -05:00
2020-07-10 03:57:25 -04:00
if mw.getOs() == 'darwin':
2018-12-16 22:06:58 -05:00
# macosx do
2020-07-10 03:57:25 -04:00
user = mw.execShell(
2018-12-07 06:12:18 -05:00
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
2018-12-08 02:47:40 -05:00
# user = 'root'
user_group = 'staff'
2018-12-07 23:42:02 -05:00
content = content.replace('{$EVENT_MODEL}', 'kqueue')
2018-12-07 06:12:18 -05:00
else:
2018-12-07 23:42:02 -05:00
content = content.replace('{$EVENT_MODEL}', 'epoll')
2018-12-08 02:47:40 -05:00
content = content.replace('{$OS_USER}', user)
content = content.replace('{$OS_USER_GROUP}', user_group)
2018-12-07 06:12:18 -05:00
2019-04-23 12:15:01 -04:00
nconf = getServerDir() + '/nginx/conf/nginx.conf'
2019-05-18 03:28:09 -04:00
2020-07-10 03:57:25 -04:00
__content = mw.readFile(nconf)
2019-05-18 03:28:09 -04:00
if __content.find('#user'):
2020-07-10 03:57:25 -04:00
mw.writeFile(getServerDir() + '/nginx/conf/nginx.conf', content)
2018-12-07 06:12:18 -05:00
2018-12-16 22:06:58 -05:00
# give nginx root permission
2018-12-08 03:25:45 -05:00
ng_exe_bin = getServerDir() + "/nginx/sbin/nginx"
2018-12-16 22:06:58 -05:00
if not checkAuthEq(ng_exe_bin, 'root'):
args = getArgs()
sudoPwd = args['pwd']
cmd_own = 'chown -R ' + 'root:' + user_group + ' ' + ng_exe_bin
os.system('echo %s|sudo -S %s' % (sudoPwd, cmd_own))
cmd_mod = 'chmod 755 ' + ng_exe_bin
os.system('echo %s|sudo -S %s' % (sudoPwd, cmd_mod))
cmd_s = 'chmod u+s ' + ng_exe_bin
os.system('echo %s|sudo -S %s' % (sudoPwd, cmd_s))
2018-12-08 02:47:40 -05:00
2018-12-07 06:12:18 -05:00
def initDreplace():
file_tpl = getInitDTpl()
service_path = os.path.dirname(os.getcwd())
initD_path = getServerDir() + '/init.d'
2020-06-02 11:05:53 -04:00
# Openresty is not installed
if not os.path.exists(getServerDir()):
2021-05-09 12:13:32 -04:00
print("ok")
2020-06-02 11:05:53 -04:00
exit(0)
2020-07-10 13:24:54 -04:00
file_bin = initD_path + '/' + getPluginName()
2018-12-07 06:12:18 -05:00
if not os.path.exists(initD_path):
os.mkdir(initD_path)
2020-07-10 13:24:54 -04:00
# initd replace
content = mw.readFile(file_tpl)
content = content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
2018-12-07 06:12:18 -05:00
2020-07-10 13:24:54 -04:00
# config replace
confReplace()
2018-12-07 06:12:18 -05:00
2018-12-08 01:43:32 -05:00
# make nginx vhost or other
makeConf()
2018-12-07 06:12:18 -05:00
return file_bin
2018-12-05 01:00:31 -05:00
def status():
2020-07-10 03:57:25 -04:00
data = mw.execShell(
2020-06-02 11:05:53 -04:00
"ps -ef|grep nginx |grep -v grep | grep -v python | awk '{print $2}'")
2018-12-05 01:00:31 -05:00
if data[0] == '':
return 'stop'
return 'start'
def start():
2018-12-07 06:12:18 -05:00
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' start')
2018-12-07 06:12:18 -05:00
if data[1] == '':
2018-12-05 01:00:31 -05:00
return 'ok'
2018-12-20 00:29:05 -05:00
return data[1]
2018-12-05 01:00:31 -05:00
def stop():
2018-12-07 06:12:18 -05:00
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' stop')
2018-12-07 23:42:02 -05:00
clearTemp()
2018-12-07 06:12:18 -05:00
if data[1] == '':
return 'ok'
2018-12-20 00:29:05 -05:00
return data[1]
2018-12-07 06:12:18 -05:00
def restart():
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' restart')
2018-12-07 06:12:18 -05:00
if data[1] == '':
2018-12-05 01:00:31 -05:00
return 'ok'
2018-12-18 06:37:46 -05:00
return data[1]
2018-12-05 01:00:31 -05:00
def reload():
2018-12-07 06:12:18 -05:00
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' reload')
2018-12-07 06:12:18 -05:00
if data[1] == '':
return 'ok'
2018-12-20 00:29:05 -05:00
return data[1]
2018-12-07 06:12:18 -05:00
def initdStatus():
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2018-12-07 06:12:18 -05:00
return "Apple Computer does not support"
initd_bin = getInitDFile()
if os.path.exists(initd_bin):
2018-12-05 01:00:31 -05:00
return 'ok'
return 'fail'
2018-12-07 06:12:18 -05:00
def initdInstall():
import shutil
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2018-12-07 06:12:18 -05:00
return "Apple Computer does not support"
source_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(source_bin, initd_bin)
2020-07-10 03:57:25 -04:00
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
2018-12-07 06:12:18 -05:00
return 'ok'
def initdUinstall():
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2018-12-07 06:12:18 -05:00
return "Apple Computer does not support"
2019-04-23 12:15:01 -04:00
2020-07-10 03:57:25 -04:00
mw.execShell('chkconfig --del ' + getPluginName())
2018-12-07 06:12:18 -05:00
initd_bin = getInitDFile()
os.remove(initd_bin)
return 'ok'
2018-12-05 07:15:00 -05:00
2018-12-08 03:24:36 -05:00
def runInfo():
# 取Openresty负载状态
2018-12-10 02:10:07 -05:00
try:
2020-07-10 03:57:25 -04:00
result = mw.httpGet('http://127.0.0.1/nginx_status')
2018-12-10 02:10:07 -05:00
tmp = result.split()
data = {}
data['active'] = tmp[2]
data['accepts'] = tmp[9]
data['handled'] = tmp[7]
data['requests'] = tmp[8]
data['Reading'] = tmp[11]
data['Writing'] = tmp[13]
data['Waiting'] = tmp[15]
2020-07-10 03:57:25 -04:00
return mw.getJson(data)
2018-12-10 02:10:07 -05:00
except Exception as e:
return 'oprenresty not started'
2018-12-08 03:24:36 -05:00
2018-12-09 22:42:13 -05:00
def errorLogPath():
return getServerDir() + '/nginx/logs/error.log'
2018-12-05 01:00:31 -05:00
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
2021-05-01 05:42:19 -04:00
print(status())
2018-12-05 01:00:31 -05:00
elif func == 'start':
2021-05-01 05:42:19 -04:00
print(start())
2018-12-05 01:00:31 -05:00
elif func == 'stop':
2021-05-01 05:42:19 -04:00
print(stop())
2018-12-07 23:42:02 -05:00
elif func == 'restart':
2021-05-01 05:42:19 -04:00
print(restart())
2018-12-05 01:00:31 -05:00
elif func == 'reload':
2021-05-01 05:42:19 -04:00
print(reload())
2018-12-07 06:12:18 -05:00
elif func == 'initd_status':
2021-05-01 05:42:19 -04:00
print(initdStatus())
2018-12-07 06:12:18 -05:00
elif func == 'initd_install':
2021-05-01 05:42:19 -04:00
print(initdInstall())
2018-12-07 06:12:18 -05:00
elif func == 'initd_uninstall':
2021-05-01 05:42:19 -04:00
print(initdUinstall())
2018-12-05 07:15:00 -05:00
elif func == 'conf':
2021-05-01 05:42:19 -04:00
print(getConf())
2018-12-16 10:29:35 -05:00
elif func == 'get_os':
2021-05-01 05:42:19 -04:00
print(getOs())
2018-12-08 03:24:36 -05:00
elif func == 'run_info':
2021-05-01 05:42:19 -04:00
print(runInfo())
2018-12-09 22:42:13 -05:00
elif func == 'error_log':
2021-05-01 05:42:19 -04:00
print(errorLogPath())
2018-12-05 01:00:31 -05:00
else:
2021-05-01 05:42:19 -04:00
print('error')