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

377 lines
9.3 KiB

6 years ago
# coding: utf-8
import time
import random
import os
import json
import re
import sys
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 'rsyncd'
6 years ago
def getInitDTpl():
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
return path
6 years ago
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
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()
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'))
6 years ago
6 years ago
def contentReplace(content):
service_path = mw.getServerDir()
6 years ago
content = content.replace('{$SERVER_PATH}', service_path)
return content
6 years ago
def status():
data = mw.execShell(
6 years ago
"ps -ef|grep rsync |grep -v grep | grep -v python | awk '{print $2}'")
6 years ago
if data[0] == '':
return 'stop'
return 'start'
6 years ago
def appConf():
if mw.isAppleSystem():
6 years ago
return getServerDir() + '/rsyncd.conf'
return '/etc/rsyncd.conf'
6 years ago
def appConfPwd():
if mw.isAppleSystem():
6 years ago
return getServerDir() + '/rsyncd.passwd'
return '/etc/rsyncd.passwd'
6 years ago
def getLog():
conf_path = appConf()
conf = mw.readFile(conf_path)
6 years ago
rep = 'log file\s*=\s*(.*)'
tmp = re.search(rep, conf)
if not tmp:
return ''
return tmp.groups()[0]
6 years ago
def initDreplace():
6 years ago
conf_path = appConf()
conf = mw.readFile(conf_path)
6 years ago
compile_sub = re.compile('^#(.*)', re.M)
conf = compile_sub.sub('', conf)
6 years ago
conf_tpl_path = getPluginDir() + '/conf/rsyncd.conf'
if conf.strip() == '':
content = mw.readFile(conf_tpl_path)
mw.writeFile(conf_path, content)
6 years ago
confpwd_path = appConfPwd()
if not os.path.exists(confpwd_path):
mw.writeFile(confpwd_path, '')
mw.execShell('chmod 0600 ' + confpwd_path)
6 years ago
6 years ago
initD_path = getServerDir() + '/init.d'
if not os.path.exists(initD_path):
os.mkdir(initD_path)
file_bin = initD_path + '/' + getPluginName()
6 years ago
6 years ago
file_tpl = getInitDTpl()
# initd replace
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
3 years ago
# if os.path.exists('/usr/lib/systemd/system/rsyncd.service'):
# mw.execShell('rm -rf /usr/lib/systemd/system/rsyncd*')
6 years ago
6 years ago
rlog = getLog()
6 years ago
if os.path.exists(rlog):
mw.writeFile(rlog, '')
6 years ago
return file_bin
6 years ago
6 years ago
6 years ago
def start():
6 years ago
file = initDreplace()
data = mw.execShell(file + ' start')
6 years ago
if data[1] == '':
return 'ok'
return 'fail'
6 years ago
def stop():
6 years ago
file = initDreplace()
data = mw.execShell(file + ' stop')
6 years ago
if data[1] == '':
return 'ok'
return 'fail'
6 years ago
def restart():
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
6 years ago
stop()
start()
return 'ok'
6 years ago
def reload():
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
# data = mw.execShell('systemctl reload rsyncd.service')
6 years ago
# if data[1] == '':
# return 'ok'
# return 'fail'
stop()
start()
return 'ok'
6 years ago
def initdStatus():
6 years ago
if not app_debug:
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
6 years ago
6 years ago
initd_bin = getInitDFile()
if os.path.exists(initd_bin):
return 'ok'
return 'fail'
6 years ago
6 years ago
6 years ago
def initdInstall():
6 years ago
import shutil
if not app_debug:
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
6 years ago
p_bin = initDreplace()
6 years ago
initd_bin = getInitDFile()
6 years ago
shutil.copyfile(p_bin, initd_bin)
mw.execShell('chmod +x ' + initd_bin)
mw.execShell('chkconfig --add ' + getPluginName())
6 years ago
return 'ok'
6 years ago
def initdUinstall():
6 years ago
if not app_debug:
if mw.isAppleSystem():
6 years ago
return "Apple Computer does not support"
initd_bin = getInitDFile()
os.remove(initd_bin)
mw.execShell('chkconfig --del ' + getPluginName())
6 years ago
return 'ok'
6 years ago
def getRecListData():
path = appConf()
content = mw.readFile(path)
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()
return mw.returnJson(True, 'ok', ret_list)
6 years ago
def getUPwdList():
pwd_path = appConfPwd()
pwd_content = mw.readFile(pwd_path)
6 years ago
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
def addRec():
args = getArgs()
6 years ago
data = checkArgs(args, ['name', 'path', 'pwd', 'ps'])
if not data[0]:
return data[1]
args_name = args['name']
6 years ago
args_pwd = args['pwd']
args_path = args['path']
args_ps = args['ps']
6 years ago
pwd_path = appConfPwd()
pwd_content = mw.readFile(pwd_path)
6 years ago
pwd_content += args_name + ':' + args_pwd + "\n"
mw.writeFile(pwd_path, pwd_content)
6 years ago
path = appConf()
content = mw.readFile(path)
con = "\n\n" + '[' + args_name + ']' + "\n"
con += 'path = ' + args_path + "\n"
con += 'comment = ' + args_ps + "\n"
6 years ago
con += 'auth users = ' + args_name + "\n"
con += 'read only = false'
content = content + con
mw.writeFile(path, content)
return mw.returnJson(True, '添加成功')
def delRec():
args = getArgs()
data = checkArgs(args, ['name'])
if not data[0]:
return data[1]
args_name = args['name']
6 years ago
cmd = "sed -i '_bak' '/" + args_name + "/d' " + appConfPwd()
mw.execShell(cmd)
6 years ago
try:
path = appConf()
content = mw.readFile(path)
6 years ago
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 + '\]'
6 years ago
conre = re.search(reg, content, re.S)
content = content.replace(
"[" + args_name + "]\n" + conre.groups()[0], '')
mw.writeFile(path, content)
return mw.returnJson(True, '删除成功!')
6 years ago
except Exception as e:
return mw.returnJson(False, '删除失败!')
6 years ago
def cmdRec():
args = getArgs()
data = checkArgs(args, ['name'])
if not data[0]:
return data[1]
an = args['name']
pwd_list = getUPwdList()
ip = mw.getLocalIp()
6 years ago
cmd = 'echo "' + pwd_list[an] + '" > /tmp/p.pass' + "<br>"
6 years ago
cmd += 'chmod 600 /tmp/p.pass' + "<br>"
6 years ago
cmd += 'rsync -arv --password-file=/tmp/p.pass --progress --delete /project ' + \
an + '@' + ip + '::' + an
return mw.returnJson(True, 'OK!', cmd)
6 years ago
# rsyncdReceive
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 == '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(appConf())
6 years ago
elif func == 'conf_pwd':
4 years ago
print(appConfPwd())
6 years ago
elif func == 'run_log':
4 years ago
print(getLog())
elif func == 'rec_list':
4 years ago
print(getRecList())
elif func == 'add_rec':
4 years ago
print(addRec())
elif func == 'del_rec':
4 years ago
print(delRec())
6 years ago
elif func == 'cmd_rec':
4 years ago
print(cmdRec())
6 years ago
else:
4 years ago
print('error')