From df35018805f8d01235332bb38abac480f3c4124a Mon Sep 17 00:00:00 2001 From: leon Date: Mon, 10 Aug 2026 21:41:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E5=A2=9E=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E9=83=A8=E7=BD=B2=E6=9B=B4=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/core/admin.py | 83 ++++++++- apps/core/deploy.py | 167 ++++++++++++++++++ apps/core/migrations/0003_deploylog.py | 32 ++++ apps/core/models.py | 34 ++++ static/css/style.css | 6 +- .../admin/core/deploylog/change_list.html | 23 +++ templates/admin/core/deploylog/detail.html | 110 ++++++++++++ templates/core/home.html | 2 +- 部署文档.md | 61 +++++++ 9 files changed, 513 insertions(+), 5 deletions(-) create mode 100644 apps/core/deploy.py create mode 100644 apps/core/migrations/0003_deploylog.py create mode 100644 templates/admin/core/deploylog/change_list.html create mode 100644 templates/admin/core/deploylog/detail.html diff --git a/apps/core/admin.py b/apps/core/admin.py index e3d4bd5..ed3803b 100644 --- a/apps/core/admin.py +++ b/apps/core/admin.py @@ -1,7 +1,12 @@ from django.contrib import admin from django.db import models +from django.shortcuts import redirect, render +from django.urls import path, reverse +from django.utils.html import format_html +from django.contrib import messages from ckeditor_uploader.widgets import CKEditorUploadingWidget -from .models import SiteSettings, Banner +from .models import SiteSettings, Banner, DeployLog +from .deploy import start_deploy @admin.register(SiteSettings) @@ -64,3 +69,79 @@ class BannerAdmin(admin.ModelAdmin): 'fields': ('link_url', 'sort_order', 'is_active') }), ) + + +@admin.register(DeployLog) +class DeployLogAdmin(admin.ModelAdmin): + list_display = ('started_at', 'status', 'triggered_by', 'duration', 'view_output_link') + list_filter = ('status',) + readonly_fields = ('id', 'triggered_by', 'started_at', 'finished_at', 'status', 'output') + ordering = ('-started_at',) + list_per_page = 20 + + def has_add_permission(self, request): + return False + + def has_change_permission(self, request, obj=None): + return False + + def has_delete_permission(self, request, obj=None): + return request.user.is_superuser + + def view_output_link(self, obj): + if not obj.id: + return '-' + url = reverse('admin:core_deploylog_detail', args=[obj.id]) + return format_html('查看日志', url) + view_output_link.short_description = '输出日志' + + def get_urls(self): + urls = super().get_urls() + custom_urls = [ + path( + 'deploy/', + self.admin_site.admin_view(self.deploy_view), + name='core_deploy_trigger', + ), + path( + '/detail/', + self.admin_site.admin_view(self.detail_view), + name='core_deploylog_detail', + ), + ] + return custom_urls + urls + + def deploy_view(self, request): + """触发一次部署。""" + # 防止并发部署:检查是否有正在执行的任务 + if DeployLog.objects.filter(status='running').exists(): + messages.error(request, '已有部署任务正在执行,请等待其完成后再试。') + return redirect('admin:core_deploylog_changelist') + + log = start_deploy(user=request.user) + messages.success(request, f'部署已启动(日志 ID: {str(log.id)[:8]}),请刷新页面查看进度。') + return redirect('admin:core_deploylog_detail', log_id=str(log.id)) + + def detail_view(self, request, log_id): + """展示单条部署日志的实时输出。""" + try: + log = DeployLog.objects.get(id=log_id) + except (DeployLog.DoesNotExist, ValueError): + messages.error(request, '部署日志不存在。') + return redirect('admin:core_deploylog_changelist') + + context = { + **self.admin_site.each_context(request), + 'opts': self.model._meta, + 'log': log, + 'title': f'部署日志 - {log.started_at:%Y-%m-%d %H:%M:%S}', + 'is_running': log.status == 'running', + } + return render(request, 'admin/core/deploylog/detail.html', context) + + def changelist_view(self, request, extra_context=None): + """在列表页顶部插入"一键部署"按钮。""" + extra_context = extra_context or {} + extra_context['deploy_url'] = reverse('admin:core_deploy_trigger') + extra_context['has_running'] = DeployLog.objects.filter(status='running').exists() + return super().changelist_view(request, extra_context) diff --git a/apps/core/deploy.py b/apps/core/deploy.py new file mode 100644 index 0000000..63d66bc --- /dev/null +++ b/apps/core/deploy.py @@ -0,0 +1,167 @@ +"""在线部署模块:从 Git 拉取最新代码并完成更新。 + +执行流程: +1. git pull origin +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 diff --git a/apps/core/migrations/0003_deploylog.py b/apps/core/migrations/0003_deploylog.py new file mode 100644 index 0000000..ec9bf70 --- /dev/null +++ b/apps/core/migrations/0003_deploylog.py @@ -0,0 +1,32 @@ +# Generated manually for DeployLog model + +import uuid +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ('core', '0002_sitesettings_about_content_en_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='DeployLog', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('started_at', models.DateTimeField(auto_now_add=True, verbose_name='开始时间')), + ('finished_at', models.DateTimeField(blank=True, null=True, verbose_name='结束时间')), + ('status', models.CharField(choices=[('running', '执行中'), ('success', '成功'), ('failed', '失败')], default='running', max_length=20, verbose_name='状态')), + ('output', models.TextField(blank=True, default='', verbose_name='输出日志')), + ('triggered_by', models.ForeignKey(null=True, on_delete=models.SET_NULL, related_name='deploy_logs', to=settings.AUTH_USER_MODEL, verbose_name='触发者')), + ], + options={ + 'verbose_name': '部署日志', + 'verbose_name_plural': '部署日志', + 'ordering': ['-started_at'], + }, + ), + ] diff --git a/apps/core/models.py b/apps/core/models.py index ed90467..b6e69bb 100644 --- a/apps/core/models.py +++ b/apps/core/models.py @@ -1,3 +1,4 @@ +import uuid from django.db import models @@ -63,3 +64,36 @@ class Banner(models.Model): def __str__(self): return self.title_zh or f'Banner #{self.id}' + + +class DeployLog(models.Model): + STATUS_CHOICES = [ + ('running', '执行中'), + ('success', '成功'), + ('failed', '失败'), + ] + + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + triggered_by = models.ForeignKey( + 'auth.User', on_delete=models.SET_NULL, null=True, + verbose_name='触发者', related_name='deploy_logs' + ) + started_at = models.DateTimeField('开始时间', auto_now_add=True) + finished_at = models.DateTimeField('结束时间', null=True, blank=True) + status = models.CharField('状态', max_length=20, choices=STATUS_CHOICES, default='running') + output = models.TextField('输出日志', blank=True, default='') + + class Meta: + ordering = ['-started_at'] + verbose_name = '部署日志' + verbose_name_plural = verbose_name + + def __str__(self): + return f'{self.started_at:%Y-%m-%d %H:%M:%S} - {self.get_status_display()}' + + @property + def duration(self): + if self.finished_at: + delta = self.finished_at - self.started_at + return f'{delta.total_seconds():.1f}s' + return '-' diff --git a/static/css/style.css b/static/css/style.css index d555cb8..b4caca3 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -70,7 +70,7 @@ a:hover { color: var(--primary-dark); } .section-title h2 { margin-bottom: 0.75rem; } .section-title p { color: var(--text-secondary); margin-bottom: 0; } -.banner-carousel .carousel-item { height: 650px; background: #000; } +.banner-carousel .carousel-item { height: 750px; background: #000; } .banner-carousel .carousel-item img { width: 100%; height: 100%; object-fit: cover; opacity: 0.85; } .banner-carousel .carousel-caption { bottom: 25%; text-align: left; } .banner-carousel .carousel-caption h1 { font-size: 2.5rem; margin-bottom: 1rem; } @@ -261,13 +261,13 @@ a:hover { color: var(--primary-dark); } .contact-form textarea.form-control { resize: vertical; min-height: 120px; } @media (max-width: 991.98px) { - .banner-carousel .carousel-item { height: 480px; } + .banner-carousel .carousel-item { height: 560px; } .banner-carousel .carousel-caption h1 { font-size: 1.75rem; } .section { padding: 2.5rem 0; } } @media (max-width: 767.98px) { - .banner-carousel .carousel-item { height: 380px; } + .banner-carousel .carousel-item { height: 440px; } .banner-carousel .carousel-caption { bottom: 20%; text-align: center; left: 10%; right: 10%; } .banner-carousel .carousel-caption h1 { font-size: 1.25rem; } .banner-carousel .carousel-caption p { font-size: 0.875rem; margin-bottom: 1rem; } diff --git a/templates/admin/core/deploylog/change_list.html b/templates/admin/core/deploylog/change_list.html new file mode 100644 index 0000000..f6f9ddc --- /dev/null +++ b/templates/admin/core/deploylog/change_list.html @@ -0,0 +1,23 @@ +{% extends "admin/change_list.html" %} +{% load i18n %} + +{% block object-tools-items %} + {% if deploy_url %} + {% if has_running %} +
  • + + {% trans '部署进行中…' %} + +
  • + {% else %} +
  • + + {% trans '一键部署更新' %} + +
  • + {% endif %} + {% endif %} + {{ block.super }} +{% endblock %} diff --git a/templates/admin/core/deploylog/detail.html b/templates/admin/core/deploylog/detail.html new file mode 100644 index 0000000..9b4686f --- /dev/null +++ b/templates/admin/core/deploylog/detail.html @@ -0,0 +1,110 @@ +{% extends "admin/base_site.html" %} +{% load i18n %} + +{% block extrastyle %} +{{ block.super }} + +{% endblock %} + +{% block content %} +
    +

    {{ title }}

    + {% if log.status == 'running' %} + + {% trans '执行中' %} + + {% elif log.status == 'success' %} + {% trans '成功' %} + {% elif log.status == 'failed' %} + {% trans '失败' %} + {% endif %} +
    + +
    + {% if log.triggered_by %}{% trans '触发者' %}: {{ log.triggered_by.username }} · {% endif %} + {% trans '开始' %}: {{ log.started_at|date:"Y-m-d H:i:s" }} + {% if log.finished_at %} · {% trans '结束' %}: {{ log.finished_at|date:"Y-m-d H:i:s" }} · {% trans '耗时' %}: {{ log.duration }}{% endif %} +
    + +
    {{ log.output|default:'(暂无输出)' }}
    + + + +{% if is_running %} + +{% endif %} +{% endblock %} diff --git a/templates/core/home.html b/templates/core/home.html index 6d2f897..2c7419f 100644 --- a/templates/core/home.html +++ b/templates/core/home.html @@ -44,7 +44,7 @@
    {% if product.main_image %} - {% ml_field product 'name' %} + {% ml_field product 'name' %} {% endif %}
    {% ml_field product 'name' %}
    diff --git a/部署文档.md b/部署文档.md index 6b766ec..145eba4 100644 --- a/部署文档.md +++ b/部署文档.md @@ -418,6 +418,67 @@ curl -I http://127.0.0.1/ --header "Host: www.tgoim.com" ### 10.1 更新代码 +#### 方式一:后台一键部署(推荐) + +网站后台已集成"在线部署"功能,可在浏览器中一键从 Git 拉取最新代码并自动完成更新,无需登录服务器。 + +**使用步骤:** + +1. 登录后台管理 `https://www.tgoim.com/admin/` +2. 在左侧菜单进入「部署日志」(核心 → 部署日志) +3. 点击右上角「一键部署更新」按钮 +4. 在确认对话框中点击「确定」 +5. 页面会跳转到日志详情页,每 3 秒自动刷新显示部署进度 +6. 部署完成后会自动重启服务 + +**部署流程会依次执行:** +1. `git pull origin main` 拉取最新代码 +2. `pip install -r requirements.txt` 安装/更新依赖 +3. `python manage.py migrate` 执行数据库迁移 +4. `python manage.py collectstatic --noinput` 收集静态文件 +5. `sudo supervisorctl restart tgoim` 重启服务 + +**前置条件(一次性配置):** + +为了让运行 Django 的 `www-data` 用户能够执行 `git pull` 和重启服务,需要配置 sudoers 允许其无密码执行特定命令。在服务器上执行: + +```bash +# 创建 sudoers 配置,允许 www-data 无密码执行 supervisorctl +sudo cat > /etc/sudoers.d/tgoim-deploy << 'EOF' +# 允许 www-data 无密码重启 tgoim 服务(用于在线部署) +www-data ALL=(root) NOPASSWD: /usr/bin/supervisorctl restart tgoim +www-data ALL=(root) NOPASSWD: /usr/bin/supervisorctl status tgoim +EOF + +# 设置正确的权限 +sudo chmod 440 /etc/sudoers.d/tgoim-deploy + +# 验证配置语法(重要!避免 sudo 配置错误导致系统问题) +sudo visudo -c +``` + +**Git 仓库权限配置:** + +确保 `www-data` 用户对项目目录有足够的权限执行 git pull: + +```bash +# 设置项目目录所有者为 www-data +sudo chown -R www-data:www-data /opt/company_website + +# 确保数据库和媒体目录可写 +sudo chown www-data:www-data /opt/company_website/db.sqlite3 +sudo chown -R www-data:www-data /opt/company_website/media +sudo chown -R www-data:www-data /opt/company_website/staticfiles +``` + +**注意事项:** +- 部署过程中请勿同时登录服务器手动执行 `git pull`,以免冲突 +- 系统会自动防止并发部署(已有任务运行时按钮会禁用) +- 如果自动重启失败,日志会提示手动执行命令,按提示操作即可 +- 所有部署操作会记录在「部署日志」中,可随时查看历史 + +#### 方式二:手动命令行部署 + ```bash cd /opt/company_website