#!/usr/bin/env bash # 数据库一致性备份 + 构建候选镜像 + 离线跑测试 + 核对候选镜像文件哈希。线上服务不受影响。 set -euo pipefail test "${REMOTE_RELEASE_APPROVED:-}" = mobile-remote-20260928 release=/home/ps/mobile-remote-20260928 cd "$release" test -f preflight-complete test ! -e candidate-passed python3 remote_server_guard.py baseline test "$(docker image inspect zyt/wecom-admin:before-mobile-remote-20260928 --format '{{.Id}}')" = "$(cat image-before.txt)" # 新表都是 CREATE TABLE IF NOT EXISTS 的纯新增;备份仍然先做,作为安全网。 # backup.json 只在备份和 quick_check 都成功后才出现;本脚本在测试阶段停下后重跑时沿用它,不重复拷整库。 if [ -f backup.json ]; then echo "reusing backup: $(cat backup.json)" docker exec -i im-admin-api-1 python - "$(cat backup.json)" <<'PY' from pathlib import Path import json, sys info = json.loads(sys.argv[1]); folder = Path(info['backup']) assert info['quick_check'] == 'ok' and (folder / 'backend.db').stat().st_size == info['bytes'], 'Recorded backup changed' assert len(list(folder.glob('*.key'))) == info['keyCount'], 'Key backup missing' PY else docker exec -i im-admin-api-1 python - <<'PY' > backup.json.tmp from pathlib import Path import sqlite3, shutil, os, json folder = Path('/data/backups/mobile-remote-20260928'); folder.mkdir(mode=0o700) source = sqlite3.connect('file:/data/backend.db?mode=ro', uri=True, timeout=30) backup = sqlite3.connect(folder / 'backend.db') try: source.backup(backup, pages=0) finally: backup.close(); source.close() with sqlite3.connect('file:' + str(folder / 'backend.db') + '?mode=ro', uri=True) as check: assert check.execute('PRAGMA quick_check').fetchone()[0] == 'ok' os.chmod(folder / 'backend.db', 0o600) keys = list(Path('/data').glob('*.key')); assert keys, 'Encryption key backup missing' for p in keys: shutil.copy2(p, folder / p.name); os.chmod(folder / p.name, 0o600) print(json.dumps({'backup': str(folder), 'quick_check': 'ok', 'bytes': (folder / 'backend.db').stat().st_size, 'keyCount': len(keys)})) PY mv backup.json.tmp backup.json cat backup.json fi docker build --network=none --pull=false -f Dockerfile.overlay -t zyt/wecom-admin:mobile-remote-20260928 . > candidate-build.log 2>&1 # serverTestModules 随包放在 tests/;imageTestModules 用镜像自带的测试文件(PYTHONPATH 后一项)。 # -P:不把工作目录 /app/wechat_rpa 放到 sys.path 最前面,否则镜像里同名的旧测试会盖住随包的新测试。 mapfile -t modules < <(python3 -c 'import json;p=json.load(open("release-plan.json"));print("\n".join(p["serverTestModules"]+p.get("imageTestModules",[])))') test "${#modules[@]}" -gt 0 docker run --rm --network=none --read-only --tmpfs /tmp:size=512m,mode=1777 --tmpfs /data:size=256m,mode=1777 \ -v "$release/tests:/release-tests:ro" -e PYTHONPATH=/release-tests:/app/wechat_rpa \ zyt/wecom-admin:mobile-remote-20260928 python -X utf8 -B -P -m unittest "${modules[@]}" -q > candidate-tests.log 2>&1 tail -n 3 candidate-tests.log python3 remote_server_guard.py candidate printf '%s\n' passed > candidate-passed