mirror of https://github.com/openspug/spug
U 优化文件分发使用体验
parent
eefda7d390
commit
cd85141b1e
|
@ -5,6 +5,7 @@ from django.views.generic import View
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
from libs import json_response, JsonParser, Argument, auth
|
from libs import json_response, JsonParser, Argument, auth
|
||||||
from apps.app.models import Deploy, App
|
from apps.app.models import Deploy, App
|
||||||
|
from apps.repository.models import Repository
|
||||||
from apps.config.models import *
|
from apps.config.models import *
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
@ -70,6 +71,8 @@ class EnvironmentView(View):
|
||||||
if error is None:
|
if error is None:
|
||||||
if Deploy.objects.filter(env_id=form.id).exists():
|
if Deploy.objects.filter(env_id=form.id).exists():
|
||||||
return json_response(error='该环境已关联了发布配置,请删除相关发布配置后再尝试删除')
|
return json_response(error='该环境已关联了发布配置,请删除相关发布配置后再尝试删除')
|
||||||
|
if Repository.objects.filter(env_id=form.id).exists():
|
||||||
|
return json_response(error='该环境关联了构建记录,请在删除应用发布/构建仓库中相关记录后再尝试')
|
||||||
# auto delete configs
|
# auto delete configs
|
||||||
Config.objects.filter(env_id=form.id).delete()
|
Config.objects.filter(env_id=form.id).delete()
|
||||||
ConfigHistory.objects.filter(env_id=form.id).delete()
|
ConfigHistory.objects.filter(env_id=form.id).delete()
|
||||||
|
|
|
@ -41,10 +41,16 @@ class TransferView(View):
|
||||||
host_id = None
|
host_id = None
|
||||||
token = uuid.uuid4().hex
|
token = uuid.uuid4().hex
|
||||||
base_dir = os.path.join(settings.TRANSFER_DIR, token)
|
base_dir = os.path.join(settings.TRANSFER_DIR, token)
|
||||||
os.makedirs(base_dir)
|
|
||||||
if form.host:
|
if form.host:
|
||||||
host_id, path = json.loads(form.host)
|
host_id, path = json.loads(form.host)
|
||||||
|
if not path.strip('/'):
|
||||||
|
return json_response(error='请输入正确的数据源路径')
|
||||||
host = Host.objects.get(pk=host_id)
|
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:
|
with tempfile.NamedTemporaryFile(mode='w') as fp:
|
||||||
fp.write(host.pkey or AppSetting.get('private_key'))
|
fp.write(host.pkey or AppSetting.get('private_key'))
|
||||||
fp.flush()
|
fp.flush()
|
||||||
|
@ -52,8 +58,10 @@ class TransferView(View):
|
||||||
command = f'sshfs -o ro -o ssh_command="ssh -p {host.port} -i {fp.name}" {target} {base_dir}'
|
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)
|
task = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||||
if task.returncode != 0:
|
if task.returncode != 0:
|
||||||
|
os.system(f'umount -f {base_dir} &> /dev/null ; rm -rf {base_dir}')
|
||||||
return json_response(error=task.stdout.decode())
|
return json_response(error=task.stdout.decode())
|
||||||
else:
|
else:
|
||||||
|
os.makedirs(base_dir)
|
||||||
index = 0
|
index = 0
|
||||||
while True:
|
while True:
|
||||||
file = request.FILES.get(f'file{index}')
|
file = request.FILES.get(f'file{index}')
|
||||||
|
|
|
@ -56,11 +56,12 @@ def auto_run_by_day():
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
timestamp = time.time() - 24 * 3600
|
timestamp = time.time() - 2 * 3600
|
||||||
for item in Path(settings.TRANSFER_DIR).iterdir():
|
for item in Path(settings.TRANSFER_DIR).iterdir():
|
||||||
if item.name != '.gitkeep':
|
if item.name != '.gitkeep':
|
||||||
if item.stat().st_atime < timestamp:
|
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:
|
finally:
|
||||||
connections.close_all()
|
connections.close_all()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue