Files
37duAutomation/.trae/documents/控制面前后端部署SOP.md
T
puddy 6d2dd93628 refactor: 整体迁移控制面至MySQL + 替换UI框架为Element Plus
1. 移除naive-ui依赖,迁移前端至Element Plus
2. 重构后端数据库层,切换为MySQL替代SQLite
3. 调整项目部署架构,停止exe打包改为源码部署
4. 新增设备上报接口与相关数据库模型
5. 优化配置路径与SQL语法,替换为MySQL占位符格式
6. 调整站点名称与文案,统一为「37du 插件管理平台」
7. 新增SQLite到MySQL迁移脚本
2026-09-16 15:58:45 +08:00

148 lines
6.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 37du 中心机控制面 · 前后端部署 SOP
> 对象:控制面(controlplane)前端 + 后端部署到宝塔面板服务器(117.72.191.188)。
> 用途:下次改完代码后按此文档快速部署,无需重新勘察。
> 本文档由 2026-09-16 实际部署过程沉淀,路径/端口/命令均为已验证事实。
---
## 一、架构现状(已落地)
| 项 | 值 |
|---|---|
| 前端 | 独立静态站(html 项目),与后端解耦 |
| 前端站点名 | `117.72.191.188`(面板 html 项目,id=2) |
| 前端访问地址 | `http://117.72.191.188:8090` |
| 前端根目录 | `/www/wwwroot/controlplane-web` |
| 前端 nginx 配置 | `/www/server/panel/vhost/nginx/html_117.72.191.188.conf` |
| 前端伪静态 | `/www/server/panel/vhost/rewrite/html_117.72.191.188.conf`(SPA try_files + /api 反代) |
| 后端 | Python 项目 `controlplane`,gunicorn + FastAPI,端口 8800 |
| 后端源码目录 | `/www/wwwroot/controlplane/controlplane/`(rfile: `app.py`,call_app: `app`) |
| 后端日志 | `/www/wwwlogs/python/controlplane` |
| 服务器外网 IP | `117.72.191.188`,宝塔 MCP 入口 `8765` |
**关键架构决策**:前端已从「后端 FastAPI 挂载 dist」改为独立静态站。
`app.py` 中**已删除** `app.mount("/", StaticFiles(...))`,后端不再提供页面,只出 `/api/*`。
`core/config.py` 中的 `DIST` 常量已删除;中心机**不再 exe 打包**(`37du-center.spec`、`register_autostart.bat`、`stop_center.bat` 已移除,统一源码部署)。
---
## 二、前端部署流程(改完 frontend/ 后)
### 1. 本地构建
```powershell
cd d:\work\code\37duAutomation\desktop\controlplane\frontend
npm install # 首次/依赖变更后
npm run build # 产物在 dist/(index.html + assets/index-*.js/css)
```
### 2. 打包
```powershell
Compress-Archive -Path "dist\*" -DestinationPath "controlplane-web-dist.zip" -Force
```
### 3. 上传(宝塔 MCP)
1. MCP 工具 `Upload`,`file_name: "controlplane-web-dist.zip"`(可带 size)
2. 返回 `next_action.windows` 后,在本机 PowerShell 执行该 curl(替换 `<LOCAL_FILE>` 为本地绝对路径)
3. 上传成功返回服务器路径:`/www/server/panel/data/agent/mcp_uploads/files/key_8b967e9fd815/<file_id>/controlplane-web-dist.zip`
### 4. 服务器部署(MCP Bash)
```bash
cd /www/wwwroot/controlplane-web
mkdir -p /www/backup/cp-web-old
mv -f index.html /www/backup/cp-web-old/ 2>/dev/null
mv -f assets /www/backup/cp-web-old/assets-$(date +%H%M%S) 2>/dev/null # 清旧产物,避免哈希文件堆积
unzip -o /www/server/panel/data/agent/mcp_uploads/files/key_8b967e9fd815/<file_id>/controlplane-web-dist.zip
chown -R www:www /www/wwwroot/controlplane-web/index.html /www/wwwroot/controlplane-web/assets
```
> 注意:**不要用 `rm -rf`** 清理站点目录——宝塔 MCP 会按"递归强删关键路径"拦截命令,必须改用 `mv` 移到备份目录再解压。
> 也不要对整个站点目录 `chown -R`,`/www/wwwroot/controlplane-web/.user.ini` 是锁定文件会报 `Operation not permitted` 并中断。
### 5. 验证
```bash
curl -s http://127.0.0.1:8090/ | head -c 300 # 首页 200,含新 index-*.js 引用
curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8090/assets/index-*.js
curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8090/api/admin/overview # 401 = 反代到后端鉴权正常
```
---
## 三、后端部署流程(改完 controlplane 后端代码后)
### 1. 语法校验(本地)
```powershell
& "d:\work\code\37duAutomation\.venv\Scripts\python.exe" -c "import ast; ast.parse(open(r'd:\work\code\37duAutomation\desktop\controlplane\app.py', encoding='utf-8').read()); print('OK')"
```
### 2. 逐个上传改动文件(MCP `Upload` + 本机 curl,同前端步骤 3)
常用文件:`app.py`、`core/config.py`、`core/db.py` 等
### 3. 覆盖到服务器(MCP Bash,先备份)
```bash
cd /www/wwwroot/controlplane/controlplane
cp app.py app.py.bak-$(date +%Y%m%d)
cp /www/server/panel/data/agent/mcp_uploads/files/key_8b967e9fd815/<file_id>/app.py app.py
chown root:root app.py
```
### 4. 重启项目
MCP `PythonProjectControl` → `project_name: "controlplane"`, `action: "restart"`
然后 MCP `PythonProjectInfo` 确认 `status=1`、`run=true`、`listen:[8800]`。
### 5. 验证
```bash
curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8800/ # 404 = 无静态挂载(正常)
curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8800/api/admin/overview # 401 = API 正常
```
---
## 四、nginx 伪静态配置(已写好,勿丢)
`/www/server/panel/vhost/rewrite/html_117.72.191.188.conf` 内容:
```nginx
# 控制面前端静态站(37du 中心机)
# /api 反代到后端 FastAPI 8800
location /api {
proxy_pass http://127.0.0.1:8800;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_read_timeout 120s;
}
# SPA 单页应用:子路由刷新回退 index.html
location / {
try_files $uri $uri/ /index.html;
}
```
改完 `nginx -t && nginx -s reload`。
---
## 五、踩坑记录
1. **8080 端口创建站点失败**:宝塔报「端口范围不合法」,8090 可用。非 80 端口建站时若报错直接换端口。
2. **`.user.ini` 锁定**:chown 站点根目录会失败,只对部署的文件 chown。
3. **`rm -rf` 被 MCP 拦截**:清理站点旧产物用 `mv` 到 `/www/backup/cp-web-old/`,不要用 rm -rf。
4. **zip 反斜杠警告**:Windows `Compress-Archive` 解压有 warning,`unzip` 仍能正常解出文件(exit 1,不影响)。
5. **WebFetch 无法验证 http 站点**:工具会自动升级 https,纯 http 站点用服务器 `curl 127.0.0.1` 验证。
6. **`DINGTALK_CALLBACK`** 在 `controlplane/.env`,回调域名是 `http://172.30.28.131:8800`,改前端/后端地址时需同步。
7. **前端 hash 路由**(`#/xxx`),静态站刷新天然安全,无需后端 history 回退。
8. **上传文件默认 `root:root/0600`**,nginx 读不到需 `chown www:www`(或至少 0644)。
---
## 六、本地前端开发
```powershell
cd d:\work\code\37duAutomation\desktop\controlplane\frontend
npm run dev # 5173,vite 代理 /api → http://127.0.0.1:8800
```
UI 组件库:Element Plus(已移除 naive-ui);图标:`@element-plus/icons-vue`(全局注册)。
主题:浅色「纸张+墨色」编辑部风格,设计令牌集中在 `src/style.css` 的 `:root`(`--cp-*` 与 `--el-*`)。