diff --git a/spug_api/apps/deploy/helper.py b/spug_api/apps/deploy/helper.py index e060742..5fda9a3 100644 --- a/spug_api/apps/deploy/helper.py +++ b/spug_api/apps/deploy/helper.py @@ -1,9 +1,11 @@ # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug # Copyright: (c) # Released under the AGPL-3.0 License. +from django.template.defaultfilters import filesizeformat from libs.utils import human_datetime, render_str from libs.spug import Notification from apps.host.models import Host +from functools import partial import subprocess import json import os @@ -259,6 +261,14 @@ class Helper: for func in self.callback: func() + def progress_callback(self, key): + def func(k, n, t): + message = f'\r {filesizeformat(n):<8}/{filesizeformat(t):>8} ' + self.send_info(k, message) + + self.send_info(key, '\r\n') + return partial(func, key) + def local(self, command, env=None): if env: env = dict(env.items()) diff --git a/spug_api/apps/deploy/utils.py b/spug_api/apps/deploy/utils.py index a516174..da48967 100644 --- a/spug_api/apps/deploy/utils.py +++ b/spug_api/apps/deploy/utils.py @@ -121,7 +121,7 @@ def _ext1_deploy(req, helper, env): _deploy_ext1_host(req, helper, h_id, new_env) req.fail_host_ids.remove(h_id) except Exception as e: - helper.send_error(h_id, f'Exception: {e}', False) + helper.send_error(h_id, f'Exception: {e}', False) for h_id in host_ids: helper.send_error(h_id, '终止发布', False) raise e @@ -147,7 +147,7 @@ def _ext2_deploy(req, helper, env): if action.get('type') == 'transfer': action['src'] = render_str(action.get('src', '').strip().rstrip('/'), env) action['dst'] = render_str(action['dst'].strip().rstrip('/'), env) - if action.get('src_mode') == '1': # upload when publish + if action.get('src_mode') == '1': # upload when publish extra = json.loads(req.extra) if 'name' in extra: action['name'] = extra['name'] @@ -245,7 +245,12 @@ def _deploy_ext1_host(req, helper, h_id, env): # transfer files tar_gz_file = f'{req.spug_version}.tar.gz' try: - ssh.put_file(os.path.join(REPOS_DIR, 'build', tar_gz_file), os.path.join(extend.dst_repo, tar_gz_file)) + callback = helper.progress_callback(host.id) + ssh.put_file( + os.path.join(REPOS_DIR, 'build', tar_gz_file), + os.path.join(extend.dst_repo, tar_gz_file), + callback + ) except Exception as e: helper.send_error(host.id, f'Exception: {e}') @@ -289,11 +294,12 @@ def _deploy_ext2_host(helper, h_id, actions, env, spug_version): dst = action['dst'] command = f'[ -e {dst} ] || mkdir -p $(dirname {dst}); [ -d {dst} ]' code, _ = ssh.exec_command_raw(command) - if code == 0: # is dir + if code == 0: # is dir if not action.get('name'): raise RuntimeError('internal error 1002') dst = dst.rstrip('/') + '/' + action['name'] - ssh.put_file(os.path.join(REPOS_DIR, env.SPUG_DEPLOY_ID, spug_version), dst) + callback = helper.progress_callback(host.id) + ssh.put_file(os.path.join(REPOS_DIR, env.SPUG_DEPLOY_ID, spug_version), dst, callback) except Exception as e: helper.send_error(host.id, f'Exception: {e}') helper.send_info(host.id, 'transfer completed\r\n') @@ -302,7 +308,8 @@ def _deploy_ext2_host(helper, h_id, actions, env, spug_version): sp_dir, sd_dst = os.path.split(action['src']) tar_gz_file = f'{spug_version}.tar.gz' try: - ssh.put_file(os.path.join(sp_dir, tar_gz_file), f'/tmp/{tar_gz_file}') + callback = helper.progress_callback(host.id) + ssh.put_file(os.path.join(sp_dir, tar_gz_file), f'/tmp/{tar_gz_file}', callback) except Exception as e: helper.send_error(host.id, f'Exception: {e}')