fix: host_deploy stops/starts and health-checks server-2 too (#56)

Co-authored-by: host <host@test.local>
This commit is contained in:
openqjandhost authored and GitHub committed 2026-10-27 00:50:33 +08:00
1 parent 84ff968111
commit c6e633d162
3 files changed
+75 -13

No files matched your search

+27 -9
View File
@@ -20,12 +20,14 @@
# 4. 备份当前 live 代码(备份名含旧 commit + 时间戳,留最近 5 份;
# 备份 tar 遇运行中文件变化返回 1 时不直接 abort,只要备份文件
# 非空就继续,见 PR #46 教训)。
# 5. 停 server + worker/autoscaler,再换目录:live -> old,
# 5. 停 server(s) + worker/autoscaler,再换目录:live -> old,
# staging -> live,work/ 与 host-config-real.json(如有)原样搬回。
# 换目录代替“原地 rm + 解包”,中途失败不会留半解包的 live。
# 6. 启动并验 health 60 秒:ok=true 且 backend=postgres 且 db_ok=true,
# 且 live 的 DEPLOYED_COMMIT 等于 expected_commit;health 里
# 若有 commit 字段且非空,也必须等于 expected_commit。
# server-2(HA 阶段 2 加的 8766)若存在必须同批停/起、同批验
# health,否则 LB 会混跑新旧内存代码(2026-10-27 首推教训)。
# 7. 成功打印 RELEASE_OK commit=... sha=... backup=...,删掉 old 目录
# (备份 tar 已留档);失败则把 old 换回、重启、再验 health,
# 回滚后 health 恢复也算部署失败(新代码未上线),退出非零。
@@ -87,8 +89,10 @@ python3 -m compileall -q "$STAGING_DIR" || {
}
check_health() {
OUT=$(curl -s --max-time 5 http://127.0.0.1:8765/health || true)
[ -n "$OUT" ] && printf '%s' "$OUT" | EXPECTED_COMMIT="$EXPECTED_COMMIT" python3 -c '
for port in $HEALTH_PORTS; do
OUT=$(curl -s --max-time 5 "http://127.0.0.1:$port/health" || true)
if [ -z "$OUT" ]; then return 1; fi
if ! printf '%s' "$OUT" | EXPECTED_COMMIT="$EXPECTED_COMMIT" python3 -c '
import json, os, sys
try:
d = json.load(sys.stdin)
@@ -101,7 +105,11 @@ commit = d.get("commit")
if commit is not None and commit != os.environ.get("EXPECTED_COMMIT"):
sys.exit(1)
sys.exit(0)
'
'; then
return 1
fi
done
return 0
}
wait_health() {
@@ -122,6 +130,16 @@ worker_unit() {
fi
}
# server-2(8766)是 HA 阶段 2 加的;unit 存在才纳入停/起与 health
# 检查,老主机没有它时退回单机行为。2026-10-27 首推教训:脚本只动
# server-1 时,LB 上游的 server-2 仍跑旧内存代码,新旧混跑。
SERVER_UNITS="codex-cloud-server"
HEALTH_PORTS="8765"
if systemctl cat codex-cloud-server-2 >/dev/null 2>&1; then
SERVER_UNITS="$SERVER_UNITS codex-cloud-server-2"
HEALTH_PORTS="$HEALTH_PORTS 8766"
fi
# --- 4. 备份 live(名含旧 commit,留 5 份) ------------------------------
mkdir -p "$BACKUP_DIR"
PREV_COMMIT=unknown
@@ -144,8 +162,8 @@ ls -t "$BACKUP_DIR"/codex-app-server-*.tar.gz 2>/dev/null \
| tail -n +6 | xargs -r rm -f
WORKER_UNIT=$(worker_unit)
echo "停服务并换目录:worker_unit=$WORKER_UNIT"
systemctl stop "$WORKER_UNIT" codex-cloud-server || true
echo "停服务并换目录:worker_unit=$WORKER_UNIT server_units=$SERVER_UNITS"
systemctl stop "$WORKER_UNIT" $SERVER_UNITS || true
# --- 5. 换目录:live -> old,staging -> live,保留文件搬回 --------------
rm -rf "$OLD_DIR" "$PRESERVE_DIR"
@@ -169,7 +187,7 @@ fi
rollback() {
echo "新代码 health 未通过,开始回滚到备份:$BACKUP_FILE" >&2
systemctl stop "$WORKER_UNIT" codex-cloud-server || true
systemctl stop "$WORKER_UNIT" $SERVER_UNITS || true
rm -rf "$FAILED_DIR" "$PRESERVE_DIR"
mkdir -p "$PRESERVE_DIR"
for name in work host-config-real.json; do
@@ -188,7 +206,7 @@ rollback() {
if id codexsvc >/dev/null 2>&1; then
chown -R codexsvc:codexsvc "$APP_DIR"
fi
systemctl start codex-cloud-server "$WORKER_UNIT" || true
systemctl start $SERVER_UNITS "$WORKER_UNIT" || true
if wait_health; then
echo "部署失败,已回滚到备份,health 已恢复(新代码未上线)" >&2
else
@@ -198,7 +216,7 @@ rollback() {
}
# --- 6. 启动 + 验 health + 验上线 commit -------------------------------
systemctl start codex-cloud-server "$WORKER_UNIT" || true
systemctl start $SERVER_UNITS "$WORKER_UNIT" || true
if ! wait_health; then
rollback
fi