Ops batch: deploy automation, backup restore drill, monitoring, multi-user verification
Deploy: deploy.sh builds tarball, backs up current code (keep 3), deploys, restarts, health-gates 60s (ok/backend=postgres/db_ok), auto-rolls back on failure; rollback.sh restores latest backup. Rollback is code-only, DB migrations are additive-only so old code can still read new columns. Backup: restore_postgres refuses to restore into the production DB name unless explicitly allowed (drill never allows it); restore-drill.sh backs up fresh, restores to a test DB, compares tasks/projects/users/audit row counts, drops the test DB. Monitoring: worker writes heartbeat file every loop (atomic replace, failure only logged); GET /health adds worker_last_seen_seconds_ago + worker_ok (180s default, env tunable, still HTTP 200 when worker is stale); monitor.sh checks server/worker/health/pg_isready/docker-postgres/disk/ app dir as one JSON line, exit non-zero on any fail, plus systemd timer every 5 minutes. Push alerting to chat still needs a separate channel decision (SSH key or webhook URL). Multi-user: verify-multiuser.py creates test users/projects in memory-key only, checks RBAC via real HTTP API (owner/ maintainer/contributor/viewer, cross-project isolation, merge 200/403/409, no-key 401), test tasks carry a never- satisfiable dependency so the production worker never claims them, cleanup deletes only multiuser-test- rows (audit log is append-only and keeps test entries by design). Tests: host 27 pass, cloud 298 pass (8 skipped are environment-dependent real-Postgres tests).
This commit is contained in:
1 parent
6afef78ca8
commit
89a918b543
14 files changed
+1386
-2
No files matched your search
@@ -0,0 +1,179 @@
|
||||
# 部署与运维(deploy/)
|
||||
|
||||
本目录是主机部署与运维的脚本集,与 `cloud/` 代码分开:部署脚本只负责
|
||||
传代码、备份、重启、验证,不改任务逻辑;运维脚本只负责检查与演练,
|
||||
不跑模型、不推代码。
|
||||
|
||||
## 部署:deploy.sh
|
||||
|
||||
VM 侧运行:
|
||||
|
||||
```sh
|
||||
deploy/deploy.sh <host> [user] [--dry-run]
|
||||
# 例如:deploy/deploy.sh 124.222.165.170 root
|
||||
# 干跑(只打印步骤,不传代码、不动主机):
|
||||
# deploy/deploy.sh <host> --dry-run
|
||||
```
|
||||
|
||||
- 密码:脚本绝不接受密码参数、不读 env 文件、不打印密钥。SSH 密码由
|
||||
ssh/scp 交互提示输入,或由调用方自己的 SSH 通道(key)提供。
|
||||
- 经代理:设环境变量 `SSH_PROXY_CMD` 传入 ProxyCommand 内容,ssh 会自行
|
||||
展开 `%h %p`,例如:
|
||||
`SSH_PROXY_CMD="python3 /tmp/proxy_tunnel.py %h %p" deploy/deploy.sh <host>`;
|
||||
未设则直连 ssh。
|
||||
- 备份保留策略:每次部署前在主机上把当前 `/opt/codex-app-server` 打包到
|
||||
`/opt/codex-app-server-backups/codex-app-server-<时间戳>.tar.gz`(排除
|
||||
work/ 目录以免过大),只留最近 3 份,多余的自动删掉。
|
||||
- 健康门:重启后循环 curl `localhost:8765/health` 最多 60 秒,要求
|
||||
`ok=true` 且 `backend=postgres` 且 `db_ok=true`;失败时自动恢复刚打的
|
||||
备份包并重启,再验一次。回滚后 health 恢复也算部署失败(退出非零),
|
||||
因为新代码没上去;回滚后 health 仍失败则退出非零并明说
|
||||
“已回滚但 health 仍失败,需人工介入”。
|
||||
- 成功时只打印新代码的 git commit(从本机 git 取,不编造版本号)。
|
||||
|
||||
## 回滚:rollback.sh
|
||||
|
||||
VM 侧运行,恢复主机上最新的一份备份包:
|
||||
|
||||
```sh
|
||||
deploy/rollback.sh <host> [user]
|
||||
```
|
||||
|
||||
恢复后同样重启并验 health(60 秒),成功/失败都明说;主机上没有备份包
|
||||
时直接报错退出,不编造。
|
||||
|
||||
### 回滚边界:只回代码,不回数据库
|
||||
|
||||
数据库迁移是只加列的幂等操作(例如 projects.repo_url、tasks 的 6 列),
|
||||
回滚代码后老代码仍能读新列——新列对老代码只是多余的列,不影响已有
|
||||
查询(老代码的 SELECT 只取它认识的列,或用 `_row_get` 容错)。所以只回
|
||||
代码是安全的;数据库本身绝不会被 deploy.sh / rollback.sh 动到。这一点
|
||||
逻辑上成立的前提是:迁移永远只加列、不改列类型、不删列;如果将来有一次
|
||||
迁移要改类型或删列,必须先单独评估回滚方案,不能直接套用本脚本。
|
||||
|
||||
## 备份恢复演练:restore-drill.sh
|
||||
|
||||
主机侧运行(root,或能读 `/etc/codex-app-server/env` 的用户):
|
||||
|
||||
```sh
|
||||
sh /opt/codex-app-server/deploy/restore-drill.sh
|
||||
# 可选:BACKUP_DIR=/var/lib/codex-app-server/backups(默认就是这个)
|
||||
```
|
||||
|
||||
步骤:跑一次 `backup_postgres` 生成新鲜备份 → 新建测试库
|
||||
`codex_control_restore_test_<时间戳>` → 用 `cloud.backup.restore_postgres`
|
||||
把备份恢复进去 → 比对生产库与测试库的 tasks / projects / users /
|
||||
audit_log 行数(行数相等即通过,只比行数,不比对具体内容,不打印任何
|
||||
行内容)→ 比对完立即删掉测试库(trap 保证中途失败也删)。
|
||||
|
||||
安全红线:恢复目标永远是新生成的测试库名,绝不等于生产库名;
|
||||
`restore_postgres` 本身也会拒绝目标库名等于 `CLOUD_DB_NAME`
|
||||
(`allow_production` 绝不在本脚本里设为 True),双重保险。本脚本绝不
|
||||
恢复到生产库,绝不删生产库;全程只打印 PASS/FAIL 与行数,不打印密码,
|
||||
不打印备份内容。
|
||||
|
||||
## 监控:monitor.sh + monitor.service / monitor.timer
|
||||
|
||||
主机侧运行,codexsvc 可读权限即可,不读 env 文件:
|
||||
|
||||
```sh
|
||||
sh /opt/codex-app-server/deploy/monitor.sh # 输出一行 JSON,任一项 fail 则退出非零
|
||||
sh /opt/codex-app-server/deploy/monitor.sh --self-test # 只验证脚本机制(见下)
|
||||
```
|
||||
|
||||
检查项:systemctl is-active server/worker、curl health(ok/backend/db_ok/
|
||||
worker_ok)、pg_isready、docker ps 里 codex-postgres 在运行、磁盘使用率
|
||||
<90%、/opt/codex-app-server 存在。health 与 pg_isready 都不需要密码;
|
||||
需要密码的 SELECT 1 那一步直接跳过,不在脚本里读密码。
|
||||
|
||||
`--self-test` 只验证脚本本身的机制(health JSON 解析 + 输出是合法 JSON):
|
||||
它起一个本地 canned health server,断言 health 那一项为 pass;其它项在
|
||||
非主机机器上 fail 是正常的,self-test 不因此失败。不要把 --self-test
|
||||
通过误读成“主机健康”。
|
||||
|
||||
定时(主机上安装,示例):
|
||||
|
||||
```sh
|
||||
cp /opt/codex-app-server/deploy/monitor.service /etc/systemd/system/monitor.service
|
||||
cp /opt/codex-app-server/deploy/monitor.timer /etc/systemd/system/monitor.timer
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now monitor.timer
|
||||
journalctl -u monitor.service -f # 看每次检查的输出
|
||||
```
|
||||
|
||||
timer 每 5 分钟跑一次,日志进 journal。
|
||||
|
||||
### 监控边界:本轮只有“主机侧检查 + journal 日志”
|
||||
|
||||
monitor.sh 本身不会发消息给用户。推送到 Muse 聊天的告警仍需用户另行
|
||||
决定通道(例如 SSH key 常驻通道让外部 cron 拉 journal,或者一个
|
||||
webhook URL 让 monitor 失败时 POST 过去),在通道定下来之前,不要假装
|
||||
monitor.sh 会通知任何人。手工验证步骤:主机上跑一次 monitor.sh 看 JSON
|
||||
与退出码,再跑一次 --self-test 看 SELF-TEST PASS,最后
|
||||
`systemctl list-timers | grep monitor` 确认 timer 已启用。
|
||||
|
||||
## worker 心跳与 health 的 worker 字段
|
||||
|
||||
worker 的 `run_loop` 每次循环结束时写心跳文件,内容只有
|
||||
`{"last_seen": epoch秒, "pid": 进程号}`,不含任何密钥;写失败只记日志,
|
||||
不炸 worker。心跳文件路径:worker 用 `--heartbeat-file` 指定(主机部署
|
||||
时指到 `/var/lib/codex-app-server/worker-heartbeat.json`),未指定时看
|
||||
环境变量 `CLOUD_WORKER_HEARTBEAT_FILE`,再未指定则为
|
||||
`./worker-heartbeat.json`(VM 本地跑的默认,不在代码里硬编码主机路径)。
|
||||
|
||||
server 的 `GET /health` 在原有 ok/service/backend/db_ok 之外加两个字段:
|
||||
`worker_last_seen_seconds_ago`(心跳文件不存在或读失败时为 None)与
|
||||
`worker_ok`(心跳在 180 秒内为 true,否则 false;阈值可经环境变量
|
||||
`CLOUD_WORKER_HEARTBEAT_MAX_AGE_SECONDS` 调,默认 180)。health 本身仍
|
||||
返回 200——worker 不 ok 不代表 server 挂了,调用方看 worker_ok 字段判断,
|
||||
不要把 health 改成 503 破坏已有调用。
|
||||
|
||||
主机部署时两边路径必须一致:worker 单元加
|
||||
`--heartbeat-file /var/lib/codex-app-server/worker-heartbeat.json`,同时
|
||||
在 server 的 env 文件(`/etc/codex-app-server/env`,server 单元的
|
||||
EnvironmentFile)里加一行
|
||||
`CLOUD_WORKER_HEARTBEAT_FILE=/var/lib/codex-app-server/worker-heartbeat.json`。
|
||||
deploy.sh 不会自动改 env 文件(那是 root-only 的密钥文件),这一行由
|
||||
部署操作人在主机上手动加,加完重启 server 即可。
|
||||
|
||||
## 多用户真实验证:verify-multiuser.py
|
||||
|
||||
主机侧以 codexsvc 身份运行,直接对生产库 + 本地 HTTP API 验证:
|
||||
|
||||
```sh
|
||||
python3 /opt/codex-app-server/deploy/verify-multiuser.py
|
||||
# 可选:--base http://127.0.0.1:8765 --db /var/lib/codex-app-server/control.db
|
||||
```
|
||||
|
||||
它在内存里创建 4 个测试用户(owner / maintainer / contributor / viewer)
|
||||
与 2 个测试项目(P2 只有 owner 是成员,用于隔离验证),再用 HTTP API
|
||||
验证:owner/maintainer/contributor/viewer 的建任务、读任务、merge 权限,
|
||||
以及跨项目隔离(P1 成员读 P2 任务 403)、无 key 401。merge 验证用的
|
||||
completed 测试任务是直接在 DB 里把测试任务置 completed,不跑真实模型。
|
||||
|
||||
防误伤要点(脚本逻辑里已实现):
|
||||
- 测试用户、项目、任务全部用 `multiuser-test-` 前缀,清理时只删这些
|
||||
前缀的行,绝不碰其它生产数据;清理失败时退出非零并列出残留测试 id
|
||||
(不含 key),不静默假装干净。
|
||||
- 测试任务创建时都带一个永不存在的依赖 `multiuser-test-never-completes`:
|
||||
worker 只派依赖全部 completed 的 queued 任务,所以这些测试任务永远
|
||||
不会被生产 worker 认领去跑模型,不花模型钱、不改任何仓库。
|
||||
- API key 只存在内存里,脚本全程只打印每项 PASS/FAIL 与总数,绝不打印
|
||||
任何 API key;任务 prompt 与用户 id 也不打印,只打印检查名与结果。
|
||||
- 审计日志是 append-only 的:验证过程的审计条目会留在 audit_log 里,
|
||||
脚本不删审计表——审计就该留痕,这一点不是清理遗漏。
|
||||
|
||||
## 公网入口与 webhook secret(已有文件,不属于本轮新增)
|
||||
|
||||
- `caddy/Caddyfile`:只暴露 `/webhooks/*` 与 `/oauth/*`,任务/项目/管理
|
||||
API 对外一律 403;域名与邮箱占位符需部署时替换。
|
||||
- `setup-webhook-secret.sh`:在主机上生成 webhook secret 并写入 env 文件,
|
||||
绝不打印 secret;已设则不动,避免轮换后 GitHub 端旧 secret 失效。
|
||||
|
||||
## 仍未做(不要假装已做)
|
||||
|
||||
- 推送告警通道:monitor 失败时通知到 Muse 聊天的通道还没定(见监控一节)。
|
||||
- 回滚不回数据库:见回滚边界一节;改类型/删列的迁移需要单独评估。
|
||||
- 恢复演练不碰生产库:演练只比行数,不比对具体内容;恢复到生产库的
|
||||
真实灾难恢复流程(停服务、恢复、重启、验 health)还没演练过。
|
||||
- CloudExecutor、GUI、Slack 执行、推送实发:与本轮无关,仍是 0 或占位。
|
||||
Executable
+158
@@ -0,0 +1,158 @@
|
||||
#!/bin/sh
|
||||
# 部署自动化:VM 侧运行,把本地代码同步到外部主机并重启验证,失败自动回滚。
|
||||
#
|
||||
# 用法:
|
||||
# deploy/deploy.sh <host> [user] [--dry-run]
|
||||
# host:主机 IP 或域名(必填);user:SSH 用户(默认 root)。
|
||||
# --dry-run:只打印将要做的步骤,不 scp、不 ssh、不改主机。
|
||||
#
|
||||
# 密码:本脚本绝不接受密码参数、不读任何 env 文件、不打印任何密钥。
|
||||
# SSH 密码由 ssh/scp 交互提示输入,或由调用方自己的 SSH 通道提供
|
||||
# (例如已配好的 key)。需要经代理时,用环境变量 SSH_PROXY_CMD
|
||||
# 传入 ProxyCommand 内容(ssh 会自行展开 %h %p),未设则直连:
|
||||
# SSH_PROXY_CMD="python3 /tmp/proxy_tunnel.py %h %p" deploy/deploy.sh <host>
|
||||
#
|
||||
# 步骤:
|
||||
# 1. 本机打代码 tar 包(排除 bin/、work/、.git、__pycache__、state、
|
||||
# host-config-real.json,与之前手工同步的排除规则一致)。
|
||||
# 2. scp 到主机 /tmp/codex-app-server-code.tar.gz。
|
||||
# 3. ssh 到主机:先把当前 /opt/codex-app-server 打备份包到
|
||||
# /opt/codex-app-server-backups/(时间戳命名,只留最近 3 份,
|
||||
# 备份排除 work/ 目录以免过大);解包新代码并 chown codexsvc;
|
||||
# 重启 server+worker;循环 curl localhost:8765/health 最多 60 秒,
|
||||
# 要求 ok=true 且 backend=postgres 且 db_ok=true。
|
||||
# 4. 健康检查失败时自动恢复刚打的备份包、重启、再验 health;回滚后
|
||||
# health 恢复也算部署失败(退出非零),因为新代码没上去;回滚后
|
||||
# health 仍失败则退出非零并明说“已回滚但 health 仍失败,需人工介入”。
|
||||
# 5. 成功时只打印新代码的 git commit(从本机 git 取,不编造版本号)。
|
||||
#
|
||||
# 回滚边界:回滚只回代码,不回数据库。数据库迁移是只加列的幂等操作
|
||||
# (projects.repo_url、tasks 的 6 列等),回滚代码后老代码仍能读新列,
|
||||
# 所以只回代码是安全的;数据库本身绝不会被本脚本动到。
|
||||
set -eu
|
||||
|
||||
HOST=${1:-}
|
||||
USER_ARG=${2:-root}
|
||||
DRY_RUN=0
|
||||
for a in "$@"; do
|
||||
if [ "$a" = "--dry-run" ]; then DRY_RUN=1; fi
|
||||
done
|
||||
if [ -z "$HOST" ] || [ "$HOST" = "--dry-run" ]; then
|
||||
echo "用法:deploy/deploy.sh <host> [user] [--dry-run]" >&2
|
||||
exit 2
|
||||
fi
|
||||
# user 位置可能是 --dry-run,处理一下
|
||||
if [ "$USER_ARG" = "--dry-run" ]; then USER_ARG=root; fi
|
||||
|
||||
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
REPO_ROOT=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
|
||||
cd "$REPO_ROOT"
|
||||
|
||||
COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "unknown")
|
||||
TARBALL="$REPO_ROOT/work/external-install/codex-app-server-code.tar.gz"
|
||||
REMOTE_TARBALL="/tmp/codex-app-server-code.tar.gz"
|
||||
|
||||
# ssh/scp 命令构造:有 SSH_PROXY_CMD 时走代理,否则直连。
|
||||
ssh_cmd() {
|
||||
if [ -n "${SSH_PROXY_CMD:-}" ]; then
|
||||
ssh -o "ProxyCommand $SSH_PROXY_CMD" -o StrictHostKeyChecking=no \
|
||||
-o UserKnownHostsFile=/dev/null "$@"
|
||||
else
|
||||
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
|
||||
fi
|
||||
}
|
||||
scp_cmd() {
|
||||
if [ -n "${SSH_PROXY_CMD:-}" ]; then
|
||||
scp -o "ProxyCommand $SSH_PROXY_CMD" -o StrictHostKeyChecking=no \
|
||||
-o UserKnownHostsFile=/dev/null "$@"
|
||||
else
|
||||
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
echo "部署目标:$USER_ARG@$HOST,代码 commit:$COMMIT"
|
||||
if [ "$DRY_RUN" = "1" ]; then
|
||||
echo "[dry-run] 将打 tar 包:$TARBALL(排除 bin/ work/ .git __pycache__ state host-config-real.json)"
|
||||
echo "[dry-run] 将 scp 到 $USER_ARG@$HOST:$REMOTE_TARBALL"
|
||||
echo "[dry-run] 将在主机上备份 /opt/codex-app-server 到 /opt/codex-app-server-backups/(留 3 份),解包、chown codexsvc、重启 server+worker、验 health 60 秒,失败自动回滚"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
mkdir -p "$REPO_ROOT/work/external-install"
|
||||
tar --exclude='./bin' --exclude='./work' --exclude='./.git' \
|
||||
--exclude='__pycache__' --exclude='*.pyc' \
|
||||
--exclude='host-state*.json*' --exclude='host-daemon.*' \
|
||||
--exclude='host-config-real.json' --exclude='host-run.log' \
|
||||
-czf "$TARBALL" .
|
||||
echo "tar 包已生成:$TARBALL"
|
||||
|
||||
scp_cmd "$TARBALL" "$USER_ARG@$HOST:$REMOTE_TARBALL"
|
||||
echo "tar 包已传到主机"
|
||||
|
||||
# 主机侧部署脚本:备份 -> 解包 -> 重启 -> 验 health -> 失败回滚。
|
||||
# 注意:这里用单引号 heredoc 传过去,主机侧变量不被本机展开;
|
||||
# 健康判断用 python3 解析 JSON,不依赖 jq(主机未必装 jq)。
|
||||
ssh_cmd "$USER_ARG@$HOST" 'sh -s' <<'HOST_SCRIPT'
|
||||
set -eu
|
||||
APP_DIR=/opt/codex-app-server
|
||||
BACKUP_DIR=/opt/codex-app-server-backups
|
||||
STAMP=$(date +%Y%m%d%H%M%S)
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
BACKUP_FILE="$BACKUP_DIR/codex-app-server-$STAMP.tar.gz"
|
||||
|
||||
check_health() {
|
||||
# 要求 ok=true 且 backend=postgres 且 db_ok=true,满足返回 0。
|
||||
OUT=$(curl -s --max-time 5 http://127.0.0.1:8765/health || true)
|
||||
[ -n "$OUT" ] && printf '%s' "$OUT" | python3 -c '
|
||||
import json, sys
|
||||
try:
|
||||
d = json.load(sys.stdin)
|
||||
except Exception:
|
||||
sys.exit(1)
|
||||
sys.exit(0 if (d.get("ok") is True and d.get("backend") == "postgres"
|
||||
and d.get("db_ok") is True) else 1)
|
||||
'
|
||||
}
|
||||
|
||||
wait_health() {
|
||||
i=0
|
||||
while [ "$i" -lt 12 ]; do
|
||||
if check_health; then return 0; fi
|
||||
sleep 5
|
||||
i=$((i + 1))
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# 1. 备份当前代码(排除 work/,只留最近 3 份)
|
||||
tar --exclude='./work' -czf "$BACKUP_FILE" -C /opt codex-app-server
|
||||
echo "已备份当前代码到:$BACKUP_FILE"
|
||||
ls -t "$BACKUP_DIR"/codex-app-server-*.tar.gz 2>/dev/null \
|
||||
| tail -n +4 | xargs -r rm -f
|
||||
|
||||
# 2. 解包新代码并改属主
|
||||
tar -xzf /tmp/codex-app-server-code.tar.gz -C "$APP_DIR"
|
||||
chown -R codexsvc:codexsvc "$APP_DIR"
|
||||
|
||||
# 3. 重启并验 health
|
||||
systemctl restart codex-cloud-server codex-cloud-worker
|
||||
if wait_health; then
|
||||
echo "部署成功,health 通过"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 4. 失败:恢复备份并重启,再验一次
|
||||
echo "新代码 health 未通过,开始回滚到备份:$BACKUP_FILE"
|
||||
systemctl stop codex-cloud-worker codex-cloud-server || true
|
||||
tar -xzf "$BACKUP_FILE" -C /opt
|
||||
chown -R codexsvc:codexsvc "$APP_DIR"
|
||||
systemctl start codex-cloud-server codex-cloud-worker
|
||||
if wait_health; then
|
||||
echo "部署失败,已回滚到备份,health 已恢复(新代码未上线)" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "已回滚但 health 仍失败,需人工介入" >&2
|
||||
exit 1
|
||||
HOST_SCRIPT
|
||||
|
||||
echo "部署完成,代码 commit:$COMMIT"
|
||||
@@ -0,0 +1,14 @@
|
||||
[Unit]
|
||||
Description=Codex cloud monitor check (one-shot)
|
||||
After=network.target codex-cloud-server.service codex-cloud-worker.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
User=codexsvc
|
||||
WorkingDirectory=/opt/codex-app-server
|
||||
# monitor.sh 只做检查、不读 env 文件、不发消息;失败时 systemd 会把
|
||||
# 退出码与输出记进 journal,告警通道见 deploy/README.md(另行决定)。
|
||||
ExecStart=/bin/sh /opt/codex-app-server/deploy/monitor.sh
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Executable
+171
@@ -0,0 +1,171 @@
|
||||
#!/bin/sh
|
||||
# 主机侧监控检查:输出一行 JSON 状态,任一项 fail 则退出非零。
|
||||
#
|
||||
# 用法(主机上,codexsvc 可读权限即可):
|
||||
# sh /opt/codex-app-server/deploy/monitor.sh
|
||||
# sh /opt/codex-app-server/deploy/monitor.sh --self-test
|
||||
#
|
||||
# 检查项(全部不需要密码、不读 env 文件):
|
||||
# server:systemctl is-active codex-cloud-server 为 active
|
||||
# worker:systemctl is-active codex-cloud-worker 为 active
|
||||
# health:curl health,要求 ok=true、backend=postgres、db_ok=true、
|
||||
# worker_ok=true(worker 心跳 180 秒内,见 server.py)
|
||||
# postgres:pg_isready -h 127.0.0.1 -p 5432(只测就绪,不连库查数据,
|
||||
# 所以不需要密码;需要密码的 SELECT 1 那一步直接跳过,不做)
|
||||
# docker_postgres:docker ps 里 codex-postgres 在运行
|
||||
# disk:磁盘使用率 < 90%(默认看 /var/lib/codex-app-server,不存在则看 /)
|
||||
# app_dir:/opt/codex-app-server 目录存在
|
||||
#
|
||||
# 输出示例(单行 JSON,值只会是 pass/fail,不含任何密钥):
|
||||
# {"overall": "pass", "checks": {"server": "pass", ...}}
|
||||
#
|
||||
# --self-test:只验证脚本本身的机制(health JSON 解析 + 输出是合法 JSON),
|
||||
# 不验证主机健康。它会起一个本地 canned health server(只返回固定健康
|
||||
# JSON),断言 health 那一项为 pass;其它项在非主机机器上 fail 是正常的,
|
||||
# self-test 不因此失败。不要把 --self-test 通过误读成“主机健康”。
|
||||
#
|
||||
# 告警边界:本脚本只做“检查 + journal 日志”(配合 monitor.timer 每 5 分钟
|
||||
# 跑一次),它本身不会发消息给用户。推送到 Muse 聊天的告警仍需用户另行
|
||||
# 决定通道(SSH key 常驻通道或 webhook URL),见 deploy/README.md。
|
||||
set -eu
|
||||
|
||||
HEALTH_URL=${MONITOR_HEALTH_URL:-http://127.0.0.1:8765/health}
|
||||
APP_DIR=${MONITOR_APP_DIR:-/opt/codex-app-server}
|
||||
DISK_PATH=${MONITOR_DISK_PATH:-/var/lib/codex-app-server}
|
||||
if [ ! -d "$DISK_PATH" ]; then DISK_PATH="/"; fi
|
||||
|
||||
check_server() {
|
||||
if systemctl is-active codex-cloud-server >/dev/null 2>&1; then
|
||||
echo pass
|
||||
else
|
||||
echo fail
|
||||
fi
|
||||
}
|
||||
|
||||
check_worker() {
|
||||
if systemctl is-active codex-cloud-worker >/dev/null 2>&1; then
|
||||
echo pass
|
||||
else
|
||||
echo fail
|
||||
fi
|
||||
}
|
||||
|
||||
check_health() {
|
||||
OUT=$(curl -s --max-time 5 "$HEALTH_URL" || true)
|
||||
if [ -z "$OUT" ]; then echo fail; return; fi
|
||||
if printf '%s' "$OUT" | HEALTH_JSON="$OUT" python3 -c '
|
||||
import json, os, sys
|
||||
try:
|
||||
d = json.loads(os.environ["HEALTH_JSON"])
|
||||
except Exception:
|
||||
sys.exit(1)
|
||||
sys.exit(0 if (d.get("ok") is True and d.get("backend") == "postgres"
|
||||
and d.get("db_ok") is True
|
||||
and d.get("worker_ok") is True) else 1)
|
||||
' 2>/dev/null; then
|
||||
echo pass
|
||||
else
|
||||
echo fail
|
||||
fi
|
||||
}
|
||||
|
||||
check_postgres() {
|
||||
if command -v pg_isready >/dev/null 2>&1 \
|
||||
&& pg_isready -h 127.0.0.1 -p 5432 >/dev/null 2>&1; then
|
||||
echo pass
|
||||
else
|
||||
echo fail
|
||||
fi
|
||||
}
|
||||
|
||||
check_docker_postgres() {
|
||||
if command -v docker >/dev/null 2>&1 \
|
||||
&& docker ps --format '{{.Names}}' 2>/dev/null \
|
||||
| grep -qx 'codex-postgres'; then
|
||||
echo pass
|
||||
else
|
||||
echo fail
|
||||
fi
|
||||
}
|
||||
|
||||
check_disk() {
|
||||
USE=$(df -P "$DISK_PATH" 2>/dev/null | awk 'NR==2 {print $5}' \
|
||||
| tr -d '% ' || true)
|
||||
case "$USE" in
|
||||
''|*[!0-9]*) echo fail ;;
|
||||
*) if [ "$USE" -lt 90 ]; then echo pass; else echo fail; fi ;;
|
||||
esac
|
||||
}
|
||||
|
||||
check_app_dir() {
|
||||
if [ -d "$APP_DIR" ]; then echo pass; else echo fail; fi
|
||||
}
|
||||
|
||||
run_checks() {
|
||||
S=$(check_server); W=$(check_worker); H=$(check_health)
|
||||
P=$(check_postgres); D=$(check_docker_postgres)
|
||||
K=$(check_disk); A=$(check_app_dir)
|
||||
OVERALL=pass
|
||||
for v in "$S" "$W" "$H" "$P" "$D" "$K" "$A"; do
|
||||
if [ "$v" != "pass" ]; then OVERALL=fail; break; fi
|
||||
done
|
||||
printf '{"overall": "%s", "checks": {"server": "%s", "worker": "%s", "health": "%s", "postgres": "%s", "docker_postgres": "%s", "disk": "%s", "app_dir": "%s"}}\n' \
|
||||
"$OVERALL" "$S" "$W" "$H" "$P" "$D" "$K" "$A"
|
||||
if [ "$OVERALL" = "pass" ]; then return 0; else return 1; fi
|
||||
}
|
||||
|
||||
if [ "${1:-}" = "--self-test" ]; then
|
||||
# 起一个 canned health server:只返回固定健康 JSON,不读任何真实服务。
|
||||
SELF_PORT=18999
|
||||
python3 - "$SELF_PORT" <<'PY' &
|
||||
import http.server
|
||||
import json
|
||||
import sys
|
||||
|
||||
|
||||
class H(http.server.BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
body = json.dumps({
|
||||
"ok": True, "service": "cloud-control-plane",
|
||||
"backend": "postgres", "db_ok": True,
|
||||
"worker_last_seen_seconds_ago": 1.0, "worker_ok": True,
|
||||
}).encode()
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
|
||||
http.server.HTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()
|
||||
PY
|
||||
SERVER_PID=$!
|
||||
trap 'kill $SERVER_PID 2>/dev/null || true' EXIT
|
||||
sleep 1
|
||||
MONITOR_HEALTH_URL="http://127.0.0.1:$SELF_PORT/health" \
|
||||
HEALTH_URL="http://127.0.0.1:$SELF_PORT/health" \
|
||||
sh "$0" > /tmp/monitor-self-test.json 2>/dev/null || true
|
||||
# 注意:上面那行是跑完整检查,health 以外项在非主机上 fail 是正常的;
|
||||
# self-test 只断言两件事:输出是合法 JSON,且 health 解析为 pass。
|
||||
if python3 -c '
|
||||
import json
|
||||
import sys
|
||||
with open("/tmp/monitor-self-test.json") as f:
|
||||
d = json.load(f)
|
||||
sys.exit(0 if d["checks"]["health"] == "pass" else 1)
|
||||
'; then
|
||||
echo "SELF-TEST PASS:输出是合法 JSON 且 health 解析为 pass(其它项在非主机上 fail 属正常,不影响 self-test)"
|
||||
cat /tmp/monitor-self-test.json
|
||||
exit 0
|
||||
else
|
||||
echo "SELF-TEST FAIL:health 解析未通过或输出不是合法 JSON" >&2
|
||||
cat /tmp/monitor-self-test.json >&2 || true
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
run_checks
|
||||
@@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=Run Codex cloud monitor check every 5 minutes
|
||||
|
||||
[Timer]
|
||||
# 每 5 分钟跑一次 monitor.sh;Persistent=true 表示主机停机错过的那次
|
||||
# 会在下次开机时补跑一次,不会静默丢检查。
|
||||
OnCalendar=*:0/5
|
||||
Persistent=true
|
||||
Unit=monitor.service
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
Executable
+106
@@ -0,0 +1,106 @@
|
||||
#!/bin/sh
|
||||
# 备份恢复演练:主机侧运行(root,或能读 /etc/codex-app-server/env 的用户)。
|
||||
#
|
||||
# 用法(主机上):sh /opt/codex-app-server/deploy/restore-drill.sh
|
||||
# 可选环境变量:BACKUP_DIR(默认 /var/lib/codex-app-server/backups)。
|
||||
#
|
||||
# 步骤:
|
||||
# 1. 跑一次 backup_postgres 生成新鲜备份(.sql)。
|
||||
# 2. 新建一个测试库 codex_control_restore_test_<时间戳>,用
|
||||
# cloud.backup.restore_postgres 把备份恢复进去。
|
||||
# 3. 比对生产库与测试库的 tasks / projects / users / audit_log 行数,
|
||||
# 行数相等即通过;只比行数,不比对具体内容,不打印任何行内容。
|
||||
# 4. 比对完立即删掉测试库(trap 保证即使中途失败也删)。
|
||||
#
|
||||
# 安全红线(写进脚本逻辑里,不是只写在注释里):
|
||||
# - 恢复目标永远是新生成的测试库名,绝不等于生产库名;restore_postgres
|
||||
# 本身也会拒绝目标库名等于 CLOUD_DB_NAME(allow_production 绝不在
|
||||
# 本脚本里设为 True),双重保险。
|
||||
# - 本脚本绝不恢复到生产库,绝不删生产库;删的只会是本次生成的测试库。
|
||||
# - 全程只打印 PASS/FAIL 与行数,不打印密码,不打印备份内容。
|
||||
# - 本脚本以 root 身份读 env 文件是为了拿到数据库连接参数与密码,
|
||||
# 密码只经 PGPASSWORD 环境变量传给 psql/python 子进程,绝不出现在
|
||||
# 命令行参数里,也绝不被 echo 出来。
|
||||
set -eu
|
||||
|
||||
APP_DIR=${APP_DIR:-/opt/codex-app-server}
|
||||
BACKUP_DIR=${BACKUP_DIR:-/var/lib/codex-app-server/backups}
|
||||
ENV_FILE=${ENV_FILE:-/etc/codex-app-server/env}
|
||||
|
||||
if [ ! -f "$ENV_FILE" ]; then
|
||||
echo "FAIL:找不到 env 文件:$ENV_FILE" >&2
|
||||
exit 1
|
||||
fi
|
||||
# shellcheck disable=SC1090
|
||||
set -a
|
||||
. "$ENV_FILE"
|
||||
set +a
|
||||
if [ -z "${CLOUD_DB_NAME:-}" ] || [ -z "${CLOUD_DB_USER:-}" ]; then
|
||||
echo "FAIL:env 文件里缺少 CLOUD_DB_NAME 或 CLOUD_DB_USER" >&2
|
||||
exit 1
|
||||
fi
|
||||
export PGPASSWORD="${CLOUD_DB_PASSWORD:-}"
|
||||
HOST_ARG=${CLOUD_DB_HOST:-127.0.0.1}
|
||||
PORT_ARG=${CLOUD_DB_PORT:-5432}
|
||||
|
||||
STAMP=$(date +%Y%m%d%H%M%S)
|
||||
TEST_DB="codex_control_restore_test_$STAMP"
|
||||
|
||||
drop_test_db() {
|
||||
# trap 保证:无论演练成功失败,测试库都会被删掉,不留垃圾库。
|
||||
psql -h "$HOST_ARG" -p "$PORT_ARG" -U "$CLOUD_DB_USER" \
|
||||
-d postgres -c "DROP DATABASE IF EXISTS \"$TEST_DB\"" >/dev/null 2>&1 || true
|
||||
}
|
||||
trap drop_test_db EXIT
|
||||
|
||||
# 1. 新鲜备份(backup_postgres 的连接参数与密码都走环境变量,
|
||||
# 密码只经 PGPASSWORD 传给子进程,绝不出现在命令行或输出里)。
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
BACKUP_FILE=$(python3 - "$APP_DIR" "$BACKUP_DIR" <<'PY'
|
||||
import sys
|
||||
sys.path.insert(0, sys.argv[1])
|
||||
from cloud.backup import backup_postgres
|
||||
print(backup_postgres(sys.argv[2]))
|
||||
PY
|
||||
)
|
||||
if [ ! -f "$BACKUP_FILE" ]; then
|
||||
echo "FAIL:备份生成失败" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "备份已生成(只打印路径,不打印内容):$BACKUP_FILE"
|
||||
|
||||
# 2. 建测试库并恢复
|
||||
psql -h "$HOST_ARG" -p "$PORT_ARG" -U "$CLOUD_DB_USER" \
|
||||
-d postgres -c "CREATE DATABASE \"$TEST_DB\"" >/dev/null
|
||||
python3 - "$APP_DIR" "$BACKUP_FILE" "$TEST_DB" <<'PY'
|
||||
import sys
|
||||
sys.path.insert(0, sys.argv[1])
|
||||
from cloud.backup import restore_postgres
|
||||
# allow_production 故意不传(默认 False):目标库名绝不能等于生产库名。
|
||||
print(restore_postgres(sys.argv[2], sys.argv[3]))
|
||||
PY
|
||||
echo "恢复完成,目标测试库:$TEST_DB"
|
||||
|
||||
# 3. 行数比对(只比行数,不打印行内容)
|
||||
FAIL=0
|
||||
for TABLE in tasks projects users audit_log; do
|
||||
PROD_N=$(psql -h "$HOST_ARG" -p "$PORT_ARG" -U "$CLOUD_DB_USER" \
|
||||
-d "$CLOUD_DB_NAME" -t -A -c "SELECT COUNT(*) FROM $TABLE" | tr -d ' \n')
|
||||
REST_N=$(psql -h "$HOST_ARG" -p "$PORT_ARG" -U "$CLOUD_DB_USER" \
|
||||
-d "$TEST_DB" -t -A -c "SELECT COUNT(*) FROM $TABLE" | tr -d ' \n')
|
||||
if [ "$PROD_N" = "$REST_N" ]; then
|
||||
echo "PASS $TABLE:生产库=$PROD_N 恢复库=$REST_N"
|
||||
else
|
||||
echo "FAIL $TABLE:生产库=$PROD_N 恢复库=$REST_N" >&2
|
||||
FAIL=1
|
||||
fi
|
||||
done
|
||||
|
||||
# 4. 删测试库由 trap 执行;这里先显式删一次,trap 再删是空操作。
|
||||
drop_test_db
|
||||
trap - EXIT
|
||||
if [ "$FAIL" = "1" ]; then
|
||||
echo "恢复演练 FAIL:有表行数不一致" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "恢复演练 PASS:全部 4 张表行数一致,测试库已删除"
|
||||
Executable
+79
@@ -0,0 +1,79 @@
|
||||
#!/bin/sh
|
||||
# 回滚:VM 侧运行,把主机恢复到最近一份代码备份包并重启验证。
|
||||
#
|
||||
# 用法:deploy/rollback.sh <host> [user]
|
||||
# host:主机 IP 或域名(必填);user:SSH 用户(默认 root)。
|
||||
#
|
||||
# 密码:与 deploy.sh 同款规则——绝不接受密码参数、不读 env 文件、
|
||||
# 不打印任何密钥;SSH 密码由交互提示或调用方通道提供;需要代理时
|
||||
# 用环境变量 SSH_PROXY_CMD 传入 ProxyCommand,未设则直连。
|
||||
#
|
||||
# 回滚边界:只回代码,不回数据库(理由见 deploy.sh 头注释与
|
||||
# deploy/README.md:数据库迁移是只加列的幂等操作,老代码仍能读新列)。
|
||||
# 备份包来自 deploy.sh 每次部署前在主机上打的
|
||||
# /opt/codex-app-server-backups/codex-app-server-<时间戳>.tar.gz,
|
||||
# 本脚本恢复其中最新的一份;主机上没有备份包时直接报错退出,不编造。
|
||||
set -eu
|
||||
|
||||
HOST=${1:-}
|
||||
USER_ARG=${2:-root}
|
||||
if [ -z "$HOST" ]; then
|
||||
echo "用法:deploy/rollback.sh <host> [user]" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
ssh_cmd() {
|
||||
if [ -n "${SSH_PROXY_CMD:-}" ]; then
|
||||
ssh -o "ProxyCommand $SSH_PROXY_CMD" -o StrictHostKeyChecking=no \
|
||||
-o UserKnownHostsFile=/dev/null "$@"
|
||||
else
|
||||
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
ssh_cmd "$USER_ARG@$HOST" 'sh -s' <<'HOST_SCRIPT'
|
||||
set -eu
|
||||
APP_DIR=/opt/codex-app-server
|
||||
BACKUP_DIR=/opt/codex-app-server-backups
|
||||
|
||||
check_health() {
|
||||
OUT=$(curl -s --max-time 5 http://127.0.0.1:8765/health || true)
|
||||
[ -n "$OUT" ] && printf '%s' "$OUT" | python3 -c '
|
||||
import json, sys
|
||||
try:
|
||||
d = json.load(sys.stdin)
|
||||
except Exception:
|
||||
sys.exit(1)
|
||||
sys.exit(0 if (d.get("ok") is True and d.get("backend") == "postgres"
|
||||
and d.get("db_ok") is True) else 1)
|
||||
'
|
||||
}
|
||||
|
||||
wait_health() {
|
||||
i=0
|
||||
while [ "$i" -lt 12 ]; do
|
||||
if check_health; then return 0; fi
|
||||
sleep 5
|
||||
i=$((i + 1))
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
BACKUP_FILE=$(ls -t "$BACKUP_DIR"/codex-app-server-*.tar.gz 2>/dev/null \
|
||||
| head -n 1 || true)
|
||||
if [ -z "$BACKUP_FILE" ]; then
|
||||
echo "主机上没有代码备份包,无法回滚:$BACKUP_DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "将恢复备份包:$BACKUP_FILE"
|
||||
systemctl stop codex-cloud-worker codex-cloud-server || true
|
||||
tar -xzf "$BACKUP_FILE" -C /opt
|
||||
chown -R codexsvc:codexsvc "$APP_DIR"
|
||||
systemctl start codex-cloud-server codex-cloud-worker
|
||||
if wait_health; then
|
||||
echo "回滚成功,health 通过,当前代码为备份包:$BACKUP_FILE"
|
||||
exit 0
|
||||
fi
|
||||
echo "回滚后 health 仍失败,需人工介入" >&2
|
||||
exit 1
|
||||
HOST_SCRIPT
|
||||
Executable
+260
@@ -0,0 +1,260 @@
|
||||
#!/usr/bin/env python3
|
||||
"""多用户真实验证:主机侧以 codexsvc 身份运行,直接对生产库 + 本地 HTTP API 验证。
|
||||
|
||||
用法(主机上):
|
||||
python3 /opt/codex-app-server/deploy/verify-multiuser.py [--base URL] [--db PATH]
|
||||
默认 --base http://127.0.0.1:8765,--db /var/lib/codex-app-server/control.db
|
||||
(Postgres 后端时 db 参数只作占位,连接参数走 CLOUD_DB_* 环境变量,
|
||||
与 server/worker 一致;SQLite 后端时就是库文件路径)。
|
||||
|
||||
安全与防误伤(写进逻辑里,不是只写在注释里):
|
||||
- 测试用户、项目、任务全部用 multiuser-test- 前缀(项目 id 固定为
|
||||
multiuser-test-p1 / multiuser-test-p2,用户按用户名 multiuser-test-*
|
||||
查找),清理时只删这些前缀的行,绝不碰其它生产数据。
|
||||
- 测试任务创建时都带一个永不存在的依赖 multiuser-test-never-completes:
|
||||
worker 只派依赖全部 completed 的 queued 任务(见 cloud/queue.py 的
|
||||
ready_tasks),所以这些测试任务永远不会被生产 worker 认领去跑模型——
|
||||
不花模型钱、不改任何仓库。验证完任务会被删掉,依赖本身只是一个
|
||||
字符串,不对应任何真实任务行。
|
||||
- API key 只存在内存里:创建用户时 AuthStore 返回的明文 key 只保存在
|
||||
本进程的变量里,用完即弃;脚本全程只打印每项 PASS/FAIL 与总数,
|
||||
绝不打印任何 API key;任务 prompt、用户 id 等内容也不打印,只打印
|
||||
检查名与结果。
|
||||
- 审计日志是 append-only 的:验证过程产生的审计条目(建项目、设成员、
|
||||
建任务、merge 等)会留在 audit_log 里,脚本不删审计表——这是故意的,
|
||||
审计就该留痕;README 里也写清了这一点。
|
||||
- 清理失败时脚本退出非零并列出残留测试 id(不含 key),不静默假装干净。
|
||||
- 本脚本不跑真实模型、不推代码、不改生产项目与任务,只动 multiuser-test-*
|
||||
的行;跑之前不需要停 worker(见上,测试任务不会被认领)。
|
||||
|
||||
检查项(预期值写死在断言里,实际不符即 FAIL):
|
||||
1. owner 建 P1、P2 成功(201)
|
||||
2. owner 建任务成功(201,contributor 建任务也成功)
|
||||
3. viewer 建任务 403;非成员(viewer 对 P2)建任务 403
|
||||
4. viewer 读 P1 任务成功(200)
|
||||
5. viewer 读 P2 任务 403(跨项目隔离:P1 成员不是 P2 成员)
|
||||
6. maintainer 对 completed 测试任务调 merge 成功(200,状态变 merged)
|
||||
7. contributor 对 completed 测试任务调 merge 403
|
||||
8. maintainer 对 queued 测试任务调 merge 409(状态不对)
|
||||
9. 无 key 调 API 401
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from cloud.auth import AuthStore # noqa: E402
|
||||
from cloud.db import connect as db_connect # noqa: E402
|
||||
|
||||
PREFIX = "multiuser-test-"
|
||||
P1 = PREFIX + "p1"
|
||||
P2 = PREFIX + "p2"
|
||||
NEVER_DEP = PREFIX + "never-completes"
|
||||
|
||||
RESULTS = []
|
||||
|
||||
|
||||
def check(name, ok):
|
||||
RESULTS.append((name, bool(ok)))
|
||||
print("%s %s" % ("PASS" if ok else "FAIL", name))
|
||||
|
||||
|
||||
def req(base, method, path, key=None, body=None):
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
r = urllib.request.Request(base + path, data=data, method=method)
|
||||
if key is not None:
|
||||
r.add_header("X-API-Key", key)
|
||||
if body is not None:
|
||||
r.add_header("Content-Type", "application/json")
|
||||
try:
|
||||
with urllib.request.urlopen(r, timeout=15) as resp:
|
||||
raw = resp.read()
|
||||
return resp.status, (json.loads(raw) if raw else {})
|
||||
except urllib.error.HTTPError as e:
|
||||
raw = e.read()
|
||||
try:
|
||||
payload = json.loads(raw) if raw else {}
|
||||
except ValueError:
|
||||
payload = {}
|
||||
return e.code, payload
|
||||
except Exception as e:
|
||||
return None, {"error": str(e)}
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description="multiuser verification")
|
||||
ap.add_argument("--base", default="http://127.0.0.1:8765")
|
||||
ap.add_argument("--db", default="/var/lib/codex-app-server/control.db")
|
||||
args = ap.parse_args()
|
||||
base = args.base.rstrip("/")
|
||||
|
||||
auth = AuthStore(args.db)
|
||||
created_task_ids = []
|
||||
created_user_ids = []
|
||||
try:
|
||||
# -- 预清理:上一轮残留的测试行(可重复跑)-------------------
|
||||
conn = db_connect(args.db)
|
||||
try:
|
||||
conn.execute(
|
||||
"DELETE FROM tasks WHERE project_id LIKE ?",
|
||||
(PREFIX + "%",))
|
||||
conn.execute(
|
||||
"DELETE FROM project_members WHERE project_id LIKE ?",
|
||||
(PREFIX + "%",))
|
||||
conn.execute(
|
||||
"DELETE FROM projects WHERE id LIKE ?", (PREFIX + "%",))
|
||||
conn.execute(
|
||||
"DELETE FROM users WHERE username LIKE ?", (PREFIX + "%",))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
# -- 建 4 个测试用户(key 只在内存里)------------------------
|
||||
users = {}
|
||||
for role in ("owner", "maintainer", "contributor", "viewer"):
|
||||
uid, key = auth.create_user(PREFIX + role)
|
||||
users[role] = (uid, key)
|
||||
created_user_ids.append(uid)
|
||||
|
||||
# -- 建 2 个测试项目(owner 建,P2 只有 owner 是成员)--------
|
||||
s, p1 = req(base, "POST", "/projects",
|
||||
users["owner"][1], {"id": P1, "name": P1})
|
||||
check("owner 创建项目 P1(201)", s == 201)
|
||||
s, p2 = req(base, "POST", "/projects",
|
||||
users["owner"][1], {"id": P2, "name": P2})
|
||||
check("owner 创建项目 P2(201)", s == 201)
|
||||
if s != 201 or p1.get("id") != P1:
|
||||
print("项目创建失败,无法继续验证")
|
||||
return 1
|
||||
for role in ("maintainer", "contributor", "viewer"):
|
||||
auth.set_member(P1, users["owner"][0], users[role][0], role)
|
||||
|
||||
# -- 建任务权限 ----------------------------------------------
|
||||
# 全部测试任务带永不满足的依赖,生产 worker 绝不会认领(见模块注释)。
|
||||
s, t_owner = req(base, "POST", "/tasks", users["owner"][1], {
|
||||
"project_id": P1, "prompt": "multiuser test task",
|
||||
"depends_on": [NEVER_DEP]})
|
||||
check("owner 在 P1 建任务(201)", s == 201)
|
||||
if s == 201:
|
||||
created_task_ids.append(t_owner["id"])
|
||||
s, t_contrib = req(base, "POST", "/tasks",
|
||||
users["contributor"][1], {
|
||||
"project_id": P1, "prompt": "multiuser test task",
|
||||
"depends_on": [NEVER_DEP]})
|
||||
check("contributor 在 P1 建任务(201)", s == 201)
|
||||
if s == 201:
|
||||
created_task_ids.append(t_contrib["id"])
|
||||
s, _ = req(base, "POST", "/tasks", users["viewer"][1], {
|
||||
"project_id": P1, "prompt": "multiuser test task"})
|
||||
check("viewer 在 P1 建任务(403)", s == 403)
|
||||
s, _ = req(base, "POST", "/tasks", users["viewer"][1], {
|
||||
"project_id": P2, "prompt": "multiuser test task"})
|
||||
check("非成员 viewer 在 P2 建任务(403)", s == 403)
|
||||
|
||||
# -- 读任务权限与跨项目隔离 ----------------------------------
|
||||
if created_task_ids:
|
||||
tid = created_task_ids[0]
|
||||
s, _ = req(base, "GET", "/tasks/" + tid, users["viewer"][1])
|
||||
check("viewer 读 P1 任务(200)", s == 200)
|
||||
s, t_p2 = req(base, "POST", "/tasks", users["owner"][1], {
|
||||
"project_id": P2, "prompt": "multiuser test task",
|
||||
"depends_on": [NEVER_DEP]})
|
||||
check("owner 在 P2 建任务(201)", s == 201)
|
||||
if s == 201:
|
||||
created_task_ids.append(t_p2["id"])
|
||||
s, _ = req(base, "GET", "/tasks/" + t_p2["id"],
|
||||
users["viewer"][1])
|
||||
check("P1 成员 viewer 读 P2 任务(403,跨项目隔离)", s == 403)
|
||||
|
||||
# -- merge 权限(需先把测试任务直接置 completed,不跑模型)--
|
||||
conn = db_connect(args.db)
|
||||
try:
|
||||
for tid in created_task_ids:
|
||||
conn.execute(
|
||||
"UPDATE tasks SET status = 'completed' "
|
||||
"WHERE id = ?", (tid,))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
if len(created_task_ids) >= 1:
|
||||
s, merged = req(base, "POST",
|
||||
"/tasks/" + created_task_ids[0] + "/merge",
|
||||
users["maintainer"][1])
|
||||
check("maintainer 对 completed 任务调 merge(200 且变 merged)",
|
||||
s == 200 and merged.get("status") == "merged")
|
||||
if len(created_task_ids) >= 2:
|
||||
s, _ = req(base, "POST",
|
||||
"/tasks/" + created_task_ids[1] + "/merge",
|
||||
users["contributor"][1])
|
||||
check("contributor 对 completed 任务调 merge(403)", s == 403)
|
||||
# queued 任务调 merge 应为 409:再建一个(不置 completed)。
|
||||
s, t_queued = req(base, "POST", "/tasks", users["owner"][1], {
|
||||
"project_id": P1, "prompt": "multiuser test task",
|
||||
"depends_on": [NEVER_DEP]})
|
||||
if s == 201:
|
||||
created_task_ids.append(t_queued["id"])
|
||||
s, _ = req(base, "POST",
|
||||
"/tasks/" + t_queued["id"] + "/merge",
|
||||
users["maintainer"][1])
|
||||
check("maintainer 对 queued 任务调 merge(409)", s == 409)
|
||||
|
||||
# -- 无 key ---------------------------------------------------
|
||||
s, _ = req(base, "GET", "/tasks/" + created_task_ids[0]
|
||||
if created_task_ids else "/tasks/none")
|
||||
check("无 key 调 API(401)", s == 401)
|
||||
finally:
|
||||
# -- 清理:只删 multiuser-test-* 的行,审计表不动 ------------
|
||||
leftover = []
|
||||
try:
|
||||
conn = db_connect(args.db)
|
||||
try:
|
||||
for tid in created_task_ids:
|
||||
conn.execute("DELETE FROM tasks WHERE id = ?", (tid,))
|
||||
conn.execute(
|
||||
"DELETE FROM tasks WHERE project_id LIKE ?",
|
||||
(PREFIX + "%",))
|
||||
conn.execute(
|
||||
"DELETE FROM project_members WHERE project_id LIKE ?",
|
||||
(PREFIX + "%",))
|
||||
conn.execute(
|
||||
"DELETE FROM projects WHERE id LIKE ?", (PREFIX + "%",))
|
||||
conn.execute(
|
||||
"DELETE FROM users WHERE username LIKE ?",
|
||||
(PREFIX + "%",))
|
||||
conn.commit()
|
||||
# 校验残留(只查测试前缀的行,不打印 key 或内容)。
|
||||
for table, col in (("tasks", "project_id"),
|
||||
("projects", "id"),
|
||||
("users", "username")):
|
||||
row = conn.execute(
|
||||
"SELECT COUNT(*) FROM %s WHERE %s LIKE ?"
|
||||
% (table, col), (PREFIX + "%",)).fetchone()
|
||||
if row and row[0]:
|
||||
leftover.append("%s:%d" % (table, row[0]))
|
||||
finally:
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
leftover.append("cleanup-error:%s" % e)
|
||||
try:
|
||||
auth.conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
if leftover:
|
||||
print("清理未完成,残留测试行:%s" % ", ".join(leftover))
|
||||
print("注意:审计日志里的测试条目是故意留下的,不算残留。")
|
||||
|
||||
passed = sum(1 for _, ok in RESULTS if ok)
|
||||
total = len(RESULTS)
|
||||
print("多用户验证:%d/%d 通过" % (passed, total))
|
||||
if leftover:
|
||||
return 1
|
||||
return 0 if passed == total else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user