后台增加自动部署更新功能

This commit is contained in:
2026-08-10 21:41:36 +08:00
parent 2022664e7e
commit df35018805
9 changed files with 513 additions and 5 deletions
+167
View File
@@ -0,0 +1,167 @@
"""在线部署模块:从 Git 拉取最新代码并完成更新。
执行流程:
1. git pull origin <branch>
2. pip install -r requirements.txt
3. python manage.py migrate
4. python manage.py collectstatic --noinput
5. sudo supervisorctl restart tgoim(可选,需配置 sudoers)
为避免 supervisor 重启时中断当前请求,部署在后台线程中执行,
用户可在后台管理界面查看实时日志。
"""
import os
import sys
import subprocess
import threading
from pathlib import Path
from django.conf import settings
from django.utils import timezone
BASE_DIR = settings.BASE_DIR
# 单步命令超时(秒)
STEP_TIMEOUT = 180
# 拉取的远程分支,可通过 settings.GIT_DEPLOY_BRANCH 覆盖
GIT_BRANCH = getattr(settings, 'GIT_DEPLOY_BRANCH', 'main')
# Supervisor 服务名,可通过 settings.SUPERVISOR_SERVICE_NAME 覆盖
SUPERVISOR_SERVICE = getattr(settings, 'SUPERVISOR_SERVICE_NAME', 'tgoim')
def _venv_python():
"""返回虚拟环境中的 python 可执行文件路径。"""
if os.name == 'nt':
candidate = BASE_DIR / 'venv' / 'Scripts' / 'python.exe'
else:
candidate = BASE_DIR / 'venv' / 'bin' / 'python'
if candidate.exists():
return str(candidate)
return sys.executable
def _venv_pip():
"""返回虚拟环境中的 pip 可执行文件路径。"""
if os.name == 'nt':
candidate = BASE_DIR / 'venv' / 'Scripts' / 'pip.exe'
else:
candidate = BASE_DIR / 'venv' / 'bin' / 'pip'
if candidate.exists():
return str(candidate)
return [sys.executable, '-m', 'pip']
def _run_step(log_lines, cmd, timeout=STEP_TIMEOUT):
"""执行一条命令,将输出追加到 log_lines,返回是否成功。"""
if isinstance(cmd, str):
display = cmd
args = cmd.split()
else:
display = ' '.join(str(c) for c in cmd)
args = [str(c) for c in cmd]
log_lines.append(f'\n$ {display}')
try:
result = subprocess.run(
args, cwd=str(BASE_DIR),
capture_output=True, text=True, timeout=timeout,
)
if result.stdout:
log_lines.append(result.stdout.rstrip())
if result.stderr:
log_lines.append(result.stderr.rstrip())
if result.returncode != 0:
log_lines.append(f'命令失败,退出码: {result.returncode}')
return False
return True
except subprocess.TimeoutExpired:
log_lines.append(f'命令超时({timeout}s)')
return False
except FileNotFoundError as e:
log_lines.append(f'命令未找到: {e}')
return False
except Exception as e:
log_lines.append(f'执行异常: {e}')
return False
def _run_deploy(log_id):
"""后台线程执行部署流程。"""
from .models import DeployLog
log = DeployLog.objects.get(id=log_id)
log_lines = []
venv_python = _venv_python()
venv_pip = _venv_pip()
pip_cmd = venv_pip if isinstance(venv_pip, list) else [venv_pip]
try:
log_lines.append(f'开始部署,分支: {GIT_BRANCH}')
log_lines.append(f'工作目录: {BASE_DIR}')
log_lines.append(f'Python: {venv_python}')
log.output = '\n'.join(log_lines)
log.save()
# 1. 拉取代码
log_lines.append('\n========== [1/5] 拉取最新代码 ==========')
log.output = '\n'.join(log_lines)
log.save()
if not _run_step(log_lines, ['git', 'pull', 'origin', GIT_BRANCH]):
raise RuntimeError('git pull 失败')
# 2. 安装依赖
log_lines.append('\n========== [2/5] 安装 Python 依赖 ==========')
log.output = '\n'.join(log_lines)
log.save()
if not _run_step(log_lines, pip_cmd + ['install', '-r', 'requirements.txt']):
raise RuntimeError('pip install 失败')
# 3. 数据库迁移
log_lines.append('\n========== [3/5] 执行数据库迁移 ==========')
log.output = '\n'.join(log_lines)
log.save()
if not _run_step(log_lines, [venv_python, 'manage.py', 'migrate']):
raise RuntimeError('migrate 失败')
# 4. 收集静态文件
log_lines.append('\n========== [4/5] 收集静态文件 ==========')
log.output = '\n'.join(log_lines)
log.save()
if not _run_step(log_lines, [venv_python, 'manage.py', 'collectstatic', '--noinput']):
raise RuntimeError('collectstatic 失败')
# 5. 重启服务
log_lines.append('\n========== [5/5] 重启应用服务 ==========')
# 先把成功状态写入数据库,因为 supervisorctl restart 可能会终止当前进程
log.status = 'success'
log_lines.append(f'代码更新成功,正在重启服务 {SUPERVISOR_SERVICE} ...')
log.output = '\n'.join(log_lines)
log.finished_at = timezone.now()
log.save()
restart_ok = _run_step(log_lines, ['sudo', '-n', 'supervisorctl', 'restart', SUPERVISOR_SERVICE])
if not restart_ok:
log_lines.append(
f'提示: 自动重启失败。请在服务器手动执行: '
f'sudo supervisorctl restart {SUPERVISOR_SERVICE}'
)
# 状态仍记为 success,因为代码已更新成功,仅重启未生效
log_lines.append('\n部署流程结束。')
log.output = '\n'.join(log_lines)
log.save()
except Exception as e:
log.status = 'failed'
log_lines.append(f'\n部署失败: {e}')
log.output = '\n'.join(log_lines)
log.finished_at = timezone.now()
log.save()
def start_deploy(user=None):
"""启动一次部署(后台线程执行),返回 DeployLog 对象。"""
from .models import DeployLog
log = DeployLog.objects.create(triggered_by=user, status='running')
thread = threading.Thread(target=_run_deploy, args=(str(log.id),), daemon=True)
thread.start()
return log