mdserver-web/plugins/sphinx/index.py

485 lines
11 KiB
Python
Raw Permalink Normal View History

2019-01-15 10:13:21 -05:00
# coding:utf-8
import sys
import io
import os
import time
2019-01-16 00:27:51 -05:00
import re
2019-01-21 22:04:35 -05:00
import string
2019-01-16 02:35:22 -05:00
import subprocess
2024-11-23 17:02:41 -05:00
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
2019-01-15 10:13:21 -05:00
app_debug = False
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-01-15 10:13:21 -05:00
app_debug = True
def getPluginName():
return 'sphinx'
def getPluginDir():
2020-07-10 03:57:25 -04:00
return mw.getPluginDir() + '/' + getPluginName()
2019-01-15 10:13:21 -05:00
2024-05-19 02:58:14 -04:00
sys.path.append(getPluginDir() +"/class")
2019-01-15 10:13:21 -05:00
def getServerDir():
2020-07-10 03:57:25 -04:00
return mw.getServerDir() + '/' + getPluginName()
2019-01-15 10:13:21 -05:00
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
2019-01-16 00:02:05 -05:00
def getConfTpl():
path = getPluginDir() + "/conf/sphinx.conf"
return path
2019-01-15 10:13:21 -05:00
def getConf():
2019-01-16 00:02:05 -05:00
path = getServerDir() + "/sphinx.conf"
2019-01-15 10:13:21 -05:00
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
2019-01-22 01:40:04 -05:00
def checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
2020-07-10 03:57:25 -04:00
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
2019-01-22 01:40:04 -05:00
def configTpl():
path = getPluginDir() + '/tpl'
pathFile = os.listdir(path)
tmp = []
for one in pathFile:
file = path + '/' + one
tmp.append(file)
2020-07-10 03:57:25 -04:00
return mw.getJson(tmp)
2019-01-22 01:40:04 -05:00
def readConfigTpl():
args = getArgs()
data = checkArgs(args, ['file'])
if not data[0]:
return data[1]
2020-07-10 03:57:25 -04:00
content = mw.readFile(args['file'])
2019-01-22 01:40:04 -05:00
content = contentReplace(content)
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'ok', content)
2019-01-22 01:40:04 -05:00
2019-01-16 00:02:05 -05:00
def contentReplace(content):
2020-07-10 03:57:25 -04:00
service_path = mw.getServerDir()
2024-11-24 12:13:47 -05:00
content = content.replace('{$ROOT_PATH}', mw.getFatherDir())
2019-01-16 00:02:05 -05:00
content = content.replace('{$SERVER_PATH}', service_path)
2025-07-21 04:08:23 -04:00
content = content.replace('{$SERVER_APP}', service_path + '/'+getPluginName())
2019-01-16 00:27:51 -05:00
return content
2019-01-16 00:02:05 -05:00
2019-01-15 10:13:21 -05:00
def status():
2020-07-10 03:57:25 -04:00
data = mw.execShell(
2023-11-09 16:05:49 -05:00
"ps -ef|grep sphinx |grep -v grep | grep -v mdserver-web | awk '{print $2}'")
# print(data)
2019-01-15 10:13:21 -05:00
if data[0] == '':
return 'stop'
return 'start'
2019-01-16 01:56:26 -05:00
def mkdirAll():
2020-07-10 03:57:25 -04:00
content = mw.readFile(getConf())
2024-07-24 01:08:04 -04:00
rep = r'path\s*=\s*(.*)'
2019-01-16 01:56:26 -05:00
p = re.compile(rep)
tmp = p.findall(content)
2019-03-20 04:49:17 -04:00
2019-01-16 01:56:26 -05:00
for x in tmp:
2019-03-20 04:49:17 -04:00
if x.find('binlog') != -1:
2020-07-10 03:57:25 -04:00
mw.execShell('mkdir -p ' + x)
2019-03-20 04:49:17 -04:00
else:
2020-07-10 03:57:25 -04:00
mw.execShell('mkdir -p ' + os.path.dirname(x))
2019-01-16 01:56:26 -05:00
2019-01-15 10:13:21 -05:00
def initDreplace():
file_tpl = getInitDTpl()
2024-11-29 05:21:12 -05:00
service_path = mw.getServerDir()
2019-01-15 10:13:21 -05:00
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/' + getPluginName()
# initd replace
2019-02-02 13:05:17 -05:00
if not os.path.exists(file_bin):
2020-07-10 03:57:25 -04:00
content = mw.readFile(file_tpl)
2019-02-02 13:05:17 -05:00
content = contentReplace(content)
2020-07-10 03:57:25 -04:00
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
2019-01-15 10:13:21 -05:00
# config replace
2019-01-16 00:02:05 -05:00
conf_bin = getConf()
if not os.path.exists(conf_bin):
2020-07-10 03:57:25 -04:00
conf_content = mw.readFile(getConfTpl())
2019-01-16 00:02:05 -05:00
conf_content = contentReplace(conf_content)
2020-07-10 03:57:25 -04:00
mw.writeFile(getServerDir() + '/sphinx.conf', conf_content)
2019-01-15 10:13:21 -05:00
2022-06-19 07:02:59 -04:00
# systemd
2022-07-11 01:49:25 -04:00
systemDir = mw.systemdCfgDir()
2022-06-19 07:02:59 -04:00
systemService = systemDir + '/sphinx.service'
systemServiceTpl = getPluginDir() + '/init.d/sphinx.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')
2019-01-16 01:56:26 -05:00
mkdirAll()
2019-01-15 10:13:21 -05:00
return file_bin
2019-03-20 04:49:17 -04:00
def checkIndexSph():
2020-07-10 03:57:25 -04:00
content = mw.readFile(getConf())
2024-07-24 01:08:04 -04:00
rep = r'path\s*=\s*(.*)'
2019-03-20 04:49:17 -04:00
p = re.compile(rep)
tmp = p.findall(content)
for x in tmp:
if x.find('binlog') != -1:
continue
else:
2020-07-10 03:57:25 -04:00
p = x + '.sph'
if os.path.exists(p):
2019-03-20 04:49:17 -04:00
return False
return True
2020-07-10 03:57:25 -04:00
2022-06-19 07:02:59 -04:00
def sphOp(method):
2019-01-15 10:13:21 -05:00
file = initDreplace()
2019-03-20 04:49:17 -04:00
2022-06-19 07:02:59 -04:00
if not mw.isAppleSystem():
2025-07-21 04:08:23 -04:00
data = mw.execShell('systemctl ' + method + ' ' + getPluginName())
2022-06-19 07:02:59 -04:00
if data[1] == '':
return 'ok'
return 'fail'
2019-03-20 04:49:17 -04:00
2022-06-19 07:02:59 -04:00
data = mw.execShell(file + ' ' + method)
2019-01-16 01:56:26 -05:00
if data[1] == '':
2019-01-15 10:13:21 -05:00
return 'ok'
2019-01-16 01:56:26 -05:00
return data[1]
2019-01-15 10:13:21 -05:00
2022-06-19 07:02:59 -04:00
def start():
2024-06-26 05:32:22 -04:00
import tool_cron
tool_cron.createBgTask()
2022-06-19 07:02:59 -04:00
return sphOp('start')
2019-01-15 10:13:21 -05:00
def stop():
2024-06-26 05:52:41 -04:00
import tool_cron
tool_cron.removeBgTask()
2022-06-19 07:02:59 -04:00
return sphOp('stop')
2019-01-15 10:13:21 -05:00
def restart():
2022-06-19 07:02:59 -04:00
return sphOp('restart')
2019-01-15 10:13:21 -05:00
def reload():
2022-06-19 07:02:59 -04:00
return sphOp('reload')
2019-01-15 10:13:21 -05:00
2019-01-16 01:56:26 -05:00
def rebuild():
file = initDreplace()
2024-05-21 04:57:05 -04:00
cmd = file + ' rebuild'
2024-05-18 13:32:31 -04:00
data = mw.execShell(cmd)
2024-05-18 14:36:34 -04:00
if data[0].find('successfully')<0:
2024-05-18 14:35:03 -04:00
return data[0].replace("\n","<br/>")
2019-01-16 02:35:22 -05:00
return 'ok'
2019-01-16 01:56:26 -05:00
2019-01-15 10:13:21 -05:00
def initdStatus():
2022-06-19 07:02:59 -04:00
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'
2019-01-15 10:13:21 -05:00
def initdInstall():
2022-06-19 07:02:59 -04:00
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl enable sphinx')
2019-01-15 10:13:21 -05:00
return 'ok'
def initdUinstall():
2022-06-19 07:02:59 -04:00
if mw.isAppleSystem():
return "Apple Computer does not support"
mw.execShell('systemctl disable sphinx')
2019-01-15 10:13:21 -05:00
return 'ok'
2019-01-16 00:27:51 -05:00
def runLog():
path = getConf()
2020-07-10 03:57:25 -04:00
content = mw.readFile(path)
2024-07-24 01:08:04 -04:00
rep = r'log\s*=\s*(.*)'
2019-01-16 00:27:51 -05:00
tmp = re.search(rep, content)
return tmp.groups()[0]
2019-01-17 02:31:57 -05:00
def getPort():
path = getConf()
2020-07-10 03:57:25 -04:00
content = mw.readFile(path)
2024-07-24 01:08:04 -04:00
rep = r'listen\s*=\s*(.*)'
2019-01-17 02:31:57 -05:00
tmp = re.search(rep, content)
return tmp.groups()[0]
2019-01-16 00:27:51 -05:00
def queryLog():
path = getConf()
2020-07-10 03:57:25 -04:00
content = mw.readFile(path)
2024-07-24 01:08:04 -04:00
rep = r'query_log\s*=\s*(.*)'
2019-01-16 00:27:51 -05:00
tmp = re.search(rep, content)
return tmp.groups()[0]
2019-01-17 02:31:57 -05:00
def runStatus():
s = status()
if s != 'start':
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '没有启动程序')
2019-01-17 02:31:57 -05:00
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]
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'ok', rData)
2019-01-17 02:31:57 -05:00
2019-01-21 02:05:56 -05:00
2019-03-20 04:49:17 -04:00
def sphinxConfParse():
2019-01-21 02:05:56 -05:00
file = getConf()
2019-01-21 22:04:35 -05:00
bin_dir = getServerDir()
2020-07-10 03:57:25 -04:00
content = mw.readFile(file)
2024-07-24 02:33:47 -04:00
rep = r'index\s(.*)'
2019-01-21 22:04:35 -05:00
sindex = re.findall(rep, content)
indexlen = len(sindex)
2019-03-20 04:49:17 -04:00
cmd = {}
2024-05-21 07:20:07 -04:00
cmd['cmd'] = bin_dir + '/bin/bin/indexer -c ' + bin_dir + '/sphinx.conf'
cmd['index'] = []
cmd_index = []
cmd_delta = []
2019-01-21 22:04:35 -05:00
if indexlen > 0:
for x in range(indexlen):
2024-05-21 07:20:07 -04:00
name = sindex[x].strip()
if name == '':
continue
if name.find(':') != -1:
cmd_delta.append(name.strip())
2019-01-21 22:04:35 -05:00
else:
2024-05-21 07:20:07 -04:00
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
2019-01-21 22:04:35 -05:00
2024-05-21 07:20:07 -04:00
cmd['index'].append(val)
2019-03-20 04:49:17 -04:00
return cmd
2019-01-21 22:04:35 -05:00
2020-07-10 03:57:25 -04:00
2019-03-20 04:49:17 -04:00
def sphinxCmd():
data = sphinxConfParse()
if 'index' in data:
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'ok', data)
2019-01-21 22:04:35 -05:00
else:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, 'no index')
2019-01-21 22:04:35 -05:00
2024-05-21 04:57:05 -04:00
def makeDbToSphinxTest():
2024-05-20 02:11:27 -04:00
conf_file = getConf()
2024-05-19 04:24:33 -04:00
import sphinx_make
2024-05-20 02:11:27 -04:00
sph_make = sphinx_make.sphinxMake()
conf = sph_make.makeSqlToSphinxAll()
mw.writeFile(conf_file,conf)
2024-05-19 04:24:33 -04:00
print(conf)
# makeSqlToSphinxTable()
2024-05-19 05:37:47 -04:00
return True
2024-05-19 02:58:14 -04:00
2024-05-21 04:57:05 -04:00
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']
2024-05-21 11:41:56 -04:00
if is_cover != 'yes':
return mw.returnJson(False,'暂时仅支持覆盖!')
2024-05-21 04:57:05 -04:00
sph_file = getConf()
import sphinx_make
sph_make = sphinx_make.sphinxMake()
2024-05-21 07:43:25 -04:00
version_pl = getServerDir() + "/version.pl"
if os.path.exists(version_pl):
version = mw.readFile(version_pl).strip()
sph_make.setVersion(version)
2024-05-21 04:57:05 -04:00
if not sph_make.checkDbName(db):
return mw.returnJson(False,'保留数据库名称,不可用!')
2024-05-21 05:12:39 -04:00
is_delta_bool = False
if is_delta == 'yes':
is_delta_bool = True
2024-05-21 04:57:05 -04:00
if is_cover == 'yes':
tables = tables.split(',')
2024-05-21 05:12:39 -04:00
content = sph_make.makeSqlToSphinx(db, tables, is_delta_bool)
2024-05-21 04:57:05 -04:00
mw.writeFile(sph_file,content)
2025-07-22 23:58:35 -04:00
mkdirAll()
2024-05-21 04:57:05 -04:00
return mw.returnJson(True,'设置成功!')
return mw.returnJson(True,'测试中')
2024-05-19 02:58:14 -04:00
2024-06-26 07:12:19 -04:00
# 全量更新
2024-06-26 06:48:40 -04:00
def updateAll():
2024-06-26 06:52:30 -04:00
data = sphinxConfParse()
2024-06-26 06:55:22 -04:00
cmd = data['cmd']
2024-06-26 07:12:19 -04:00
if not 'index' in data:
return '无更新'
2024-06-26 06:55:22 -04:00
index = data['index']
2024-06-26 07:01:30 -04:00
for x in range(len(index)):
cmd_index = cmd + ' ' + index[x]['index'] + ' --rotate'
2024-06-26 07:24:35 -04:00
print(cmd_index)
2024-06-26 07:01:30 -04:00
os.system(cmd_index)
2024-06-26 07:12:19 -04:00
return ''
#增量更新
def updateDelta():
data = sphinxConfParse()
cmd = data['cmd']
if not 'index' in data:
return '无更新'
index = data['index']
for x in range(len(index)):
2024-06-26 07:24:35 -04:00
if 'delta' in index[x]:
cmd_index = cmd + ' ' + index[x]['delta'] + ' --rotate'
print(cmd_index)
os.system(cmd_index)
2024-06-26 07:26:14 -04:00
cmd_index_merge = cmd + ' --merge ' + index[x]['index'] + ' ' + index[x]['delta'] + ' --rotate'
print(cmd_index_merge)
os.system(cmd_index_merge)
else:
2024-06-26 07:27:13 -04:00
print(index[x]['index'],'no delta')
2024-06-26 07:26:14 -04:00
2024-06-26 07:12:19 -04:00
return ''
2024-06-26 06:55:22 -04:00
2024-07-24 02:48:47 -04:00
def installPreInspection(version):
2024-07-24 02:46:10 -04:00
data = mw.execShell('arch')
if data[0].strip().startswith('aarch'):
return '不支持aarch架构'
return 'ok'
2019-01-15 10:13:21 -05:00
if __name__ == "__main__":
2024-07-24 02:48:47 -04:00
version = "3.1.1"
version_pl = getServerDir() + "/version.pl"
if os.path.exists(version_pl):
version = mw.readFile(version_pl).strip()
2019-01-15 10:13:21 -05:00
func = sys.argv[1]
if func == 'status':
2021-05-08 13:16:41 -04:00
print(status())
2019-01-15 10:13:21 -05:00
elif func == 'start':
2021-05-08 13:16:41 -04:00
print(start())
2019-01-15 10:13:21 -05:00
elif func == 'stop':
2021-05-08 13:16:41 -04:00
print(stop())
2019-01-15 10:13:21 -05:00
elif func == 'restart':
2021-05-08 13:16:41 -04:00
print(restart())
2019-01-15 10:13:21 -05:00
elif func == 'reload':
2021-05-08 13:16:41 -04:00
print(reload())
2019-01-16 01:56:26 -05:00
elif func == 'rebuild':
2021-05-08 13:16:41 -04:00
print(rebuild())
2019-01-15 10:13:21 -05:00
elif func == 'initd_status':
2021-05-08 13:16:41 -04:00
print(initdStatus())
2019-01-15 10:13:21 -05:00
elif func == 'initd_install':
2021-05-08 13:16:41 -04:00
print(initdInstall())
2019-01-15 10:13:21 -05:00
elif func == 'initd_uninstall':
2021-05-08 13:16:41 -04:00
print(initdUinstall())
2024-07-24 02:46:10 -04:00
elif func == 'install_pre_inspection':
print(installPreInspection(version))
2019-01-15 10:13:21 -05:00
elif func == 'conf':
2021-05-08 13:16:41 -04:00
print(getConf())
2019-01-22 01:40:04 -05:00
elif func == 'config_tpl':
2021-05-08 13:16:41 -04:00
print(configTpl())
2019-01-22 01:40:04 -05:00
elif func == 'read_config_tpl':
2021-05-08 13:16:41 -04:00
print(readConfigTpl())
2019-01-15 10:13:21 -05:00
elif func == 'run_log':
2021-05-08 13:16:41 -04:00
print(runLog())
2019-01-16 00:27:51 -05:00
elif func == 'query_log':
2021-05-08 13:16:41 -04:00
print(queryLog())
2019-01-17 02:31:57 -05:00
elif func == 'run_status':
2021-05-08 13:16:41 -04:00
print(runStatus())
2019-01-21 02:05:56 -05:00
elif func == 'sphinx_cmd':
2021-05-08 13:16:41 -04:00
print(sphinxCmd())
2024-05-19 02:58:14 -04:00
elif func == 'db_to_sphinx':
print(makeDbToSphinx())
2024-06-26 06:48:40 -04:00
elif func == 'update_all':
print(updateAll())
2024-06-26 07:12:19 -04:00
elif func == 'update_delta':
print(updateDelta())
2019-01-15 10:13:21 -05:00
else:
2021-05-08 13:16:41 -04:00
print('error')