mdserver-web/plugins/rsyncd/index.py

377 lines
9.3 KiB
Python
Raw Normal View History

2019-03-05 06:20:04 -05:00
# coding: utf-8
import time
import random
import os
import json
import re
import sys
sys.path.append(os.getcwd() + "/class/core")
2020-07-10 03:57:25 -04:00
import mw
2019-03-05 06:20:04 -05:00
app_debug = False
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-03-05 06:20:04 -05:00
app_debug = True
def getPluginName():
return 'rsyncd'
2019-09-12 10:38:34 -04:00
def getInitDTpl():
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
return path
2019-03-05 06:20:04 -05:00
def getPluginDir():
2020-07-10 03:57:25 -04:00
return mw.getPluginDir() + '/' + getPluginName()
2019-03-05 06:20:04 -05:00
def getServerDir():
2020-07-10 03:57:25 -04:00
return mw.getServerDir() + '/' + getPluginName()
2019-03-05 06:20:04 -05:00
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:
2020-07-10 03:57:25 -04:00
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
2019-03-05 06:20:04 -05:00
2019-09-12 10:38:34 -04:00
def contentReplace(content):
2020-07-10 03:57:25 -04:00
service_path = mw.getServerDir()
2019-09-12 10:38:34 -04:00
content = content.replace('{$SERVER_PATH}', service_path)
return content
2019-03-05 06:20:04 -05:00
def status():
2020-07-10 03:57:25 -04:00
data = mw.execShell(
2019-03-06 01:33:32 -05:00
"ps -ef|grep rsync |grep -v grep | grep -v python | awk '{print $2}'")
2019-03-05 06:20:04 -05:00
if data[0] == '':
return 'stop'
return 'start'
2019-03-06 02:07:04 -05:00
def appConf():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-03-06 02:07:04 -05:00
return getServerDir() + '/rsyncd.conf'
return '/etc/rsyncd.conf'
2019-03-06 07:31:29 -05:00
def appConfPwd():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-03-06 07:31:29 -05:00
return getServerDir() + '/rsyncd.passwd'
return '/etc/rsyncd.passwd'
2019-03-06 02:07:04 -05:00
def getLog():
conf_path = appConf()
2020-07-10 03:57:25 -04:00
conf = mw.readFile(conf_path)
2019-03-06 02:07:04 -05:00
rep = 'log file\s*=\s*(.*)'
tmp = re.search(rep, conf)
if not tmp:
return ''
return tmp.groups()[0]
2019-09-12 10:51:51 -04:00
def initDreplace():
2019-03-06 02:07:04 -05:00
conf_path = appConf()
2020-07-10 03:57:25 -04:00
conf = mw.readFile(conf_path)
2019-04-02 03:13:41 -04:00
2019-09-12 06:48:57 -04:00
compile_sub = re.compile('^#(.*)', re.M)
2019-04-02 03:13:41 -04:00
conf = compile_sub.sub('', conf)
2019-03-06 02:07:04 -05:00
conf_tpl_path = getPluginDir() + '/conf/rsyncd.conf'
if conf.strip() == '':
2020-07-10 03:57:25 -04:00
content = mw.readFile(conf_tpl_path)
mw.writeFile(conf_path, content)
2019-03-06 07:31:29 -05:00
confpwd_path = appConfPwd()
if not os.path.exists(confpwd_path):
2020-07-10 03:57:25 -04:00
mw.writeFile(confpwd_path, '')
mw.execShell('chmod 0600 ' + confpwd_path)
2019-03-06 02:07:04 -05:00
2019-09-12 10:38:34 -04:00
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/' + getPluginName()
2019-03-06 02:07:04 -05:00
2019-09-12 10:38:34 -04:00
file_tpl = getInitDTpl()
# initd replace
if not os.path.exists(file_bin):
2020-07-10 03:57:25 -04:00
content = mw.readFile(file_tpl)
2019-09-12 10:38:34 -04:00
content = contentReplace(content)
2020-07-10 03:57:25 -04:00
mw.writeFile(file_bin, content)
mw.execShell('chmod +x ' + file_bin)
2019-03-06 02:07:04 -05:00
2019-09-12 12:02:55 -04:00
if os.path.exists('/usr/lib/systemd/system/rsyncd.service'):
2020-07-10 03:57:25 -04:00
mw.execShell('rm -rf /usr/lib/systemd/system/rsyncd*')
2019-09-12 12:02:55 -04:00
2019-09-12 13:42:30 -04:00
rlog = getLog()
2019-09-12 13:43:05 -04:00
if os.path.exists(rlog):
2020-07-10 03:57:25 -04:00
mw.writeFile(rlog, '')
2019-09-12 10:38:34 -04:00
return file_bin
2019-03-05 06:20:04 -05:00
2019-09-12 06:48:57 -04:00
2019-09-12 10:38:34 -04:00
def start():
2019-09-12 10:51:51 -04:00
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' start')
2019-09-12 10:38:34 -04:00
if data[1] == '':
return 'ok'
return 'fail'
2019-03-05 06:20:04 -05:00
def stop():
2019-09-12 10:51:51 -04:00
file = initDreplace()
2020-07-10 03:57:25 -04:00
data = mw.execShell(file + ' stop')
2019-09-12 10:38:34 -04:00
if data[1] == '':
return 'ok'
return 'fail'
2019-03-05 06:20:04 -05:00
def restart():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-03-05 06:20:04 -05:00
return "Apple Computer does not support"
2019-09-12 06:48:57 -04:00
stop()
start()
return 'ok'
2019-03-05 06:20:04 -05:00
def reload():
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-03-05 06:20:04 -05:00
return "Apple Computer does not support"
2020-07-10 03:57:25 -04:00
# data = mw.execShell('systemctl reload rsyncd.service')
2019-09-12 06:48:57 -04:00
# if data[1] == '':
# return 'ok'
# return 'fail'
stop()
start()
return 'ok'
2019-03-05 06:20:04 -05:00
def initdStatus():
2019-09-12 10:38:34 -04:00
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-09-12 10:38:34 -04:00
return "Apple Computer does not support"
2019-03-06 07:31:29 -05:00
2019-09-12 10:38:34 -04:00
initd_bin = getInitDFile()
if os.path.exists(initd_bin):
return 'ok'
return 'fail'
2019-03-05 06:20:04 -05:00
2019-09-12 06:48:57 -04:00
2019-03-05 06:20:04 -05:00
def initdInstall():
2019-09-12 10:38:34 -04:00
import shutil
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-09-12 10:38:34 -04:00
return "Apple Computer does not support"
2019-09-12 10:49:35 -04:00
p_bin = initDreplace()
2019-09-12 10:38:34 -04:00
initd_bin = getInitDFile()
2019-09-12 10:49:35 -04:00
shutil.copyfile(p_bin, initd_bin)
2020-07-10 03:57:25 -04:00
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
2019-03-06 07:40:11 -05:00
return 'ok'
2019-03-05 06:20:04 -05:00
def initdUinstall():
2019-09-12 10:38:34 -04:00
if not app_debug:
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-09-12 10:38:34 -04: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-09-12 10:38:34 -04:00
return 'ok'
2019-03-05 06:20:04 -05:00
2019-03-06 05:47:43 -05:00
def getRecListData():
path = appConf()
2020-07-10 03:57:25 -04:00
content = mw.readFile(path)
2019-03-06 05:47:43 -05:00
flist = re.findall("\[(.*)\]", content)
flist_len = len(flist)
ret_list = []
for i in range(flist_len):
tmp = {}
tmp['name'] = flist[i]
n = i + 1
reg = ''
if n == flist_len:
reg = '\[' + flist[i] + '\](.*)\[?'
else:
reg = '\[' + flist[i] + '\](.*)\[' + flist[n] + '\]'
t1 = re.search(reg, content, re.S)
if t1:
args = t1.groups()[0]
# print 'args start', args, 'args_end'
t2 = re.findall('\s*(.*)\s*=\s*(.*)', args, re.M)
for i in range(len(t2)):
tmp[t2[i][0].strip()] = t2[i][1]
ret_list.append(tmp)
return ret_list
def getRecList():
ret_list = getRecListData()
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'ok', ret_list)
2019-03-06 05:47:43 -05:00
2019-03-06 07:31:29 -05:00
def getUPwdList():
pwd_path = appConfPwd()
2020-07-10 03:57:25 -04:00
pwd_content = mw.readFile(pwd_path)
2019-03-06 07:31:29 -05:00
plist = pwd_content.strip().split('\n')
plist_len = len(plist)
data = {}
for x in range(plist_len):
tmp = plist[x].split(':')
data[tmp[0]] = tmp[1]
return data
2019-03-06 05:47:43 -05:00
def addRec():
args = getArgs()
2019-03-06 07:31:29 -05:00
data = checkArgs(args, ['name', 'path', 'pwd', 'ps'])
2019-03-06 05:47:43 -05:00
if not data[0]:
return data[1]
args_name = args['name']
2019-03-06 07:31:29 -05:00
args_pwd = args['pwd']
2019-03-06 05:47:43 -05:00
args_path = args['path']
args_ps = args['ps']
2019-03-06 07:31:29 -05:00
pwd_path = appConfPwd()
2020-07-10 03:57:25 -04:00
pwd_content = mw.readFile(pwd_path)
2019-03-06 07:31:29 -05:00
pwd_content += args_name + ':' + args_pwd + "\n"
2020-07-10 03:57:25 -04:00
mw.writeFile(pwd_path, pwd_content)
2019-03-06 07:31:29 -05:00
2019-03-06 05:47:43 -05:00
path = appConf()
2020-07-10 03:57:25 -04:00
content = mw.readFile(path)
2019-03-06 05:47:43 -05:00
con = "\n\n" + '[' + args_name + ']' + "\n"
con += 'path = ' + args_path + "\n"
con += 'comment = ' + args_ps + "\n"
2019-03-06 07:31:29 -05:00
con += 'auth users = ' + args_name + "\n"
2019-03-06 05:47:43 -05:00
con += 'read only = false'
content = content + con
2020-07-10 03:57:25 -04:00
mw.writeFile(path, content)
return mw.returnJson(True, '添加成功')
2019-03-06 05:47:43 -05:00
def delRec():
args = getArgs()
data = checkArgs(args, ['name'])
if not data[0]:
return data[1]
args_name = args['name']
2019-03-06 07:31:29 -05:00
cmd = "sed -i '_bak' '/" + args_name + "/d' " + appConfPwd()
2020-07-10 03:57:25 -04:00
mw.execShell(cmd)
2019-03-06 07:31:29 -05:00
try:
path = appConf()
2020-07-10 03:57:25 -04:00
content = mw.readFile(path)
2019-03-06 07:31:29 -05:00
ret_list = getRecListData()
ret_list_len = len(ret_list)
is_end = False
next_name = ''
for x in range(ret_list_len):
tmp = ret_list[x]
if tmp['name'] == args_name:
if x + 1 == ret_list_len:
is_end = True
else:
next_name = ret_list[x + 1]['name']
reg = ''
if is_end:
reg = '\[' + args_name + '\]\s*(.*)'
else:
reg = '\[' + args_name + '\]\s*(.*)\s*\[' + next_name + '\]'
2019-03-06 05:47:43 -05:00
2019-03-06 07:31:29 -05:00
conre = re.search(reg, content, re.S)
content = content.replace(
"[" + args_name + "]\n" + conre.groups()[0], '')
2020-07-10 03:57:25 -04:00
mw.writeFile(path, content)
return mw.returnJson(True, '删除成功!')
2019-03-06 07:31:29 -05:00
except Exception as e:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '删除失败!')
2019-03-06 05:47:43 -05:00
2019-03-06 07:31:29 -05:00
def cmdRec():
args = getArgs()
data = checkArgs(args, ['name'])
if not data[0]:
return data[1]
an = args['name']
pwd_list = getUPwdList()
2020-07-10 03:57:25 -04:00
ip = mw.getLocalIp()
2019-03-06 07:31:29 -05:00
cmd = 'echo "' + pwd_list[an] + '" > /tmp/p.pass' + "<br>"
2019-04-02 03:53:47 -04:00
cmd += 'chmod 600 /tmp/p.pass' + "<br>"
2019-03-06 07:31:29 -05:00
cmd += 'rsync -arv --password-file=/tmp/p.pass --progress --delete /project ' + \
an + '@' + ip + '::' + an
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'OK!', cmd)
2019-03-06 05:47:43 -05:00
2019-03-06 02:07:04 -05:00
# rsyncdReceive
2019-03-05 06:20:04 -05:00
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
2021-10-31 12:33:14 -04:00
print(status())
2019-03-05 06:20:04 -05:00
elif func == 'start':
2021-10-31 12:33:14 -04:00
print(start())
2019-03-05 06:20:04 -05:00
elif func == 'stop':
2021-10-31 12:33:14 -04:00
print(stop())
2019-03-05 06:20:04 -05:00
elif func == 'restart':
2021-10-31 12:33:14 -04:00
print(restart())
2019-03-05 06:20:04 -05:00
elif func == 'reload':
2021-10-31 12:33:14 -04:00
print(reload())
2019-03-05 06:20:04 -05:00
elif func == 'initd_status':
2021-10-31 12:33:14 -04:00
print(initdStatus())
2019-03-05 06:20:04 -05:00
elif func == 'initd_install':
2021-10-31 12:33:14 -04:00
print(initdInstall())
2019-03-05 06:20:04 -05:00
elif func == 'initd_uninstall':
2021-10-31 12:33:14 -04:00
print(initdUinstall())
2019-03-05 06:20:04 -05:00
elif func == 'conf':
2021-10-31 12:33:14 -04:00
print(appConf())
2019-03-06 07:31:29 -05:00
elif func == 'conf_pwd':
2021-10-31 12:33:14 -04:00
print(appConfPwd())
2019-03-06 02:07:04 -05:00
elif func == 'run_log':
2021-10-31 12:33:14 -04:00
print(getLog())
2019-03-06 05:47:43 -05:00
elif func == 'rec_list':
2021-10-31 12:33:14 -04:00
print(getRecList())
2019-03-06 05:47:43 -05:00
elif func == 'add_rec':
2021-10-31 12:33:14 -04:00
print(addRec())
2019-03-06 05:47:43 -05:00
elif func == 'del_rec':
2021-10-31 12:33:14 -04:00
print(delRec())
2019-03-06 07:31:29 -05:00
elif func == 'cmd_rec':
2021-10-31 12:33:14 -04:00
print(cmdRec())
2019-03-05 06:20:04 -05:00
else:
2021-10-31 12:33:14 -04:00
print('error')