Skip to content

Commit

Permalink
U 优化文件分发使用体验
Browse files Browse the repository at this point in the history
  • Loading branch information
vapao committed Jul 31, 2022
1 parent eefda7d commit cd85141
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions spug_api/apps/config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.db.models import F
from libs import json_response, JsonParser, Argument, auth
from apps.app.models import Deploy, App
from apps.repository.models import Repository
from apps.config.models import *
import json
import re
Expand Down Expand Up @@ -70,6 +71,8 @@ def delete(self, request):
if error is None:
if Deploy.objects.filter(env_id=form.id).exists():
return json_response(error='该环境已关联了发布配置,请删除相关发布配置后再尝试删除')
if Repository.objects.filter(env_id=form.id).exists():
return json_response(error='该环境关联了构建记录,请在删除应用发布/构建仓库中相关记录后再尝试')
# auto delete configs
Config.objects.filter(env_id=form.id).delete()
ConfigHistory.objects.filter(env_id=form.id).delete()
Expand Down
10 changes: 9 additions & 1 deletion spug_api/apps/exec/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,27 @@ def post(self, request):
host_id = None
token = uuid.uuid4().hex
base_dir = os.path.join(settings.TRANSFER_DIR, token)
os.makedirs(base_dir)
if form.host:
host_id, path = json.loads(form.host)
if not path.strip('/'):
return json_response(error='请输入正确的数据源路径')
host = Host.objects.get(pk=host_id)
with host.get_ssh() as ssh:
code, _ = ssh.exec_command_raw(f'[ -d {path} ]')
if code != 0:
return json_response(error='数据源路径必须为该主机上已存在的目录')
os.makedirs(base_dir)
with tempfile.NamedTemporaryFile(mode='w') as fp:
fp.write(host.pkey or AppSetting.get('private_key'))
fp.flush()
target = f'{host.username}@{host.hostname}:{path}'
command = f'sshfs -o ro -o ssh_command="ssh -p {host.port} -i {fp.name}" {target} {base_dir}'
task = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if task.returncode != 0:
os.system(f'umount -f {base_dir} &> /dev/null ; rm -rf {base_dir}')
return json_response(error=task.stdout.decode())
else:
os.makedirs(base_dir)
index = 0
while True:
file = request.FILES.get(f'file{index}')
Expand Down
5 changes: 3 additions & 2 deletions spug_api/apps/schedule/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ def auto_run_by_day():
except IndexError:
pass

timestamp = time.time() - 24 * 3600
timestamp = time.time() - 2 * 3600
for item in Path(settings.TRANSFER_DIR).iterdir():
if item.name != '.gitkeep':
if item.stat().st_atime < timestamp:
os.system(f'rm -rf {item.absolute()}')
transfer_dir = item.absolute()
os.system(f'umount -f {transfer_dir} &> /dev/null ; rm -rf {transfer_dir}')
finally:
connections.close_all()

Expand Down

0 comments on commit cd85141

Please sign in to comment.