diff --git a/spug_api/apps/config/views.py b/spug_api/apps/config/views.py index 64f69f15..f2e5a72d 100644 --- a/spug_api/apps/config/views.py +++ b/spug_api/apps/config/views.py @@ -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 @@ -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() diff --git a/spug_api/apps/exec/transfer.py b/spug_api/apps/exec/transfer.py index e09ce6b8..4f85117b 100644 --- a/spug_api/apps/exec/transfer.py +++ b/spug_api/apps/exec/transfer.py @@ -41,10 +41,16 @@ 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() @@ -52,8 +58,10 @@ def post(self, request): 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}') diff --git a/spug_api/apps/schedule/builtin.py b/spug_api/apps/schedule/builtin.py index cf0541ed..2bf9547b 100644 --- a/spug_api/apps/schedule/builtin.py +++ b/spug_api/apps/schedule/builtin.py @@ -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()