mdserver-web/plugins/openresty/index.py

327 lines
7.8 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-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'
2022-06-26 09:47:13 -04:00
mw.writeFile(nconf, content)
# 静态配置
2022-06-28 12:42:16 -04:00
php_conf = mw.getServerDir() + '/web_conf/php/conf'
if not os.path.exists(php_conf):
mw.execShell('mkdir -p ' + php_conf)
static_conf = mw.getServerDir() + '/web_conf/php/conf/enable-php-00.conf'
2022-06-22 01:44:35 -04:00
if not os.path.exists(static_conf):
2022-07-04 21:26:52 -04:00
mw.writeFile(static_conf, 'set $PHP_ENV 0;')
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
2022-06-18 00:55:27 -04:00
# OpenResty is not installed
2020-06-02 11:05:53 -04:00
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)
2022-06-18 00:55:27 -04:00
# init.d
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
2022-06-18 00:55:27 -04:00
# systemd
2022-07-11 02:02:52 -04:00
# /usr/lib/systemd/system
2022-07-11 01:49:25 -04:00
systemDir = mw.systemdCfgDir()
2022-06-18 00:55:27 -04:00
systemService = systemDir + '/openresty.service'
systemServiceTpl = getPluginDir() + '/init.d/openresty.service.tpl'
if os.path.exists(systemDir) and not os.path.exists(systemService):
se_content = mw.readFile(systemServiceTpl)
se_content = se_content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(systemService, se_content)
mw.execShell('systemctl daemon-reload')
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(
2022-07-03 10:02:17 -04:00
"ps -ef|grep openresty |grep -v grep | grep -v python | awk '{print $2}'")
2018-12-05 01:00:31 -05:00
if data[0] == '':
return 'stop'
return 'start'
2022-06-19 01:55:54 -04:00
def restyOp(method):
2022-06-19 01:56:23 -04:00
file = initDreplace()
2022-06-18 01:37:38 -04:00
2022-06-28 14:22:43 -04:00
# 启动时,先检查一下配置文件
check = getServerDir() + "/bin/openresty -t"
check_data = mw.execShell(check)
2022-06-28 14:47:00 -04:00
if not check_data[1].find('test is successful'):
2022-06-28 14:22:43 -04:00
return check_data[1]
2022-06-18 01:37:38 -04:00
if not mw.isAppleSystem():
2022-06-18 02:40:06 -04:00
data = mw.execShell('systemctl ' + method + ' openresty')
2022-06-18 01:37:38 -04:00
if data[1] == '':
return 'ok'
2022-07-05 22:42:54 -04:00
return data[1]
2022-06-18 01:37:38 -04:00
2022-06-18 02:40:06 -04:00
data = mw.execShell(file + ' ' + method)
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
2022-06-18 02:40:06 -04:00
def start():
2022-06-19 01:54:44 -04:00
return restyOp('start')
2022-06-18 01:37:38 -04:00
2022-06-18 02:40:06 -04:00
def stop():
2022-06-19 01:54:44 -04:00
return restyOp('stop')
2018-12-07 06:12:18 -05:00
def restart():
2022-06-19 01:54:44 -04:00
return restyOp('restart')
2018-12-05 01:00:31 -05:00
def reload():
2022-06-19 01:54:44 -04:00
return restyOp('reload')
2018-12-07 06:12:18 -05:00
def initdStatus():
2022-06-18 00:55:27 -04:00
if mw.isAppleSystem():
return "Apple Computer does not support"
shell_cmd = 'systemctl status openresty | grep loaded | grep "enabled;"'
data = mw.execShell(shell_cmd)
if data[0] == '':
return 'fail'
return 'ok'
2018-12-05 01:00:31 -05:00
2018-12-07 06:12:18 -05:00
def initdInstall():
2022-06-18 00:55:27 -04:00
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl enable openresty')
2018-12-07 06:12:18 -05:00
return 'ok'
def initdUinstall():
2022-06-18 00:55:27 -04:00
if mw.isAppleSystem():
return "Apple Computer does not support"
2019-04-23 12:15:01 -04:00
2022-06-18 00:55:27 -04:00
mw.execShell('systemctl disable openresty')
2018-12-07 06:12:18 -05:00
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:
2022-06-22 15:01:30 -04:00
url = 'http://' + mw.getHostAddr() + '/nginx_status'
2022-06-22 14:49:49 -04:00
result = mw.httpGet(url)
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:
2022-06-22 14:49:49 -04:00
url = 'http://127.0.0.1/nginx_status'
result = mw.httpGet(url)
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]
return mw.getJson(data)
2022-06-22 15:20:45 -04:00
except Exception as e:
2018-12-10 02:10:07 -05:00
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')