mdserver-web/plugins/sphinx/index.py

362 lines
8.0 KiB
Python
Raw 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
2019-01-15 10:13:21 -05:00
sys.path.append(os.getcwd() + "/class/core")
2020-07-10 03:57:25 -04:00
import 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
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()
content = content.replace('{$ROOT_PATH}', mw.getRootDir())
2019-01-16 00:02:05 -05:00
content = content.replace('{$SERVER_PATH}', service_path)
2019-01-16 00:27:51 -05:00
content = content.replace('{$SERVER_APP}', service_path + '/sphinx')
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(
2019-01-16 00:02:05 -05:00
"ps -ef|grep sphinx |grep -v grep | grep -v python | awk '{print $2}'")
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())
2019-01-16 01:56:26 -05:00
rep = 'path\s*=\s*(.*)'
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()
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
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
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())
2019-03-20 04:49:17 -04:00
rep = 'path\s*=\s*(.*)'
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
2019-01-15 10:13:21 -05:00
def start():
file = initDreplace()
2019-03-20 04:49:17 -04:00
data = sphinxConfParse()
2020-07-10 03:57:25 -04:00
if 'index' in data:
2019-03-20 04:49:17 -04:00
if checkIndexSph():
rebuild()
time.sleep(5)
else:
return '配置不正确!'
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' start')
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
def stop():
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' stop')
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
def restart():
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' restart')
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
def reload():
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' reload')
2019-01-15 10:13:21 -05:00
if data[1] == '':
return 'ok'
return 'fail'
2019-01-16 01:56:26 -05:00
def rebuild():
file = initDreplace()
2019-01-16 02:35:22 -05:00
subprocess.Popen(file + ' rebuild &',
stdout=subprocess.PIPE, shell=True)
2020-07-10 03:57:25 -04:00
# data = mw.execShell(file + ' rebuild')
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():
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-01-15 10:13:21 -05:00
return "Apple Computer does not support"
initd_bin = getInitDFile()
if os.path.exists(initd_bin):
return 'ok'
return 'fail'
def initdInstall():
import shutil
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-01-15 10:13:21 -05:00
return "Apple Computer does not support"
source_bin = initDreplace()
initd_bin = getInitDFile()
shutil.copyfile(source_bin, initd_bin)
2020-07-10 03:57:25 -04:00
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
2019-01-15 10:13:21 -05:00
return 'ok'
def initdUinstall():
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-01-15 10:13:21 -05:00
return "Apple Computer does not support"
initd_bin = getInitDFile()
os.remove(initd_bin)
2020-07-10 03:57:25 -04:00
mw.execShell('chkconfig --del ' + getPluginName())
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)
2019-01-16 00:27:51 -05:00
rep = 'log\s*=\s*(.*)'
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)
2019-01-17 02:31:57 -05:00
rep = 'listen\s*=\s*(.*)'
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)
2019-01-16 00:27:51 -05:00
rep = 'query_log\s*=\s*(.*)'
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)
2019-01-21 10:16:16 -05:00
rep = '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 = {}
2019-01-21 22:04:35 -05:00
if indexlen > 0:
cmd_index = []
cmd_delta = []
for x in range(indexlen):
2021-11-12 07:13:29 -05:00
if sindex[x].find(':') != -1:
2019-01-22 01:40:04 -05:00
cmd_delta.append(sindex[x])
2019-01-21 22:04:35 -05:00
else:
cmd_index.append(sindex[x])
cmd['index'] = cmd_index
cmd['delta'] = cmd_delta
2019-01-22 01:40:04 -05:00
cmd['cmd'] = bin_dir + '/bin/bin/indexer -c ' + bin_dir + '/sphinx.conf'
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
2019-01-21 02:05:56 -05:00
2019-01-15 10:13:21 -05:00
if __name__ == "__main__":
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())
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())
2019-01-15 10:13:21 -05:00
else:
2021-05-08 13:16:41 -04:00
print('error')