挂到 Koa 同一 http.Server 的 upgrade 事件(复用 8800,不占新端口、不改 nginx),X-API-Token 握手鉴权,帧格式对齐执行机 notifier.js;新建通知后立即广播(target=all / account),GET /api/notifications 轮询继续兜底。 连接状态放模块作用域(生命周期挂载与请求内推送的 provider 实例不保证同一个),心跳 30s 清理半开连接;src/index.ts 补导出(bundle 关闭 moduleDetector 时必须显式导出)。 部署 SOP 增加 WS 端点探针,客户端交接文档补 /ws/notifications 契约。
198 lines
10 KiB
Markdown
198 lines
10 KiB
Markdown
# 37du 中心机 · 服务端部署 SOP(controlplane-next + fileserver)
|
||
|
||
> 对象:内网中心机 `172.30.30.243` 上的**现役服务端**——控制面 `desktop/controlplane-next`(Midway + cool-admin 8 + Art Design Pro)与文件服务器 `desktop/fileserver`(FastAPI)。
|
||
> 用途:改完服务端代码后按本文部署,无需重新勘察。
|
||
> 最后更新:2026-10-31(依据 2026-10-22 实际部署记录 + 当前代码核对)。
|
||
> ⚠️ 第一代 FastAPI 中心机(原 `desktop/controlplane/`)已于 2026-11 从仓库移除,历史排查看 git 记录。
|
||
|
||
---
|
||
|
||
## 一、部署拓扑与路径
|
||
|
||
| 项 | 值 |
|
||
|---|---|
|
||
| 服务器 | 内网 `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` |
|
||
|
||
执行机连接顺序:`/api/plugins` 拉权限清单 → 票据下载插件 → `/api/shell/version`、`/api/file-sources/python-env` 检查更新 → `/api/runtime/report`、`/api/runtime/logs` 回报。
|
||
|
||
---
|
||
|
||
## 二、控制面配置(`desktop/controlplane-next/server/.env`)
|
||
|
||
只读 `server/.env`(加载器 `server/src/modules/center/comm/env.ts`),进程环境变量优先。
|
||
|
||
| 键 | 说明 |
|
||
|---|---|
|
||
| `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) |
|
||
|
||
密钥类值只写在服务器 `.env`,不写进仓库、不写进文档。
|
||
|
||
---
|
||
|
||
## 三、首次 / 全量部署
|
||
|
||
前置:服务器 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. 后端构建与上传
|
||
|
||
```powershell
|
||
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
|
||
```
|
||
|
||
```bash
|
||
# 服务器
|
||
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
|
||
```
|
||
|
||
### 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
|
||
```
|
||
|
||
```bash
|
||
# 服务器:备份后替换(不要 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
|
||
```
|
||
|
||
### 4. nginx(8090 站点)
|
||
|
||
```nginx
|
||
server {
|
||
listen 8090;
|
||
root /opt/37du/controlplane-web;
|
||
index 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; }
|
||
}
|
||
```
|
||
|
||
前端与后端同源(`/api`),浏览器不触发 CORS;前端生产环境 `VITE_API_URL=/`,dev 环境代理到 `http://127.0.0.1:8001`。
|
||
|
||
---
|
||
|
||
## 四、日常增量发布
|
||
|
||
| 改动 | 步骤 |
|
||
|---|---|
|
||
| 后端代码 | `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/
|
||
# 通知 WS 端点已挂载(未带票据应为 401;返回 404 说明该版本后端还没有 /ws/notifications)
|
||
curl -s -o /dev/null -w '%{http_code}\n' \
|
||
-H 'Connection: Upgrade' -H 'Upgrade: websocket' -H 'Sec-WebSocket-Version: 13' \
|
||
-H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==' \
|
||
'http://127.0.0.1:8800/ws/notifications?account=test'
|
||
```
|
||
|
||
本机发版 CLI 预检:
|
||
|
||
```powershell
|
||
.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
|
||
```
|
||
|
||
---
|
||
|
||
## 五、文件服务器(8802 / 8899)
|
||
|
||
细节见 [`desktop/fileserver/README.md`](../../desktop/fileserver/README.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 自签。
|
||
|
||
> ⚠️ 控制面只有内网 `172.30.30.243:8800` 一套(执行机唯一连接源),发版/授权/登记一律指向它。详见 [插件发版规范](../插件/发版规范.md)。
|
||
|
||
---
|
||
|
||
## 七、排错
|
||
|
||
| 现象 | 原因 / 处理 |
|
||
|---|---|
|
||
| 所有请求 500 | `server/public` 空目录缺失 → `mkdir -p` 后重启 |
|
||
| 插件列表 500 `Unknown column 'test_version'` | 线上库未执行 `migrations/20261031_plugin_test_version.sql`(实体已加列、库没加)→ 执行后立即生效,无需重启;**凡是实体新增列的改动,部署时必须同步跑 migrations** |
|
||
| 启动报 typescript 缺失 | 安装时用了 `--omit=dev` → 重新 `npm install --production=false` |
|
||
| 执行机拉不到插件 | ①`--base` 指错地址(应为 `http://172.30.30.243:8800`)②`CONTROLPLANE_TOKEN` 两端不一致 ③账号未 `grant` |
|
||
| 壳子检测不到更新 | `file_sources(category='shell')` 未登记或 `url` 指向文件服务器不可达(执行机必须能直连 8802) |
|
||
| 前端页面 401/白屏 | nginx `/api` 未反代到 8800;或站点文件权限不足(`www-data` 可读) |
|
||
| 文件服务器告警离线 | `37du-fileserver` 未起、`CONTROLPLANE_URL/TOKEN` 配错、或 60s 心跳被防火墙拦截 |
|
||
| 钉钉扫码建号失败 | 回调地址未指向内网中心机;此时扫码建号不可用,密码登录不受影响 |
|
||
|