Files
37duAutomation/docs/部署/服务端前后端部署SOP.md
T
puddy 843ff244a6 feat: 新增多插件 UI 支持及运行日志集中回传功能
本次提交包含多项功能更新:
1. 为多个插件(xq0002/0003/0004/0008/0010/0011/0020/0031_xq0032/0033/0035/0037/0045_xq0046/0047/0048)新增 UI 界面及配置支持
2. 新增运行日志集中回传功能,客户端自动上传运行日志到中心机集中存储
3. 新增插件分类字段支持,用于场景库分组管理
4. 优化 xq0034/0037/0048 等插件的功能及描述
5. 修复控制面响应兼容问题,支持新旧版本数据格式
6. 新增 xq0033 插件的状态存储模块,用于历史记录管理
7. 优化 CRM 登录窗口模式,支持全屏置顶
2026-10-26 15:42:50 +08:00

161 lines
7.4 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-*`)。
---
## 七、运行日志集中回传(2026-10 新增)
执行机(electron-shell)运行日志 + 壳日志自动上送中心机集中留存,管理端「运行日志」页统一排查。
- 后端:`run_logs` / `device_logs` 两张新表为 `CREATE TABLE IF NOT EXISTS`,**重启项目自动建表,无需手工迁移**;
日志文件落盘 `controlplane/logs/shell/<device_id>.log`(随源码目录)。
- 保留天数:`controlplane/.env` 加 `LOG_RETENTION_DAYS=30`(默认 30 天,超期自动清理,启动 + 每 6 小时 + 管理端手动)。
- 前端:本功能已包含在 `controlplane-web-dist.zip`(重新构建产物),按上方前端部署流程发布即可。
- 客户端侧改动随壳子版本发布(`electron/log-upload.js` 等),执行机升级新壳子后自动开始回传,无需额外配置。
- 详细设计/接口/验证步骤见 [运行日志集中回传](../桌面端/运行日志集中回传.md)。