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

427 lines
9.8 KiB

6 years ago
# coding:utf-8
import sys
import io
import os
import time
6 years ago
import re
6 years ago
import string
6 years ago
import subprocess
6 years ago
sys.path.append(os.getcwd() + "/class/core")
import mw
6 years ago
app_debug = False
if mw.isAppleSystem():
6 years ago
app_debug = True
def getPluginName():
return 'sphinx'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
6 years ago
12 months ago
sys.path.append(getPluginDir() +"/class")
6 years ago
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
6 years ago
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
6 years ago
def getConfTpl():
path = getPluginDir() + "/conf/sphinx.conf"
return path
6 years ago
def getConf():
6 years ago
path = getServerDir() + "/sphinx.conf"
6 years ago
return path
def getInitDTpl():
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
return path
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
6 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'))
6 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)
6 years ago
def readConfigTpl():
args = getArgs()
data = checkArgs(args, ['file'])
if not data[0]:
return data[1]
content = mw.readFile(args['file'])
6 years ago
content = contentReplace(content)
return mw.returnJson(True, 'ok', content)
6 years ago
6 years ago
def contentReplace(content):
service_path = mw.getServerDir()
content = content.replace('{$ROOT_PATH}', mw.getRootDir())
6 years ago
content = content.replace('{$SERVER_PATH}', service_path)
6 years ago
content = content.replace('{$SERVER_APP}', service_path + '/sphinx')
return content
6 years ago
6 years ago
def status():
data = mw.execShell(
2 years ago
"ps -ef|grep sphinx |grep -v grep | grep -v mdserver-web | awk '{print $2}'")
# print(data)
6 years ago
if data[0] == '':
return 'stop'
return 'start'
6 years ago
def mkdirAll():
content = mw.readFile(getConf())
6 years ago
rep = 'path\s*=\s*(.*)'
p = re.compile(rep)
tmp = p.findall(content)
6 years ago
6 years ago
for x in tmp:
6 years ago
if x.find('binlog') != -1:
mw.execShell('mkdir -p ' + x)
6 years ago
else:
mw.execShell('mkdir -p ' + os.path.dirname(x))
6 years ago
6 years ago
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
6 years ago
if not os.path.exists(file_bin):
content = mw.readFile(file_tpl)
6 years ago
content = contentReplace(content)
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
6 years ago
# config replace
6 years ago
conf_bin = getConf()
if not os.path.exists(conf_bin):
conf_content = mw.readFile(getConfTpl())
6 years ago
conf_content = contentReplace(conf_content)
mw.writeFile(getServerDir() + '/sphinx.conf', conf_content)
6 years ago
# systemd
3 years ago
systemDir = mw.systemdCfgDir()
systemService = systemDir + '/sphinx.service'
systemServiceTpl = getPluginDir() + '/init.d/sphinx.service.tpl'
if os.path.exists(systemDir) and not os.path.exists(systemService):
service_path = mw.getServerDir()
se_content = mw.readFile(systemServiceTpl)
se_content = se_content.replace('{$SERVER_PATH}', service_path)
mw.writeFile(systemService, se_content)
mw.execShell('systemctl daemon-reload')
6 years ago
mkdirAll()
6 years ago
return file_bin
6 years ago
def checkIndexSph():
content = mw.readFile(getConf())
6 years ago
rep = 'path\s*=\s*(.*)'
p = re.compile(rep)
tmp = p.findall(content)
for x in tmp:
if x.find('binlog') != -1:
continue
else:
p = x + '.sph'
if os.path.exists(p):
6 years ago
return False
return True
def sphOp(method):
6 years ago
file = initDreplace()
6 years ago
if not mw.isAppleSystem():
data = mw.execShell('systemctl ' + method + ' sphinx')
if data[1] == '':
return 'ok'
return 'fail'
6 years ago
data = mw.execShell(file + ' ' + method)
6 years ago
if data[1] == '':
6 years ago
return 'ok'
6 years ago
return data[1]
6 years ago
def start():
11 months ago
import tool_cron
tool_cron.createBgTask()
return sphOp('start')
6 years ago
def stop():
return sphOp('stop')
6 years ago
def restart():
return sphOp('restart')
6 years ago
def reload():
return sphOp('reload')
6 years ago
6 years ago
def rebuild():
file = initDreplace()
12 months ago
cmd = file + ' rebuild'
12 months ago
data = mw.execShell(cmd)
12 months ago
if data[0].find('successfully')<0:
12 months ago
return data[0].replace("\n","<br/>")
6 years ago
return 'ok'
6 years ago
6 years ago
def initdStatus():
if mw.isAppleSystem():
return "Apple Computer does not support"
shell_cmd = 'systemctl status sphinx | grep loaded | grep "enabled;"'
data = mw.execShell(shell_cmd)
if data[0] == '':
return 'fail'
return 'ok'
6 years ago
def initdInstall():
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl enable sphinx')
6 years ago
return 'ok'
def initdUinstall():
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl disable sphinx')
6 years ago
return 'ok'
6 years ago
def runLog():
path = getConf()
content = mw.readFile(path)
6 years ago
rep = 'log\s*=\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0]
6 years ago
def getPort():
path = getConf()
content = mw.readFile(path)
6 years ago
rep = 'listen\s*=\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0]
6 years ago
def queryLog():
path = getConf()
content = mw.readFile(path)
6 years ago
rep = 'query_log\s*=\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0]
6 years ago
def runStatus():
s = status()
if s != 'start':
return mw.returnJson(False, '没有启动程序')
6 years ago
sys.path.append(getPluginDir() + "/class")
import sphinxapi
sh = sphinxapi.SphinxClient()
port = getPort()
sh.SetServer('127.0.0.1', port)
info_status = sh.Status()
rData = {}
for x in range(len(info_status)):
rData[info_status[x][0]] = info_status[x][1]
return mw.returnJson(True, 'ok', rData)
6 years ago
6 years ago
6 years ago
def sphinxConfParse():
6 years ago
file = getConf()
6 years ago
bin_dir = getServerDir()
content = mw.readFile(file)
6 years ago
rep = 'index\s(.*)'
6 years ago
sindex = re.findall(rep, content)
indexlen = len(sindex)
6 years ago
cmd = {}
12 months ago
cmd['cmd'] = bin_dir + '/bin/bin/indexer -c ' + bin_dir + '/sphinx.conf'
cmd['index'] = []
cmd_index = []
cmd_delta = []
6 years ago
if indexlen > 0:
for x in range(indexlen):
12 months ago
name = sindex[x].strip()
if name == '':
continue
if name.find(':') != -1:
cmd_delta.append(name.strip())
6 years ago
else:
12 months ago
cmd_index.append(name.strip())
# print(cmd_index)
# print(cmd_delta)
for ci in cmd_index:
val = {}
val['index'] = ci
for cd in cmd_delta:
cd = cd.replace(" ", '')
if cd.find(":"+ci) > -1:
val['delta'] = cd.split(":")[0].strip()
break
6 years ago
12 months ago
cmd['index'].append(val)
6 years ago
return cmd
6 years ago
6 years ago
def sphinxCmd():
data = sphinxConfParse()
if 'index' in data:
return mw.returnJson(True, 'ok', data)
6 years ago
else:
return mw.returnJson(False, 'no index')
6 years ago
12 months ago
def makeDbToSphinxTest():
12 months ago
conf_file = getConf()
12 months ago
import sphinx_make
12 months ago
sph_make = sphinx_make.sphinxMake()
conf = sph_make.makeSqlToSphinxAll()
mw.writeFile(conf_file,conf)
12 months ago
print(conf)
# makeSqlToSphinxTable()
12 months ago
return True
12 months ago
12 months ago
def makeDbToSphinx():
args = getArgs()
check = checkArgs(args, ['db','tables','is_delta','is_cover'])
if not check[0]:
return check[1]
db = args['db']
tables = args['tables']
is_delta = args['is_delta']
is_cover = args['is_cover']
12 months ago
if is_cover != 'yes':
return mw.returnJson(False,'暂时仅支持覆盖!')
12 months ago
sph_file = getConf()
import sphinx_make
sph_make = sphinx_make.sphinxMake()
12 months ago
version_pl = getServerDir() + "/version.pl"
if os.path.exists(version_pl):
version = mw.readFile(version_pl).strip()
sph_make.setVersion(version)
12 months ago
if not sph_make.checkDbName(db):
return mw.returnJson(False,'保留数据库名称,不可用!')
12 months ago
is_delta_bool = False
if is_delta == 'yes':
is_delta_bool = True
12 months ago
if is_cover == 'yes':
tables = tables.split(',')
12 months ago
content = sph_make.makeSqlToSphinx(db, tables, is_delta_bool)
12 months ago
mw.writeFile(sph_file,content)
return mw.returnJson(True,'设置成功!')
return mw.returnJson(True,'测试中')
12 months ago
6 years ago
6 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
4 years ago
print(status())
6 years ago
elif func == 'start':
4 years ago
print(start())
6 years ago
elif func == 'stop':
4 years ago
print(stop())
6 years ago
elif func == 'restart':
4 years ago
print(restart())
6 years ago
elif func == 'reload':
4 years ago
print(reload())
6 years ago
elif func == 'rebuild':
4 years ago
print(rebuild())
6 years ago
elif func == 'initd_status':
4 years ago
print(initdStatus())
6 years ago
elif func == 'initd_install':
4 years ago
print(initdInstall())
6 years ago
elif func == 'initd_uninstall':
4 years ago
print(initdUinstall())
6 years ago
elif func == 'conf':
4 years ago
print(getConf())
6 years ago
elif func == 'config_tpl':
4 years ago
print(configTpl())
6 years ago
elif func == 'read_config_tpl':
4 years ago
print(readConfigTpl())
6 years ago
elif func == 'run_log':
4 years ago
print(runLog())
6 years ago
elif func == 'query_log':
4 years ago
print(queryLog())
6 years ago
elif func == 'run_status':
4 years ago
print(runStatus())
6 years ago
elif func == 'sphinx_cmd':
4 years ago
print(sphinxCmd())
12 months ago
elif func == 'db_to_sphinx':
print(makeDbToSphinx())
6 years ago
else:
4 years ago
print('error')