docs: 更新 AGENTS/README 平台架构说明与月度总结,运行日志视图支持已取消状态

This commit is contained in:
2026-10-31 17:55:25 +08:00
parent e1b4097573
commit b976b80d9d
201 changed files with 11049 additions and 7398 deletions
+152 -112
View File
@@ -1,160 +1,200 @@
# 37du 中心机控制面 · 前后端部署 SOP
# 37du 中心机 · 服务端部署 SOP(controlplane-next + fileserver)
> 对象:控制面(controlplane)前端 + 后端部署到宝塔面板服务器(117.72.191.188)。
> 用途:下次改完代码后按此文档快速部署,无需重新勘察。
> 本文档由 2026-09-16 实际部署过程沉淀,路径/端口/命令均为已验证事实。
> 对象:内网中心机 `172.30.30.243` 上的**现役服务端**——控制面 `desktop/controlplane-next`(Midway + cool-admin 8 + Art Design Pro)与文件服务器 `desktop/fileserver`(FastAPI)。
> 用途:改完服务端代码后按本文部署,无需重新勘察。
> 最后更新:2026-10-31(依据 2026-10-22 实际部署记录 + 当前代码核对)。
> ⚠️ `desktop/controlplane/`(第一代 FastAPI 中心机)已 **DEPRECATED**,仅在存量排查时参考,见文末附录 A。
> 公网 `117.72.191.188`(宝塔)已停更,不再部署。
---
## 一、架构现状(已落地)
## 一、部署拓扑与路径
| 项 | 值 |
|---|---|
| 前端 | 独立静态站(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` |
| 服务器 | 内网 `172.30.30.243`(SSH `root`,凭证按运维传递,不入库) |
| 控制面后端 | Node20 / Midway,systemd `37du-controlplane-next.service`,监听 **8800** |
| 后端源码 | `/opt/37du/controlplane-next/server`(`npm run build` 后 `bootstrap.js` 启动) |
| 控制台前端 | Art Design Pro,构建产物部署到 `/opt/37du/controlplane-web`,nginx **8090**(`/api` 反代 8800) |
| 业务数据根 | `CENTER_DATA_DIR=/opt/37du/desktop/controlplane`(派生 `packages/ shell/ avatars/ logs/`) |
| 数据库 | MySQL(`MYSQL_*` 指向,生产库名见 `server/.env`),Redis 可选(`CENTER_REDIS=0` 降级本地文件缓存) |
| 文件服务器 | `/opt/37du/fileserver`,venv `/opt/37du/venv`,systemd `37du-fileserver`,下载 **8802** |
| 文件服务器管理台 | `/opt/37du/fileserver-admin`(nginx 站点 `/etc/nginx/sites-available/37du-fileserver-admin`,**8899**) |
| 文件数据/元数据 | 落盘 `/opt/37du/fileserver/data/`,MySQL 库 `37du_fileserver` 表 `fs_files` |
**关键架构决策**:前端已从「后端 FastAPI 挂载 dist」改为独立静态站。
`app.py` 中**已删除** `app.mount("/", StaticFiles(...))`,后端不再提供页面,只出 `/api/*`。
`core/config.py` 中的 `DIST` 常量已删除;中心机**不再 exe 打包**(`37du-center.spec`、`register_autostart.bat`、`stop_center.bat` 已移除,统一源码部署)。
执行机连接顺序:`/api/plugins` 拉权限清单 → 票据下载插件 → `/api/shell/version`、`/api/file-sources/python-env` 检查更新 → `/api/runtime/report`、`/api/runtime/logs` 回报。
---
## 二、前端部署流程(改完 frontend/ 后)
## 二、控制面配置(`desktop/controlplane-next/server/.env`)
### 1. 本地构建
```powershell
cd d:\work\code\37duAutomation\desktop\controlplane\frontend
npm install # 首次/依赖变更后
npm run build # 产物在 dist/(index.html + assets/index-*.js/css)
```
只读 `server/.env`(加载器 `server/src/modules/center/comm/env.ts`),进程环境变量优先。
### 2. 打包
```powershell
Compress-Archive -Path "dist\*" -DestinationPath "controlplane-web-dist.zip" -Force
```
| 键 | 说明 |
|---|---|
| `CONTROLPLANE_TOKEN` | 机器凭证;非空时执行机接口须带 `X-API-Token`,值必须与客户端 `.env` 一致 |
| `CONTROLPLANE_ADMIN_SECRET` | 管理票据 HMAC 密钥(可留空,回退 `CENTER_DATA_DIR/.admin_secret`) |
| `CENTER_PORT` | 监听端口,生产固定 `8800`(不填则 8001 起自动找空闲口) |
| `CENTER_DATA_DIR` | 业务数据根(生产 `/opt/37du/desktop/controlplane`) |
| `CENTER_REDIS` / `CENTER_REDIS_HOST` / `CENTER_REDIS_PORT` / `CENTER_REDIS_PASSWORD` | `CENTER_REDIS=0` 关闭 Redis,走本地文件缓存 |
| `MYSQL_HOST` / `MYSQL_PORT` / `MYSQL_USER` / `MYSQL_PASSWORD` / `MYSQL_DATABASE` | 数据库连接(生产配置 `synchronize:false`,不自动改表) |
| `DINGTALK_APP_KEY` / `DINGTALK_APP_SECRET` / `DINGTALK_AGENT_ID` / `DINGTALK_CALLBACK` / `DINGTALK_DEFAULT_PASSWORD` / `DINGTALK_ADMIN_NAME` | 钉钉扫码登录、自动建号、工作通知 |
| `FILESERVER_HEARTBEAT_TIMEOUT` / `FILESERVER_MONITOR_INTERVAL` | 文件服务器离线监控(默认 180s / 30s) |
| `LOG_RETENTION_DAYS` | 运行日志保留天数(默认 30) |
### 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 = 反代到后端鉴权正常
```
密钥类值只写在服务器 `.env`,不写进仓库、不写进文档。
---
## 三、后端部署流程(改完 controlplane 后端代码后)
## 三、首次 / 全量部署
前置:服务器 Node ≥ 20(后端 `engines` 为 ≥18,生产按 20)、前端构建机 Node ≥ 20.19 + pnpm ≥ 8.8、MySQL 已建库。
### 1. 数据库初始化
```bash
# 新环境:用现网只读快照初始化结构(含 cool 框架 base_sys_* 表)
mysql -h 127.0.0.1 -u <user> -p <db> < schema_prod_intranet.sql
# 之后所有增量变更只走 migrations/,手工执行后重启服务
mysql -h 127.0.0.1 -u <user> -p <db> < migrations/20261029_plugin_notes.sql
```
> `schema_prod.sql` 是公网旧库快照(仅历史参考),`schema_cool_tables.sql` 是 cool 框架自带表 DDL。
### 2. 后端构建与上传
### 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')"
cd d:\work\code\37duAutomation\desktop\controlplane-next\server
npm install
npm run build # cool entity && bundle && mwtsc --cleanOutDir
tar -czf server-dist.tar.gz -C . . # 或只传 dist/、package.json、package-lock.json、bootstrap.js
```
### 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
# 服务器
mkdir -p /opt/37du/controlplane-next/server/public # 必须存在,缺失会导致所有请求 500
cd /opt/37du/controlplane-next/server
# 解压覆盖(保留 .env,勿覆盖)
npm install --production=false # typescript 必须装(--omit=dev 会崩)
systemctl restart 37du-controlplane-next.service
systemctl status 37du-controlplane-next.service --no-pager
```
### 4. 重启项目
MCP `PythonProjectControl` → `project_name: "controlplane"`, `action: "restart"`
然后 MCP `PythonProjectInfo` 确认 `status=1`、`run=true`、`listen:[8800]`。
### 3. 前端构建与上传
```powershell
cd d:\work\code\37duAutomation\desktop\controlplane-next\frontend
pnpm install
pnpm build # vue-tsc --noEmit && vite build → frontend/dist
Compress-Archive -Path dist\* -DestinationPath controlplane-web-dist.zip -Force
```
### 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 正常
# 服务器:备份后替换(不要 rm -rf)
cd /opt/37du/controlplane-web
mkdir -p /opt/37du/backup/cp-web-$(date +%Y%m%d%H%M)
mv -f index.html assets /opt/37du/backup/cp-web-$(date +%Y%m%d%H%M)/ 2>/dev/null
unzip -o /tmp/controlplane-web-dist.zip
chown -R www-data:www-data /opt/37du/controlplane-web # 按实际 nginx 用户
sudo nginx -t && sudo nginx -s reload
```
---
## 四、nginx 伪静态配置(已写好,勿丢)
`/www/server/panel/vhost/rewrite/html_117.72.191.188.conf` 内容:
### 4. nginx(8090 站点)
```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;
}
server {
listen 8090;
root /opt/37du/controlplane-web;
index index.html;
# SPA 单页应用:子路由刷新回退 index.html
location / {
try_files $uri $uri/ /index.html;
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_http_version 1.1;
proxy_read_timeout 120s;
client_max_body_size 64m; # 插件包 / 头像上传
}
location / { try_files $uri $uri/ /index.html; }
}
```
改完 `nginx -t && nginx -s reload`。
前端与后端同源(`/api`),浏览器不触发 CORS;前端生产环境 `VITE_API_URL=/`,dev 环境代理到 `http://127.0.0.1:8001`。
---
## 五、踩坑记录
## 四、日常增量发布
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)。
| 改动 | 步骤 |
|---|---|
| 后端代码 | `npm run build` → 上传产物 → `systemctl restart 37du-controlplane-next.service` |
| 前端代码 | `pnpm build` → 上传 `dist/` → 无需重启后端,只 `nginx -s reload`(改 nginx 配置时) |
| 数据库 | 执行 `migrations/*.sql` → 重启后端 |
| 文件服务器 | 上传代码到 `/opt/37du/fileserver` → `systemctl restart 37du-fileserver` |
---
### 验证
## 六、本地前端开发
```bash
# 后端存活(GET / 命中 cool 静态首页,非 JSON 也算活着)
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8800/
# 接口存活:未带票据应 401
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8800/api/shell/version
# 前端
curl -s http://127.0.0.1:8090/ | head -c 200
# 文件服务器
curl -s http://127.0.0.1:8802/
```
本机发版 CLI 预检:
```powershell
cd d:\work\code\37duAutomation\desktop\controlplane\frontend
npm run dev # 5173,vite 代理 /api → http://127.0.0.1:8800
.venv\Scripts\python.exe tools\agent_cli.py --admin puddy --password 123456 --base http://172.30.30.243:8800 ping
.venv\Scripts\python.exe tools\agent_cli.py --admin puddy --password 123456 --base http://172.30.30.243:8800 me
```
UI 组件库:Element Plus(已移除 naive-ui);图标:`@element-plus/icons-vue`(全局注册)。
主题:浅色「纸张+墨色」编辑部风格,设计令牌集中在 `src/style.css` 的 `:root`(`--cp-*` 与 `--el-*`)。
---
## 七、运行日志集中回传(2026-10 新增)
## 五、文件服务器(8802 / 8899)
执行机(electron-shell)运行日志 + 壳日志自动上送中心机集中留存,管理端「运行日志」页统一排查。
细节见 [`desktop/fileserver/README.md`](../../desktop/fileserver/README.md),部署要点:
- 后端:`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)。
* 代码 `/opt/37du/fileserver`,venv `/opt/37du/venv`,数据 `/opt/37du/fileserver/data`,服务 `37du-fileserver`。
* `.env` 关键键:`FILESERVER_PORT`(8802)、`FILESERVER_TOKEN`(上传/删除/列表鉴权,下载开放)、`FILESERVER_BASE_URL`(`http://172.30.30.243:8802`,用于拼直链)、`CONTROLPLANE_URL` + `CONTROLPLANE_TOKEN`(心跳上报)、`FILESERVER_DB_*`(库 `37du_fileserver`)、`FILESERVER_ADMIN_USER/PASSWORD`。
* 心跳:文件服务器每 60s 主动 `POST /api/fileserver/heartbeat` 到控制面,超时 180s 控制面标离线告警——**控制面不调用文件服务器**,只在 `file_sources` 表保存直链。
* 上传链路:`agent_cli file push` 先 `POST {fs}/api/upload`,再登记 `POST /api/admin/file-sources`。
---
## 六、发版 CLI(`tools/agent_cli.py`)
* 位置:仓库根 `tools/agent_cli.py`,任意 cwd 可执行;旧的 `python -m controlplane.tools.agent_cli` 已移除。
* 配置读取:`desktop/controlplane-next/server/.env` → `desktop/fileserver/.env`(**不读** `desktop/controlplane/.env`)。
* 默认 base:`CONTROLPLANE_AGENT_BASE` 或 `http://127.0.0.1:${CONTROLPLANE_PORT:-8800}`;**跨机器操作一律显式 `--base http://172.30.30.243:8800`**。
* 常用子命令:`publish` / `grant` / `revoke` / `plugins` / `plugin-status` / `users` / `user-add` / `user-reset-pwd` / `shell-release` / `file push` / `file list` / `file rm` / `stats` / `ping` / `me`。
* 鉴权:`--admin <账号> --password <密码>`(换 1 小时管理票据),或中心机本机用 `.admin_secret` HMAC 自签。
> ⚠️ 内网 8800 与公网 117.72.191.188:8800 是**两套不同步的库**。发到公网库的症状:执行机永远"已是最新版本"。详见 [插件发版规范](../插件/发版规范.md)。
---
## 七、排错
| 现象 | 原因 / 处理 |
|---|---|
| 所有请求 500 | `server/public` 空目录缺失 → `mkdir -p` 后重启 |
| 启动报 typescript 缺失 | 安装时用了 `--omit=dev` → 重新 `npm install --production=false` |
| 执行机拉不到插件 | ①`--base` 发到了公网旧库 ②`CONTROLPLANE_TOKEN` 两端不一致 ③账号未 `grant` |
| 壳子检测不到更新 | `file_sources(category='shell')` 未登记或 `url` 指向文件服务器不可达(执行机必须能直连 8802) |
| 前端页面 401/白屏 | nginx `/api` 未反代到 8800;或站点文件权限不足(`www-data` 可读) |
| 文件服务器告警离线 | `37du-fileserver` 未起、`CONTROLPLANE_URL/TOKEN` 配错、或 60s 心跳被防火墙拦截 |
| 钉钉扫码建号失败 | 回调仍登记在公网旧库;内网切换后扫码建号不可用,密码登录不受影响 |
---
## 附录 A:旧 FastAPI 控制面(DEPRECATED,仅历史参考)
* 代码:`desktop/controlplane/`(文件头已标 DEPRECATED),启动 `python -m controlplane.app`(cwd = `desktop/`),端口 8800,MySQL 库 `37du_center`。
* 旧部署(已停更,不再执行):宝塔服务器 `117.72.191.188`,后端 gunicorn `/www/wwwroot/controlplane/controlplane/`,前端静态站 8090 `/www/wwwroot/controlplane-web`,伪静态 `html_117.72.191.188.conf` 反代 `/api`。
* 该库线上仍有数据,但**执行机不再连接**;任何发版、授权、登记都不允许再写这套。
* 旧命令行 `python -m controlplane.tools.agent_cli` 已删除,统一用仓库根 `tools/agent_cli.py`。