|
|
|
# coding:utf-8
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import io
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import re
|
|
|
|
import string
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
sys.path.append(os.getcwd() + "/class/core")
|
|
|
|
import mw
|
|
|
|
|
|
|
|
app_debug = False
|
|
|
|
if mw.isAppleSystem():
|
|
|
|
app_debug = True
|
|
|
|
|
|
|
|
|
|
|
|
def getPluginName():
|
|
|
|
return 'lvs'
|
|
|
|
|
|
|
|
|
|
|
|
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 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
|
|
|
|
|
|
|
|
|
|
|
|
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 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)
|
|
|
|
|
|
|
|
|
|
|
|
def contentReplace(content):
|
|
|
|
service_path = mw.getServerDir()
|
|
|
|
content = content.replace('{$ROOT_PATH}', mw.getRootDir())
|
|
|
|
content = content.replace('{$SERVER_PATH}', service_path)
|
|
|
|
content = content.replace('{$SERVER_APP}', service_path + '/'+getPluginName())
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
|
|
def status():
|
|
|
|
data = mw.execShell("which ipvsadm")
|
|
|
|
if data[0] == '':
|
|
|
|
return 'stop'
|
|
|
|
return 'start'
|
|
|
|
|
|
|
|
|
|
|
|
def initDreplace():
|
|
|
|
|
|
|
|
file_tpl = getInitDTpl()
|
|
|
|
service_path = os.path.dirname(os.getcwd())
|
|
|
|
|
|
|
|
initD_path = getServerDir() + '/init.d'
|
|
|
|
if not os.path.exists(initD_path):
|
|
|
|
os.mkdir(initD_path)
|
|
|
|
file_bin = initD_path + '/' + getPluginName()
|
|
|
|
|
|
|
|
# initd replace
|
|
|
|
if not os.path.exists(file_bin):
|
|
|
|
content = mw.readFile(file_tpl)
|
|
|
|
content = contentReplace(content)
|
|
|
|
mw.writeFile(file_bin, content)
|
|
|
|
mw.execShell('chmod +x ' + file_bin)
|
|
|
|
|
|
|
|
|
|
|
|
return file_bin
|
|
|
|
|
|
|
|
|
|
|
|
def haOp(method):
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
|
|
|
|
def start():
|
|
|
|
return haOp('start')
|
|
|
|
|
|
|
|
|
|
|
|
def stop():
|
|
|
|
return haOp('stop')
|
|
|
|
|
|
|
|
|
|
|
|
def restart():
|
|
|
|
return haOp('restart')
|
|
|
|
|
|
|
|
|
|
|
|
def reload():
|
|
|
|
return haOp('reload')
|
|
|
|
|
|
|
|
|
|
|
|
def runLog():
|
|
|
|
path = getConf()
|
|
|
|
content = mw.readFile(path)
|
|
|
|
rep = 'log\s*=\s*(.*)'
|
|
|
|
tmp = re.search(rep, content)
|
|
|
|
return tmp.groups()[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
else:
|
|
|
|
print('error')
|