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

213 lines
4.4 KiB

3 years ago
# coding:utf-8
import sys
import io
import os
import time
web_dir = os.getcwd() + "/web"
if os.path.exists(web_dir):
sys.path.append(web_dir)
os.chdir(web_dir)
import core.mw as mw
3 years ago
app_debug = False
if mw.isAppleSystem():
app_debug = True
def getPluginName():
return 'varnish'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getConf():
3 years ago
path = '/etc/varnish/vcl.conf'
if os.path.exists(path):
return path
3 years ago
path = "/etc/varnish/default.vcl"
3 years ago
return path
def getInitDTpl():
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
return path
3 years ago
def contentReplace(content):
service_path = mw.getServerDir()
6 months ago
content = content.replace('{$ROOT_PATH}', mw.getFatherDir())
3 years ago
content = content.replace('{$SERVER_PATH}', service_path)
content = content.replace('{$SERVER_APP}', service_path + '/varnish')
return content
3 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
3 years ago
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(
3 years ago
"ps -ef|grep varnish |grep -v grep | grep -v python | grep -v mdserver-web | awk '{print $2}'")
3 years ago
if data[0] == '':
return 'stop'
return 'start'
3 years ago
def vaOp(method):
mw.execShell("systemctl daemon-reload")
data = mw.execShell('systemctl ' + method + ' ' + getPluginName())
3 years ago
if data[1] == '':
return 'ok'
return 'fail'
3 years ago
def start():
return vaOp('start')
3 years ago
def stop():
3 years ago
return vaOp('stop')
3 years ago
def restart():
3 years ago
return vaOp('restart')
3 years ago
def reload():
3 years ago
return vaOp('reload')
3 years ago
def runInfo():
3 years ago
cmd = "varnishstat -j"
3 years ago
data = mw.execShell(cmd)[0].strip()
3 years ago
return data
3 years ago
3 years ago
def configTpl():
path = getPluginDir() + '/tpl'
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'])
content = contentReplace(content)
return mw.returnJson(True, 'ok', content)
3 years ago
def initdStatus():
3 years ago
if mw.isAppleSystem():
return "Apple Computer does not support"
3 years ago
shell_cmd = 'systemctl status ' + \
getPluginName() + ' | 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 ' + getPluginName())
3 years ago
return 'ok'
def initdUinstall():
3 years ago
if mw.isAppleSystem():
return "Apple Computer does not support"
3 years ago
3 years ago
mw.execShell('systemctl disable ' + getPluginName())
3 years ago
return 'ok'
def runLog():
3 years ago
if os.path.exists("/var/log/varnish/varnish.log"):
return "/var/log/varnish/varnish.log"
3 years ago
return "/var/log/varnish/varnishncsa.log"
3 years ago
def confService():
3 years ago
return mw.systemdCfgDir() + '/varnish.service'
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())
elif func == 'run_info':
print(runInfo())
elif func == 'conf':
print(getConf())
elif func == 'conf_service':
print(confService())
3 years ago
elif func == 'run_log':
print(runLog())
3 years ago
elif func == 'config_tpl':
print(configTpl())
elif func == 'read_config_tpl':
print(readConfigTpl())
3 years ago
else:
print('error')