|
|
|
@ -73,18 +73,24 @@ def status(): |
|
|
|
|
"ps -ef|grep rsync |grep -v grep | grep -v python | awk '{print $2}'") |
|
|
|
|
if data[0] == '': |
|
|
|
|
return 'stop' |
|
|
|
|
|
|
|
|
|
# data = mw.execShell( |
|
|
|
|
# "ps -ef|grep lsyncd |grep -v grep | grep -v python | awk '{print $2}'") |
|
|
|
|
# if data[0] == '': |
|
|
|
|
# return 'stop' |
|
|
|
|
|
|
|
|
|
return 'start' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def appConf(): |
|
|
|
|
return getServerDir() + '/rsyncd.conf' |
|
|
|
|
# return '/etc/rsyncd.conf' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def appConfPwd(): |
|
|
|
|
# if mw.isAppleSystem(): |
|
|
|
|
return getServerDir() + '/rsyncd.passwd' |
|
|
|
|
# return '/etc/rsyncd.passwd' |
|
|
|
|
def appAuthPwd(name): |
|
|
|
|
nameDir = getServerDir() + '/receive/' + name |
|
|
|
|
if not os.path.exists(nameDir): |
|
|
|
|
mw.execShell("mkdir -p " + nameDir) |
|
|
|
|
return nameDir + '/auth.db' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getLog(): |
|
|
|
@ -97,8 +103,17 @@ def getLog(): |
|
|
|
|
return tmp.groups()[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initDreplace(): |
|
|
|
|
def getLsyncdLog(): |
|
|
|
|
path = getServerDir() + "/lsyncd.conf" |
|
|
|
|
conf = mw.readFile(path) |
|
|
|
|
rep = 'logfile\s*=\s*\"(.*)\"' |
|
|
|
|
tmp = re.search(rep, conf) |
|
|
|
|
if not tmp: |
|
|
|
|
return '' |
|
|
|
|
return tmp.groups()[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initDReceive(): |
|
|
|
|
# conf |
|
|
|
|
conf_path = appConf() |
|
|
|
|
conf_tpl_path = getPluginDir() + '/conf/rsyncd.conf' |
|
|
|
@ -106,18 +121,13 @@ def initDreplace(): |
|
|
|
|
content = mw.readFile(conf_tpl_path) |
|
|
|
|
mw.writeFile(conf_path, content) |
|
|
|
|
|
|
|
|
|
# pwd |
|
|
|
|
confpwd_path = appConfPwd() |
|
|
|
|
if not os.path.exists(confpwd_path): |
|
|
|
|
mw.writeFile(confpwd_path, '') |
|
|
|
|
mw.execShell('chmod 0600 ' + confpwd_path) |
|
|
|
|
|
|
|
|
|
initD_path = getServerDir() + '/init.d' |
|
|
|
|
if not os.path.exists(initD_path): |
|
|
|
|
os.mkdir(initD_path) |
|
|
|
|
file_bin = initD_path + '/' + getPluginName() |
|
|
|
|
|
|
|
|
|
file_bin = initD_path + '/' + getPluginName() |
|
|
|
|
file_tpl = getInitDTpl() |
|
|
|
|
# print(file_bin, file_tpl) |
|
|
|
|
# initd replace |
|
|
|
|
if not os.path.exists(file_bin): |
|
|
|
|
content = mw.readFile(file_tpl) |
|
|
|
@ -132,14 +142,14 @@ def initDreplace(): |
|
|
|
|
if os.path.exists(systemDir) and not os.path.exists(systemService): |
|
|
|
|
rsync_bin = mw.execShell('which rsync')[0].strip() |
|
|
|
|
if rsync_bin == '': |
|
|
|
|
print('rsync缺失!') |
|
|
|
|
print('rsync missing!') |
|
|
|
|
exit(0) |
|
|
|
|
|
|
|
|
|
service_path = mw.getServerDir() |
|
|
|
|
se_content = mw.readFile(systemServiceTpl) |
|
|
|
|
se_content = se_content.replace('{$SERVER_PATH}', service_path) |
|
|
|
|
se_content = se_content.replace('{$RSYNC_BIN}', rsync_bin) |
|
|
|
|
mw.writeFile(systemService, se_content) |
|
|
|
|
se = mw.readFile(systemServiceTpl) |
|
|
|
|
se = se.replace('{$SERVER_PATH}', service_path) |
|
|
|
|
se = se.replace('{$RSYNC_BIN}', rsync_bin) |
|
|
|
|
mw.writeFile(systemService, se) |
|
|
|
|
mw.execShell('systemctl daemon-reload') |
|
|
|
|
|
|
|
|
|
rlog = getLog() |
|
|
|
@ -148,6 +158,89 @@ def initDreplace(): |
|
|
|
|
return file_bin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initDSend(): |
|
|
|
|
|
|
|
|
|
service_path = mw.getServerDir() |
|
|
|
|
|
|
|
|
|
conf_path = getServerDir() + '/lsyncd.conf' |
|
|
|
|
conf_tpl_path = getPluginDir() + '/conf/lsyncd.conf' |
|
|
|
|
if not os.path.exists(conf_path): |
|
|
|
|
content = mw.readFile(conf_tpl_path) |
|
|
|
|
content = content.replace('{$SERVER_PATH}', service_path) |
|
|
|
|
mw.writeFile(conf_path, content) |
|
|
|
|
|
|
|
|
|
initD_path = getServerDir() + '/init.d' |
|
|
|
|
if not os.path.exists(initD_path): |
|
|
|
|
os.mkdir(initD_path) |
|
|
|
|
|
|
|
|
|
# initd replace |
|
|
|
|
file_bin = initD_path + '/lsyncd' |
|
|
|
|
file_tpl = getPluginDir() + "/init.d/lsyncd.tpl" |
|
|
|
|
if not os.path.exists(file_bin): |
|
|
|
|
content = mw.readFile(file_tpl) |
|
|
|
|
content = contentReplace(content) |
|
|
|
|
mw.writeFile(file_bin, content) |
|
|
|
|
mw.execShell('chmod +x ' + file_bin) |
|
|
|
|
|
|
|
|
|
lock_file = getServerDir() + "/installed.pl" |
|
|
|
|
# systemd |
|
|
|
|
systemDir = mw.systemdCfgDir() |
|
|
|
|
systemService = systemDir + '/lsyncd.service' |
|
|
|
|
systemServiceTpl = getPluginDir() + '/init.d/lsyncd.service.tpl' |
|
|
|
|
if not os.path.exists(lock_file): |
|
|
|
|
lsyncd_bin = mw.execShell('which lsyncd')[0].strip() |
|
|
|
|
if lsyncd_bin == '': |
|
|
|
|
print('lsyncd missing!') |
|
|
|
|
exit(0) |
|
|
|
|
|
|
|
|
|
content = mw.readFile(systemServiceTpl) |
|
|
|
|
content = content.replace('{$SERVER_PATH}', service_path) |
|
|
|
|
content = content.replace('{$LSYNCD_BIN}', lsyncd_bin) |
|
|
|
|
mw.writeFile(systemService, content) |
|
|
|
|
mw.execShell('systemctl daemon-reload') |
|
|
|
|
|
|
|
|
|
mw.writeFile(lock_file, "ok") |
|
|
|
|
|
|
|
|
|
lslog = getLsyncdLog() |
|
|
|
|
if os.path.exists(lslog): |
|
|
|
|
mw.writeFile(lslog, '') |
|
|
|
|
|
|
|
|
|
return file_bin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getDefaultConf(): |
|
|
|
|
path = getServerDir() + "/config.json" |
|
|
|
|
data = mw.readFile(path) |
|
|
|
|
data = json.loads(data) |
|
|
|
|
return data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setDefaultConf(data): |
|
|
|
|
path = getServerDir() + "/config.json" |
|
|
|
|
mw.writeFile(path, json.dumps(data)) |
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initConfigJson(): |
|
|
|
|
path = getServerDir() + "/config.json" |
|
|
|
|
tpl = getPluginDir() + "/conf/config.json" |
|
|
|
|
if not os.path.exists(path): |
|
|
|
|
data = mw.readFile(tpl) |
|
|
|
|
data = json.loads(data) |
|
|
|
|
mw.writeFile(path, json.dumps(data)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initDreplace(): |
|
|
|
|
|
|
|
|
|
initDSend() |
|
|
|
|
|
|
|
|
|
# conf |
|
|
|
|
file_bin = initDReceive() |
|
|
|
|
initConfigJson() |
|
|
|
|
|
|
|
|
|
return file_bin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rsyncOp(method): |
|
|
|
|
file = initDreplace() |
|
|
|
|
if not mw.isAppleSystem(): |
|
|
|
@ -186,6 +279,12 @@ def initdStatus(): |
|
|
|
|
data = mw.execShell(shell_cmd) |
|
|
|
|
if data[0] == '': |
|
|
|
|
return 'fail' |
|
|
|
|
|
|
|
|
|
shell_cmd = 'systemctl status lsyncd | grep loaded | grep "enabled;"' |
|
|
|
|
data = mw.execShell(shell_cmd) |
|
|
|
|
if data[0] == '': |
|
|
|
|
return 'fail' |
|
|
|
|
|
|
|
|
|
return 'ok' |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -193,6 +292,7 @@ def initdInstall(): |
|
|
|
|
if mw.isAppleSystem(): |
|
|
|
|
return "Apple Computer does not support" |
|
|
|
|
|
|
|
|
|
mw.execShell('systemctl enable lsyncd') |
|
|
|
|
mw.execShell('systemctl enable rsyncd') |
|
|
|
|
return 'ok' |
|
|
|
|
|
|
|
|
@ -202,6 +302,7 @@ def initdUinstall(): |
|
|
|
|
if mw.isAppleSystem(): |
|
|
|
|
return "Apple Computer does not support" |
|
|
|
|
|
|
|
|
|
mw.execShell('systemctl diable lsyncd') |
|
|
|
|
mw.execShell('systemctl diable rsyncd') |
|
|
|
|
return 'ok' |
|
|
|
|
|
|
|
|
@ -211,6 +312,7 @@ def getRecListData(): |
|
|
|
|
content = mw.readFile(path) |
|
|
|
|
|
|
|
|
|
flist = re.findall("\[(.*)\]", content) |
|
|
|
|
|
|
|
|
|
flist_len = len(flist) |
|
|
|
|
ret_list = [] |
|
|
|
|
for i in range(flist_len): |
|
|
|
@ -226,31 +328,27 @@ def getRecListData(): |
|
|
|
|
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) |
|
|
|
|
# print('args start', args, 'args_end') |
|
|
|
|
t2 = re.findall('\s*(.*)\s*\=\s*?(.*)?', args, re.M | re.I) |
|
|
|
|
for i in range(len(t2)): |
|
|
|
|
tmp[t2[i][0].strip()] = t2[i][1] |
|
|
|
|
tmp[t2[i][0].strip()] = t2[i][1].strip() |
|
|
|
|
ret_list.append(tmp) |
|
|
|
|
|
|
|
|
|
return ret_list |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getRecListDataBy(name): |
|
|
|
|
l = getRecListData() |
|
|
|
|
for x in range(len(l)): |
|
|
|
|
if name == l[x]["name"]: |
|
|
|
|
return l[x] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getRecList(): |
|
|
|
|
ret_list = getRecListData() |
|
|
|
|
return mw.returnJson(True, 'ok', ret_list) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getUPwdList(): |
|
|
|
|
pwd_path = appConfPwd() |
|
|
|
|
pwd_content = mw.readFile(pwd_path) |
|
|
|
|
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() |
|
|
|
|
data = checkArgs(args, ['name', 'path', 'pwd', 'ps']) |
|
|
|
@ -262,10 +360,12 @@ def addRec(): |
|
|
|
|
args_path = args['path'] |
|
|
|
|
args_ps = args['ps'] |
|
|
|
|
|
|
|
|
|
pwd_path = appConfPwd() |
|
|
|
|
pwd_content = mw.readFile(pwd_path) |
|
|
|
|
pwd_content += args_name + ':' + args_pwd + "\n" |
|
|
|
|
mw.writeFile(pwd_path, pwd_content) |
|
|
|
|
delRecBy(args_name) |
|
|
|
|
|
|
|
|
|
auth_path = appAuthPwd(args_name) |
|
|
|
|
pwd_content = args_name + ':' + args_pwd + "\n" |
|
|
|
|
mw.writeFile(auth_path, pwd_content) |
|
|
|
|
mw.execShell("chmod 600 " + auth_path) |
|
|
|
|
|
|
|
|
|
path = appConf() |
|
|
|
|
content = mw.readFile(path) |
|
|
|
@ -274,71 +374,498 @@ def addRec(): |
|
|
|
|
con += 'path = ' + args_path + "\n" |
|
|
|
|
con += 'comment = ' + args_ps + "\n" |
|
|
|
|
con += 'auth users = ' + args_name + "\n" |
|
|
|
|
con += 'ignore errors' + "\n" |
|
|
|
|
con += 'secrets file = ' + auth_path + "\n" |
|
|
|
|
con += 'read only = false' |
|
|
|
|
|
|
|
|
|
content = content + con |
|
|
|
|
content = content.strip() + "\n" + con |
|
|
|
|
mw.writeFile(path, content) |
|
|
|
|
return mw.returnJson(True, '添加成功') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delRec(): |
|
|
|
|
def getRec(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
args_name = args['name'] |
|
|
|
|
|
|
|
|
|
cmd = "sed -i '_bak' '/" + args_name + "/d' " + appConfPwd() |
|
|
|
|
mw.execShell(cmd) |
|
|
|
|
name = args['name'] |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
if name == "": |
|
|
|
|
tmp = {} |
|
|
|
|
tmp["name"] = "" |
|
|
|
|
tmp["comment"] = "" |
|
|
|
|
tmp["path"] = mw.getWwwDir() |
|
|
|
|
tmp["pwd"] = mw.getRandomString(16) |
|
|
|
|
return mw.returnJson(True, 'OK', tmp) |
|
|
|
|
|
|
|
|
|
data = getRecListDataBy(name) |
|
|
|
|
|
|
|
|
|
content = mw.readFile(data['secrets file']) |
|
|
|
|
pwd = content.strip().split(":") |
|
|
|
|
data['pwd'] = pwd[1] |
|
|
|
|
return mw.returnJson(True, 'OK', data) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delRecBy(name): |
|
|
|
|
try: |
|
|
|
|
path = appConf() |
|
|
|
|
content = mw.readFile(path) |
|
|
|
|
|
|
|
|
|
ret_list = getRecListData() |
|
|
|
|
ret_list_len = len(ret_list) |
|
|
|
|
reclist = getRecListData() |
|
|
|
|
ret_list_len = len(reclist) |
|
|
|
|
is_end = False |
|
|
|
|
next_name = '' |
|
|
|
|
for x in range(ret_list_len): |
|
|
|
|
tmp = ret_list[x] |
|
|
|
|
if tmp['name'] == args_name: |
|
|
|
|
tmp = reclist[x] |
|
|
|
|
if tmp['name'] == name: |
|
|
|
|
|
|
|
|
|
secrets_file = tmp['secrets file'] |
|
|
|
|
tp = os.path.dirname(secrets_file) |
|
|
|
|
if os.path.exists(tp): |
|
|
|
|
mw.execShell("rm -rf " + tp) |
|
|
|
|
|
|
|
|
|
if x + 1 == ret_list_len: |
|
|
|
|
is_end = True |
|
|
|
|
else: |
|
|
|
|
next_name = ret_list[x + 1]['name'] |
|
|
|
|
next_name = reclist[x + 1]['name'] |
|
|
|
|
reg = '' |
|
|
|
|
if is_end: |
|
|
|
|
reg = '\[' + args_name + '\]\s*(.*)' |
|
|
|
|
reg = '\[' + name + '\]\s*(.*)' |
|
|
|
|
else: |
|
|
|
|
reg = '\[' + args_name + '\]\s*(.*)\s*\[' + next_name + '\]' |
|
|
|
|
reg = '\[' + name + '\]\s*(.*)\s*\[' + next_name + '\]' |
|
|
|
|
|
|
|
|
|
conre = re.search(reg, content, re.S) |
|
|
|
|
content = content.replace( |
|
|
|
|
"[" + args_name + "]\n" + conre.groups()[0], '') |
|
|
|
|
"[" + name + "]\n" + conre.groups()[0], '') |
|
|
|
|
mw.writeFile(path, content) |
|
|
|
|
return mw.returnJson(True, '删除成功!') |
|
|
|
|
except Exception as e: |
|
|
|
|
return mw.returnJson(False, '删除失败!') |
|
|
|
|
return False |
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cmdRec(): |
|
|
|
|
def delRec(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
name = args['name'] |
|
|
|
|
ok = delRecBy(name) |
|
|
|
|
if ok: |
|
|
|
|
return mw.returnJson(True, '删除成功!') |
|
|
|
|
return mw.returnJson(False, '删除失败!') |
|
|
|
|
|
|
|
|
|
an = args['name'] |
|
|
|
|
pwd_list = getUPwdList() |
|
|
|
|
|
|
|
|
|
def cmdRecSecretKey(): |
|
|
|
|
import base64 |
|
|
|
|
|
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
name = args['name'] |
|
|
|
|
info = getRecListDataBy(name) |
|
|
|
|
|
|
|
|
|
secrets_file = info['secrets file'] |
|
|
|
|
content = mw.readFile(info['secrets file']) |
|
|
|
|
pwd = content.strip().split(":") |
|
|
|
|
|
|
|
|
|
m = {"A": info['name'], "B": pwd[1], "C": "873"} |
|
|
|
|
m = json.dumps(m) |
|
|
|
|
m = m.encode("utf-8") |
|
|
|
|
m = base64.b64encode(m) |
|
|
|
|
cmd = m.decode("utf-8") |
|
|
|
|
return mw.returnJson(True, 'OK!', cmd) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cmdRecCmd(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
name = args['name'] |
|
|
|
|
info = getRecListDataBy(name) |
|
|
|
|
ip = mw.getLocalIp() |
|
|
|
|
|
|
|
|
|
cmd = 'echo "' + pwd_list[an] + '" > /tmp/p.pass' + "<br>" |
|
|
|
|
cmd += 'chmod 600 /tmp/p.pass' + "<br>" |
|
|
|
|
cmd += 'rsync -arv --password-file=/tmp/p.pass --progress --delete /project ' + \ |
|
|
|
|
an + '@' + ip + '::' + an |
|
|
|
|
content = mw.readFile(info['secrets file']) |
|
|
|
|
pwd = content.strip().split(":") |
|
|
|
|
|
|
|
|
|
tmp_name = '/tmp/' + name + '.pass' |
|
|
|
|
|
|
|
|
|
cmd = 'echo "' + pwd[1] + '" > ' + tmp_name + '<br>' |
|
|
|
|
cmd += 'chmod 600 ' + tmp_name + ' <br>' |
|
|
|
|
cmd += 'rsync -arv --password-file=' + tmp_name + \ |
|
|
|
|
' --progress --delete /project ' + name + '@' + ip + '::' + name |
|
|
|
|
return mw.returnJson(True, 'OK!', cmd) |
|
|
|
|
|
|
|
|
|
# rsyncdReceive |
|
|
|
|
|
|
|
|
|
# ----------------------------- rsyncdSend start ------------------------- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdReload(): |
|
|
|
|
mw.execShell('systemctl reload lsyncd') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def makeLsyncdConf(data): |
|
|
|
|
# print(data) |
|
|
|
|
|
|
|
|
|
lsyncd_data = data['send'] |
|
|
|
|
lsyncd_setting = lsyncd_data['default'] |
|
|
|
|
|
|
|
|
|
content = "settings {\n" |
|
|
|
|
for x in lsyncd_setting: |
|
|
|
|
v = lsyncd_setting[x] |
|
|
|
|
# print(v, type(v)) |
|
|
|
|
if type(v) == str: |
|
|
|
|
content += "\t" + x + ' = "' + v + "\",\n" |
|
|
|
|
elif type(v) == int: |
|
|
|
|
content += "\t" + x + ' = ' + str(v) + ",\n" |
|
|
|
|
content += "}\n\n" |
|
|
|
|
|
|
|
|
|
lsyncd_list = lsyncd_data['list'] |
|
|
|
|
|
|
|
|
|
rsync_bin = mw.execShell('which rsync')[0].strip() |
|
|
|
|
send_dir = getServerDir() + "/send" |
|
|
|
|
|
|
|
|
|
if len(lsyncd_list) > 0: |
|
|
|
|
for x in range(len(lsyncd_list)): |
|
|
|
|
|
|
|
|
|
t = lsyncd_list[x] |
|
|
|
|
name_dir = send_dir + "/" + t["name"] |
|
|
|
|
if not os.path.exists(name_dir): |
|
|
|
|
mw.execShell("mkdir -p " + name_dir) |
|
|
|
|
|
|
|
|
|
cmd_exclude = name_dir + "/exclude" |
|
|
|
|
cmd_exclude_txt = "" |
|
|
|
|
for x in t['exclude']: |
|
|
|
|
cmd_exclude_txt += x + "\n" |
|
|
|
|
mw.writeFile(cmd_exclude, cmd_exclude_txt) |
|
|
|
|
cmd_pass = name_dir + "/pass" |
|
|
|
|
mw.writeFile(cmd_pass, t['password']) |
|
|
|
|
mw.execShell("chmod 600 " + cmd_pass) |
|
|
|
|
|
|
|
|
|
delete_ok = ' ' |
|
|
|
|
if t['delete'] == "true": |
|
|
|
|
delete_ok = ' --delete ' |
|
|
|
|
|
|
|
|
|
remote_addr = t['name'] + '@' + t['ip'] + "::" + t['name'] |
|
|
|
|
cmd = rsync_bin + " -avzP " + "--port=" + str(t['rsync']['port']) + " --bwlimit=" + t['rsync'][ |
|
|
|
|
'bwlimit'] + delete_ok + " --exclude-from=" + cmd_exclude + " --password-file=" + cmd_pass + " " + t["path"] + " " + remote_addr |
|
|
|
|
mw.writeFile(name_dir + "/cmd", cmd) |
|
|
|
|
mw.execShell("cmod +x " + name_dir + "/cmd") |
|
|
|
|
|
|
|
|
|
if t['realtime'] == "false": |
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
# print(x, t) |
|
|
|
|
content += "sync {\n" |
|
|
|
|
content += "\tdefault.rsync,\n" |
|
|
|
|
content += "\tsource = \"" + t['path'] + "\",\n" |
|
|
|
|
content += "\ttarget = \"" + remote_addr + "\",\n" |
|
|
|
|
content += "\tdelete = " + t['delete'] + ",\n" |
|
|
|
|
content += "\tdelay = " + t['delay'] + ",\n" |
|
|
|
|
content += "\tinit = false,\n" |
|
|
|
|
|
|
|
|
|
exclude_str = json.dumps(t['exclude']) |
|
|
|
|
exclude_str = exclude_str.replace("[", "{") |
|
|
|
|
exclude_str = exclude_str.replace("]", "}") |
|
|
|
|
# print(exclude_str) |
|
|
|
|
content += "\texclude = " + exclude_str + ",\n" |
|
|
|
|
|
|
|
|
|
# rsync |
|
|
|
|
content += "\trsync = {\n" |
|
|
|
|
content += "\t\tbinary = \"" + rsync_bin + "\",\n" |
|
|
|
|
content += "\t\tarchive = true,\n" |
|
|
|
|
content += "\t\tverbose = true,\n" |
|
|
|
|
content += "\t\tcompress = " + t['rsync']['compress'] + ",\n" |
|
|
|
|
content += "\t\tpassword_file = \"" + cmd_pass + "\",\n" |
|
|
|
|
|
|
|
|
|
content += "\t\t_extra = {\"--bwlimit=" + t['rsync'][ |
|
|
|
|
'bwlimit'] + "\", \"--port=" + str(t['rsync']['port']) + "\"},\n" |
|
|
|
|
|
|
|
|
|
content += "\t}\n" |
|
|
|
|
content += "}\n" |
|
|
|
|
|
|
|
|
|
path = getServerDir() + "/lsyncd.conf" |
|
|
|
|
mw.writeFile(path, content) |
|
|
|
|
|
|
|
|
|
lsyncdReload() |
|
|
|
|
|
|
|
|
|
import tool_task |
|
|
|
|
tool_task.createBgTask(lsyncd_list) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdListFindIp(slist, ip): |
|
|
|
|
for x in range(len(slist)): |
|
|
|
|
if slist[x]["ip"] == ip: |
|
|
|
|
return (True, x) |
|
|
|
|
return (False, -1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdListFindName(slist, name): |
|
|
|
|
for x in range(len(slist)): |
|
|
|
|
if slist[x]["name"] == name: |
|
|
|
|
return (True, x) |
|
|
|
|
return (False, -1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdList(): |
|
|
|
|
data = getDefaultConf() |
|
|
|
|
send = data['send'] |
|
|
|
|
return mw.returnJson(True, "设置成功!", send) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdGet(): |
|
|
|
|
import base64 |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
name = args['name'] |
|
|
|
|
data = getDefaultConf() |
|
|
|
|
|
|
|
|
|
slist = data['send']["list"] |
|
|
|
|
res = lsyncdListFindName(slist, name) |
|
|
|
|
|
|
|
|
|
rsync = { |
|
|
|
|
'bwlimit': "1024", |
|
|
|
|
"compress": "true", |
|
|
|
|
"archive": "true", |
|
|
|
|
"verbose": "true" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
info = { |
|
|
|
|
"secret_key": '', |
|
|
|
|
"ip": '', |
|
|
|
|
"path": mw.getServerDir(), |
|
|
|
|
'rsync': rsync, |
|
|
|
|
'realtime': "true", |
|
|
|
|
'delete': "false", |
|
|
|
|
} |
|
|
|
|
if res[0]: |
|
|
|
|
list_index = res[1] |
|
|
|
|
info = slist[list_index] |
|
|
|
|
m = {"A": info['name'], "B": info["password"], "C": "873"} |
|
|
|
|
m = json.dumps(m) |
|
|
|
|
m = m.encode("utf-8") |
|
|
|
|
m = base64.b64encode(m) |
|
|
|
|
info['secret_key'] = m.decode("utf-8") |
|
|
|
|
return mw.returnJson(True, "OK", info) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdDelete(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
name = args['name'] |
|
|
|
|
data = getDefaultConf() |
|
|
|
|
slist = data['send']["list"] |
|
|
|
|
res = lsyncdListFindName(slist, name) |
|
|
|
|
retdata = {} |
|
|
|
|
if res[0]: |
|
|
|
|
list_index = res[1] |
|
|
|
|
slist.pop(list_index) |
|
|
|
|
|
|
|
|
|
data['send']["list"] = slist |
|
|
|
|
setDefaultConf(data) |
|
|
|
|
makeLsyncdConf(data) |
|
|
|
|
return mw.returnJson(True, "OK") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdAdd(): |
|
|
|
|
import base64 |
|
|
|
|
|
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['ip', 'conn_type', 'path', |
|
|
|
|
'secret_key', 'delay', 'period']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
ip = args['ip'] |
|
|
|
|
path = args['path'] |
|
|
|
|
|
|
|
|
|
conn_type = args['conn_type'] |
|
|
|
|
secret_key = args['secret_key'] |
|
|
|
|
delete = args['delete'] |
|
|
|
|
realtime = args['realtime'] |
|
|
|
|
delay = args['delay'] |
|
|
|
|
bwlimit = args['bwlimit'] |
|
|
|
|
compress = args['compress'] |
|
|
|
|
period = args['period'] |
|
|
|
|
|
|
|
|
|
hour = args['hour'] |
|
|
|
|
minute = args['minute'] |
|
|
|
|
minute_n = args['minute-n'] |
|
|
|
|
|
|
|
|
|
info = { |
|
|
|
|
"ip": ip, |
|
|
|
|
"path": path, |
|
|
|
|
"delete": delete, |
|
|
|
|
"realtime": realtime, |
|
|
|
|
'delay': delay, |
|
|
|
|
"conn_type": conn_type, |
|
|
|
|
"period": period, |
|
|
|
|
"hour": hour, |
|
|
|
|
"minute": minute, |
|
|
|
|
"minute-n": minute_n, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if conn_type == "key": |
|
|
|
|
try: |
|
|
|
|
m = base64.b64decode(secret_key) |
|
|
|
|
m = json.loads(m) |
|
|
|
|
info['name'] = m['A'] |
|
|
|
|
info['password'] = m['B'] |
|
|
|
|
info['port'] = m['C'] |
|
|
|
|
except Exception as e: |
|
|
|
|
return mw.returnJson(False, "接收密钥格式错误!") |
|
|
|
|
else: |
|
|
|
|
data = checkArgs(args, ['uname']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
info['name'] = args['uname'] |
|
|
|
|
info['password'] = args['uname'] |
|
|
|
|
|
|
|
|
|
rsync = { |
|
|
|
|
'bwlimit': bwlimit, |
|
|
|
|
"port": info['port'], |
|
|
|
|
"compress": compress, |
|
|
|
|
"archive": "true", |
|
|
|
|
"verbose": "true" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
info['rsync'] = rsync |
|
|
|
|
|
|
|
|
|
if not 'exclude' in info: |
|
|
|
|
info["exclude"] = [ |
|
|
|
|
"/**.upload.tmp", |
|
|
|
|
"**/*.log", |
|
|
|
|
"**/*.tmp", |
|
|
|
|
"**/*.temp", |
|
|
|
|
".git", |
|
|
|
|
".gitignore", |
|
|
|
|
".user.ini", |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
data = getDefaultConf() |
|
|
|
|
slist = data['send']["list"] |
|
|
|
|
res = lsyncdListFindName(slist, info['name']) |
|
|
|
|
if res[0]: |
|
|
|
|
list_index = res[1] |
|
|
|
|
slist[list_index] = info |
|
|
|
|
else: |
|
|
|
|
slist.append(info) |
|
|
|
|
|
|
|
|
|
data['send']["list"] = slist |
|
|
|
|
|
|
|
|
|
setDefaultConf(data) |
|
|
|
|
makeLsyncdConf(data) |
|
|
|
|
return mw.returnJson(True, "设置成功!") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdRun(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
send_dir = getServerDir() + "/send" |
|
|
|
|
name = args['name'] |
|
|
|
|
app_dir = send_dir + "/" + name |
|
|
|
|
|
|
|
|
|
cmd = "bash " + app_dir + "/cmd >> " + app_dir + "/run.log" + " 2>&1 &" |
|
|
|
|
mw.execShell(cmd) |
|
|
|
|
return mw.returnJson(True, "执行成功!") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdConfLog(): |
|
|
|
|
logs_path = getServerDir() + "/lsyncd.log" |
|
|
|
|
return logs_path |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdLog(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
send_dir = getServerDir() + "/send" |
|
|
|
|
name = args['name'] |
|
|
|
|
app_dir = send_dir + "/" + name |
|
|
|
|
return app_dir + "/run.log" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdGetExclude(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
data = getDefaultConf() |
|
|
|
|
slist = data['send']["list"] |
|
|
|
|
res = lsyncdListFindName(slist, args['name']) |
|
|
|
|
i = res[1] |
|
|
|
|
info = slist[i] |
|
|
|
|
return mw.returnJson(True, "OK!", info['exclude']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdRemoveExclude(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name', 'exclude']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
exclude = args['exclude'] |
|
|
|
|
|
|
|
|
|
data = getDefaultConf() |
|
|
|
|
slist = data['send']["list"] |
|
|
|
|
res = lsyncdListFindName(slist, args['name']) |
|
|
|
|
i = res[1] |
|
|
|
|
info = slist[i] |
|
|
|
|
|
|
|
|
|
exclude_list = info['exclude'] |
|
|
|
|
exclude_pop_key = -1 |
|
|
|
|
for x in range(len(exclude_list)): |
|
|
|
|
if exclude_list[x] == exclude: |
|
|
|
|
exclude_pop_key = x |
|
|
|
|
|
|
|
|
|
if exclude_pop_key > -1: |
|
|
|
|
exclude_list.pop(exclude_pop_key) |
|
|
|
|
|
|
|
|
|
data['send']["list"][i]['exclude'] = exclude_list |
|
|
|
|
setDefaultConf(data) |
|
|
|
|
makeLsyncdConf(data) |
|
|
|
|
return mw.returnJson(True, "OK!", exclude_list) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lsyncdAddExclude(): |
|
|
|
|
args = getArgs() |
|
|
|
|
data = checkArgs(args, ['name', 'exclude']) |
|
|
|
|
if not data[0]: |
|
|
|
|
return data[1] |
|
|
|
|
|
|
|
|
|
exclude = args['exclude'] |
|
|
|
|
|
|
|
|
|
data = getDefaultConf() |
|
|
|
|
slist = data['send']["list"] |
|
|
|
|
res = lsyncdListFindName(slist, args['name']) |
|
|
|
|
i = res[1] |
|
|
|
|
info = slist[i] |
|
|
|
|
|
|
|
|
|
exclude_list = info['exclude'] |
|
|
|
|
exclude_list.append(exclude) |
|
|
|
|
|
|
|
|
|
data['send']["list"][i]['exclude'] = exclude_list |
|
|
|
|
setDefaultConf(data) |
|
|
|
|
makeLsyncdConf(data) |
|
|
|
|
return mw.returnJson(True, "OK!", exclude_list) |
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
func = sys.argv[1] |
|
|
|
|
if func == 'status': |
|
|
|
@ -359,8 +886,6 @@ if __name__ == "__main__": |
|
|
|
|
print(initdUinstall()) |
|
|
|
|
elif func == 'conf': |
|
|
|
|
print(appConf()) |
|
|
|
|
elif func == 'conf_pwd': |
|
|
|
|
print(appConfPwd()) |
|
|
|
|
elif func == 'run_log': |
|
|
|
|
print(getLog()) |
|
|
|
|
elif func == 'rec_list': |
|
|
|
@ -369,7 +894,31 @@ if __name__ == "__main__": |
|
|
|
|
print(addRec()) |
|
|
|
|
elif func == 'del_rec': |
|
|
|
|
print(delRec()) |
|
|
|
|
elif func == 'cmd_rec': |
|
|
|
|
print(cmdRec()) |
|
|
|
|
elif func == 'get_rec': |
|
|
|
|
print(getRec()) |
|
|
|
|
elif func == 'cmd_rec_secret_key': |
|
|
|
|
print(cmdRecSecretKey()) |
|
|
|
|
elif func == 'cmd_rec_cmd': |
|
|
|
|
print(cmdRecCmd()) |
|
|
|
|
elif func == 'lsyncd_list': |
|
|
|
|
print(lsyncdList()) |
|
|
|
|
elif func == 'lsyncd_add': |
|
|
|
|
print(lsyncdAdd()) |
|
|
|
|
elif func == 'lsyncd_get': |
|
|
|
|
print(lsyncdGet()) |
|
|
|
|
elif func == 'lsyncd_delete': |
|
|
|
|
print(lsyncdDelete()) |
|
|
|
|
elif func == 'lsyncd_run': |
|
|
|
|
print(lsyncdRun()) |
|
|
|
|
elif func == 'lsyncd_log': |
|
|
|
|
print(lsyncdLog()) |
|
|
|
|
elif func == 'lsyncd_conf_log': |
|
|
|
|
print(lsyncdConfLog()) |
|
|
|
|
elif func == 'lsyncd_get_exclude': |
|
|
|
|
print(lsyncdGetExclude()) |
|
|
|
|
elif func == 'lsyncd_remove_exclude': |
|
|
|
|
print(lsyncdRemoveExclude()) |
|
|
|
|
elif func == 'lsyncd_add_exclude': |
|
|
|
|
print(lsyncdAddExclude()) |
|
|
|
|
else: |
|
|
|
|
print('error') |
|
|
|
|