From b727f06318f52a000556ee40ac9712b3faa05099 Mon Sep 17 00:00:00 2001 From: vapao Date: Mon, 24 Oct 2022 17:51:40 +0800 Subject: [PATCH] =?UTF-8?q?U=20=E5=AE=8C=E5=96=84=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=88=A0=E9=99=A4=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/deploy/models.py | 11 ++++++++--- spug_api/apps/repository/models.py | 11 ++++++++--- spug_api/libs/ssh.py | 9 ++++----- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/spug_api/apps/deploy/models.py b/spug_api/apps/deploy/models.py index 521f77b..92da1d5 100644 --- a/spug_api/apps/deploy/models.py +++ b/spug_api/apps/deploy/models.py @@ -7,8 +7,8 @@ from libs import ModelMixin, human_datetime from apps.account.models import User from apps.app.models import Deploy from apps.repository.models import Repository +from pathlib import Path import json -import os class DeployRequest(models.Model, ModelMixin): @@ -63,11 +63,16 @@ class DeployRequest(models.Model, ModelMixin): if not DeployRequest.objects.filter(repository=self.repository).exists(): self.repository.delete() if self.deploy.extend == '2': + for p in Path(settings.REPOS_DIR, str(self.deploy_id)).glob(f'{self.spug_version}*'): + try: + p.unlink() + except FileNotFoundError: + pass + for p in Path(settings.DEPLOY_DIR).glob(f'{self.deploy_key}:*'): try: - os.remove(os.path.join(settings.REPOS_DIR, str(self.deploy_id), self.spug_version)) + p.unlink() except FileNotFoundError: pass - #TODO: 清理日志文件, 删除自定义发布tar.gz文件 def __repr__(self): return f'' diff --git a/spug_api/apps/repository/models.py b/spug_api/apps/repository/models.py index 1bc5b1d..4f5d27b 100644 --- a/spug_api/apps/repository/models.py +++ b/spug_api/apps/repository/models.py @@ -7,8 +7,8 @@ from libs.mixins import ModelMixin from apps.app.models import App, Environment, Deploy from apps.account.models import User from datetime import datetime +from pathlib import Path import json -import os class Repository(models.Model, ModelMixin): @@ -56,11 +56,16 @@ class Repository(models.Model, ModelMixin): def delete(self, using=None, keep_parents=False): super().delete(using, keep_parents) try: - build_file = f'{self.spug_version}.tar.gz' - os.remove(os.path.join(settings.BUILD_DIR, build_file)) + Path(settings.BUILD_DIR, f'{self.spug_version}.tar.gz').unlink() except FileNotFoundError: pass + for p in Path(settings.DEPLOY_DIR).glob(f'{self.deploy_key}:*'): + try: + p.unlink() + except FileNotFoundError: + pass + class Meta: db_table = 'repositories' ordering = ('-id',) diff --git a/spug_api/libs/ssh.py b/spug_api/libs/ssh.py index 16558bc..7beca84 100644 --- a/spug_api/libs/ssh.py +++ b/spug_api/libs/ssh.py @@ -190,6 +190,7 @@ class SSH: command = '[ -n "$BASH_VERSION" ] && set +o history\n' command += '[ -n "$ZSH_VERSION" ] && set +o zle && set -o no_nomatch\n' command += 'export PS1= && stty -echo\n' + command += self._make_env_command(self.default_env) command += f'echo {self.eof} $$\n' self.channel.sendall(command) out = '' @@ -220,7 +221,7 @@ class SSH: def _make_env_command(self, environment): if not environment: - return None + return '' str_envs = [] for k, v in environment.items(): k = k.replace('-', '_') @@ -228,7 +229,7 @@ class SSH: v = v.replace("'", "'\"'\"'") str_envs.append(f"{k}='{v}'") str_envs = ' '.join(str_envs) - return f'export {str_envs}' + return f'export {str_envs}\n' def _handle_command(self, command, environment): new_command = commands = '' @@ -236,9 +237,7 @@ class SSH: self.exec_file = f'/tmp/spug.{uuid4().hex}' commands += f'trap \'rm -f {self.exec_file}\' EXIT\n' - env_command = self._make_env_command(environment) - if env_command: - new_command += f'{env_command}\n' + new_command += self._make_env_command(environment) new_command += command new_command += f'\necho {self.eof} $?\n' self.put_file_by_fl(StringIO(new_command), self.exec_file)