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/openresty/index.py

312 lines
7.2 KiB

7 years ago
# coding:utf-8
import sys
import io
import os
import time
6 years ago
import subprocess
7 years ago
sys.path.append(os.getcwd() + "/class/core")
import mw
7 years ago
7 years ago
app_debug = False
6 years ago
if mw.isAppleSystem():
7 years ago
app_debug = True
def getPluginName():
return 'openresty'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
7 years ago
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
7 years ago
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
6 years ago
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
7 years ago
def clearTemp():
path_bin = getServerDir() + "/nginx"
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')
7 years ago
7 years ago
def getConf():
6 years ago
path = getServerDir() + "/nginx/conf/nginx.conf"
7 years ago
return path
6 years ago
def getConfTpl():
path = getPluginDir() + '/conf/nginx.conf'
return path
6 years ago
def getOs():
data = {}
data['os'] = mw.getOs()
6 years ago
ng_exe_bin = getServerDir() + "/nginx/sbin/nginx"
if checkAuthEq(ng_exe_bin, 'root'):
data['auth'] = True
else:
data['auth'] = False
return mw.getJson(data)
6 years ago
7 years ago
def getInitDTpl():
path = getPluginDir() + "/init.d/nginx.tpl"
return path
7 years ago
def makeConf():
vhost = getServerDir() + '/nginx/conf/vhost'
if not os.path.exists(vhost):
os.mkdir(vhost)
6 years ago
php_status = getServerDir() + '/nginx/conf/php_status'
if not os.path.exists(php_status):
os.mkdir(php_status)
7 years ago
6 years ago
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
7 years ago
def confReplace():
service_path = os.path.dirname(os.getcwd())
content = mw.readFile(getConfTpl())
7 years ago
content = content.replace('{$SERVER_PATH}', service_path)
7 years ago
user = 'www'
user_group = 'www'
6 years ago
if mw.getOs() == 'darwin':
6 years ago
# macosx do
user = mw.execShell(
7 years ago
"who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
7 years ago
# user = 'root'
user_group = 'staff'
7 years ago
content = content.replace('{$EVENT_MODEL}', 'kqueue')
7 years ago
else:
7 years ago
content = content.replace('{$EVENT_MODEL}', 'epoll')
7 years ago
content = content.replace('{$OS_USER}', user)
content = content.replace('{$OS_USER_GROUP}', user_group)
7 years ago
6 years ago
nconf = getServerDir() + '/nginx/conf/nginx.conf'
6 years ago
__content = mw.readFile(nconf)
6 years ago
if __content.find('#user'):
mw.writeFile(getServerDir() + '/nginx/conf/nginx.conf', content)
7 years ago
6 years ago
# give nginx root permission
7 years ago
ng_exe_bin = getServerDir() + "/nginx/sbin/nginx"
6 years ago
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))
7 years ago
7 years ago
def initDreplace():
file_tpl = getInitDTpl()
service_path = os.path.dirname(os.getcwd())
initD_path = getServerDir() + '/init.d'
5 years ago
# OpenResty is not installed
5 years ago
if not os.path.exists(getServerDir()):
4 years ago
print("ok")
5 years ago
exit(0)
# init.d
5 years ago
file_bin = initD_path + '/' + getPluginName()
7 years ago
if not os.path.exists(initD_path):
os.mkdir(initD_path)
5 years ago
# 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)
7 years ago
5 years ago
# config replace
confReplace()
7 years ago
# systemd
systemDir = '/lib/systemd/system'
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')
7 years ago
# make nginx vhost or other
makeConf()
7 years ago
return file_bin
7 years ago
def status():
data = mw.execShell(
5 years ago
"ps -ef|grep nginx |grep -v grep | grep -v python | awk '{print $2}'")
7 years ago
if data[0] == '':
return 'stop'
return 'start'
def restyOp(version, method):
file = initReplace(version)
3 years ago
if not mw.isAppleSystem():
data = mw.execShell('systemctl ' + method + ' openresty')
3 years ago
if data[1] == '':
return 'ok'
return 'fail'
data = mw.execShell(file + ' ' + method)
7 years ago
if data[1] == '':
7 years ago
return 'ok'
6 years ago
return data[1]
7 years ago
def start():
3 years ago
return restyOp('start')
3 years ago
def stop():
3 years ago
return restyOp('stop')
7 years ago
def restart():
3 years ago
return restyOp('restart')
7 years ago
def reload():
3 years ago
return restyOp('reload')
7 years ago
def initdStatus():
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'
7 years ago
7 years ago
def initdInstall():
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl enable openresty')
7 years ago
return 'ok'
def initdUinstall():
if mw.isAppleSystem():
return "Apple Computer does not support"
6 years ago
mw.execShell('systemctl disable openresty')
7 years ago
return 'ok'
7 years ago
7 years ago
def runInfo():
# 取Openresty负载状态
7 years ago
try:
result = mw.httpGet('http://127.0.0.1/nginx_status')
7 years ago
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)
7 years ago
except Exception as e:
return 'oprenresty not started'
7 years ago
def errorLogPath():
return getServerDir() + '/nginx/logs/error.log'
7 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
4 years ago
print(status())
7 years ago
elif func == 'start':
4 years ago
print(start())
7 years ago
elif func == 'stop':
4 years ago
print(stop())
7 years ago
elif func == 'restart':
4 years ago
print(restart())
7 years ago
elif func == 'reload':
4 years ago
print(reload())
7 years ago
elif func == 'initd_status':
4 years ago
print(initdStatus())
7 years ago
elif func == 'initd_install':
4 years ago
print(initdInstall())
7 years ago
elif func == 'initd_uninstall':
4 years ago
print(initdUinstall())
7 years ago
elif func == 'conf':
4 years ago
print(getConf())
6 years ago
elif func == 'get_os':
4 years ago
print(getOs())
7 years ago
elif func == 'run_info':
4 years ago
print(runInfo())
elif func == 'error_log':
4 years ago
print(errorLogPath())
7 years ago
else:
4 years ago
print('error')