U 优化文件分发进度展示

pull/586/head
vapao 2022-07-09 23:20:27 +08:00
parent ec7ea4df8a
commit 0546c47762
2 changed files with 41 additions and 23 deletions

View File

@ -10,13 +10,14 @@ from apps.account.utils import has_host_perm
from apps.host.models import Host from apps.host.models import Host
from apps.setting.utils import AppSetting from apps.setting.utils import AppSetting
from libs import json_response, JsonParser, Argument, auth from libs import json_response, JsonParser, Argument, auth
from libs.utils import str_decode from libs.utils import str_decode, human_seconds_time
from concurrent import futures from concurrent import futures
from threading import Thread from threading import Thread
import subprocess import subprocess
import tempfile import tempfile
import uuid import uuid
import json import json
import time
import os import os
@ -97,7 +98,10 @@ def _dispatch_sync(task):
for t in futures.as_completed(threads): for t in futures.as_completed(threads):
exc = t.exception() exc = t.exception()
if exc: if exc:
rds.publish(t.token, json.dumps({'key': t.key, 'status': -1, 'data': f'\x1b[31mException: {exc}\x1b[0m'})) rds.publish(
t.token,
json.dumps({'key': t.key, 'status': -1, 'data': f'\x1b[31mException: {exc}\x1b[0m'})
)
if task.host_id: if task.host_id:
command = f'umount -f {task.src_dir} && rm -rf {task.src_dir}' command = f'umount -f {task.src_dir} && rm -rf {task.src_dir}'
else: else:
@ -113,20 +117,31 @@ def _do_sync(rds, task, host):
fp.write(host.pkey or AppSetting.get('private_key')) fp.write(host.pkey or AppSetting.get('private_key'))
fp.flush() fp.flush()
flag = time.time()
options = '-azv --progress' if task.host_id else '-rzv --progress' options = '-azv --progress' if task.host_id else '-rzv --progress'
argument = f'{task.src_dir}/ {host.username}@{host.hostname}:{task.dst_dir}' argument = f'{task.src_dir}/ {host.username}@{host.hostname}:{task.dst_dir}'
command = f'rsync {options} -h -e "ssh -p {host.port} -o StrictHostKeyChecking=no -i {fp.name}" {argument}' command = f'rsync {options} -h -e "ssh -p {host.port} -o StrictHostKeyChecking=no -i {fp.name}" {argument}'
task = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) task = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
message = b''
while True: while True:
message = task.stdout.readline() output = task.stdout.read(1)
if not message: if not output:
break break
message = str_decode(message).rstrip('\r\n') if output in (b'\r', b'\n'):
if 'rsync: command not found' in message: message += b'\r\n' if output == b'\n' else b'\r'
data = '\r\n\x1b[31m检测到该主机未安装rsync可通过批量执行/执行任务模块进行以下命令批量安装\x1b[0m' message = str_decode(message)
data += '\r\nCentos/Redhat: yum install -y rsync' if 'rsync: command not found' in message:
data += '\r\nUbuntu/Debian: apt install -y rsync' data = '\r\n\x1b[31m检测到该主机未安装rsync可通过批量执行/执行任务模块进行以下命令批量安装\x1b[0m'
rds.publish(token, json.dumps({'key': host.id, 'data': data})) data += '\r\nCentos/Redhat: yum install -y rsync'
break data += '\r\nUbuntu/Debian: apt install -y rsync'
rds.publish(token, json.dumps({'key': host.id, 'data': message + '\r\n'})) rds.publish(token, json.dumps({'key': host.id, 'data': data}))
break
rds.publish(token, json.dumps({'key': host.id, 'data': message}))
message = b''
else:
message += output
status = task.wait()
if status == 0:
human_time = human_seconds_time(time.time() - flag)
rds.publish(token, json.dumps({'key': host.id, 'data': f'\r\n\x1b[32m** 分发完成,总耗时:{human_time} **\x1b[0m'}))
rds.publish(token, json.dumps({'key': host.id, 'status': task.wait()})) rds.publish(token, json.dumps({'key': host.id, 'status': task.wait()}))

View File

@ -58,17 +58,20 @@ def parse_time(value):
# 传两个时间得到一个时间差 # 传两个时间得到一个时间差
def human_diff_time(time1, time2): def human_seconds_time(seconds):
time1 = parse_time(time1) text = ''
time2 = parse_time(time2) if seconds >= 3600:
delta = time1 - time2 if time1 > time2 else time2 - time1 text += '%d小时' % (seconds / 3600)
if delta.seconds < 60: seconds = seconds % 3600
text = '%d' % delta.seconds if seconds >= 60:
elif delta.seconds < 3600: text += '%d' % (seconds / 60)
text = '%d' % (delta.seconds / 60) seconds = seconds % 60
else: if seconds > 0:
text = '%d小时' % (delta.seconds / 3600) if text or isinstance(seconds, int):
return '%d%s' % (delta.days, text) if delta.days else text text += '%.d秒' % seconds
else:
text += '%.1f' % seconds
return text
# 字符串模版渲染 # 字符串模版渲染