U 完善发布记录删除逻辑

4.0
vapao 2022-10-24 17:51:40 +08:00
parent 9889c95a42
commit b727f06318
3 changed files with 20 additions and 11 deletions

View File

@ -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'<DeployRequest name={self.name}>'

View File

@ -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',)

View File

@ -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)