#!/usr/bin/env bash # 替换 remote_api.py 和整个 /m/ 目录;切换镜像,只重建 api 容器。任何一步失败自动调用 rollback.sh。 set -euo pipefail test "${REMOTE_RELEASE_APPROVED:-}" = mobile-http-20260928 release=/home/ps/mobile-http-20260928 cd "$release" test -f candidate-passed python3 remote_server_guard.py baseline cd /opt/im-admin/deploy/im-admin docker compose config --quiet rollback_on_error() { rc=$?; trap - ERR; bash "$release/rollback.sh"; exit "$rc"; } trap rollback_on_error ERR python3 - <<'PY' from pathlib import Path import json, hashlib, os, shutil r = Path('/home/ps/mobile-http-20260928'); target = Path('/opt/im-admin') plan = json.loads((r / 'release-plan.json').read_text()) rows = [row for row in json.loads((r / 'manifest.json').read_text()) if row['path'].startswith('payload/')] frontend = plan['frontendDir'] # 容器从镜像里读 /m/,宿主上的目录只给以后整包重建用;仍然整体换名,避免留下半套文件。 staging = target / (frontend + '.mobile-http.tmp'); assert not staging.exists() old = target / (frontend + '.mobile-http.old'); assert not old.exists() for row in rows: rel = row['path'][len('payload/'):] data = (r / row['path']).read_bytes(); assert hashlib.sha256(data).hexdigest() == row['sha256'], rel if rel.startswith(frontend + '/'): dest = staging / rel[len(frontend) + 1:] dest.parent.mkdir(parents=True, exist_ok=True) dest.write_bytes(data); os.chmod(dest, 0o644) continue dest = (target / rel).resolve(); assert dest.is_relative_to(target) and dest.exists() tmp = dest.with_name(dest.name + '.mobile-http.tmp'); assert not tmp.exists() with tmp.open('xb') as f: f.write(data) os.chmod(tmp, dest.stat().st_mode & 0o777); os.replace(tmp, dest) for directory in [staging] + [p for p in staging.rglob('*') if p.is_dir()]: os.chmod(directory, 0o755) final = target / frontend os.replace(final, old) os.replace(staging, final) shutil.rmtree(old) # 原目录已在 source-before 里 PY docker tag zyt/wecom-admin:mobile-http-20260928 zyt/wecom-admin:current docker compose up -d --no-deps --no-build --wait --wait-timeout 180 api bash "$release/04-verify.sh" trap - ERR printf '%s\n' 'Plain-HTTP phone login enabled, deployed and verified.'