"""Read-only checks for mobile-remote-20260928: baseline / candidate / deployed. Prints hashes and status only; never reads chat content, patients or credentials. """ from pathlib import Path import argparse, hashlib, json, subprocess, urllib.error, urllib.request parser = argparse.ArgumentParser() parser.add_argument('mode', choices=['baseline', 'candidate', 'deployed']) args = parser.parse_args() RELEASE = 'mobile-remote-20260928' folder = Path(__file__).resolve().parent project = Path('/opt/im-admin') base = json.loads((folder / 'server-baseline.json').read_text()) manifest = json.loads((folder / 'manifest.json').read_text()) payload_rows = [row for row in manifest if row['path'].startswith('payload/')] sha = lambda data: hashlib.sha256(data).hexdigest() image = lambda ref: subprocess.check_output(['docker', 'image', 'inspect', ref, '--format', '{{.Id}}'], text=True).strip() # The unpacked bundle itself must be intact. for row in manifest: path = (folder / row['path']).resolve() assert path.is_relative_to(folder), row['path'] assert path.stat().st_size == row['bytes'] and sha(path.read_bytes()) == row['sha256'], row['path'] # project-relative path -> expected sha256 on the host if args.mode == 'baseline': expected_host = dict(base['expected']) else: expected_host = {row['path'][len('payload/'):]: row['sha256'] for row in payload_rows} container_code = {k: v for k, v in expected_host.items() if k.startswith(('wechat_rpa/', 'admin-web/'))} def container_hashes(target, ref=None): code = ('from pathlib import Path;import hashlib,json,sys;names=json.load(sys.stdin);' 'print(json.dumps({n:(hashlib.sha256((Path("/app")/n).read_bytes()).hexdigest() ' 'if (Path("/app")/n).is_file() else None) for n in names}))') command = (['docker', 'run', '--rm', '-i', '--network=none', '--entrypoint', 'python', ref, '-c', code] if ref else ['docker', 'exec', '-i', 'im-admin-api-1', 'python', '-c', code]) return json.loads(subprocess.check_output(command, input=json.dumps(target).encode())) current = image('zyt/wecom-admin:current') result = {'mode': args.mode, 'release': RELEASE, 'image': current} # 只重建 api:网关和知识库 worker 继续跑它们自己的镜像(2026-09-24 那次前端发布同样只换了 api), # 这里记录下来,部署后核对它们没有被动过。 OTHERS = ['im-admin-gateway-1', 'im-admin-knowledge-worker-1'] def inspect(container): raw = json.loads(subprocess.check_output(['docker', 'inspect', container]))[0] return {'id': raw['Id'], 'image': raw['Image'], 'running': raw['State']['Running'], 'health': raw['State'].get('Health', {}).get('Status', 'not_configured')} if args.mode == 'baseline': api = inspect('im-admin-api-1') assert api['image'] == current, 'API container is not on zyt/wecom-admin:current' assert api['running'] and api['health'] == 'healthy', 'API container is not healthy' others = {name: inspect(name) for name in OTHERS} for name, state in others.items(): assert state['running'], 'Not running: ' + name result['others'] = others for name in base.get('must_not_exist', []): assert not (project / name).exists(), 'Unexpected existing path (partially deployed?): ' + name if args.mode in ('baseline', 'deployed'): for name, wanted in expected_host.items(): assert sha((project / name).read_bytes()) == wanted, 'Host drift: ' + name actual = container_hashes(sorted(container_code)) assert actual == container_code, 'Running api container differs from expected code' result['files'] = len(expected_host) if args.mode == 'candidate': candidate = 'zyt/wecom-admin:' + RELEASE actual = container_hashes(sorted(container_code), candidate) assert actual == container_code, 'Candidate image differs from manifest' result['candidate'] = image(candidate) result['files'] = len(container_code) if args.mode == 'deployed': wanted = image('zyt/wecom-admin:' + RELEASE) assert current == wanted, 'current tag does not point at the release image' raw = json.loads(subprocess.check_output(['docker', 'inspect', 'im-admin-api-1']))[0] assert raw['Image'] == wanted and raw['State']['Running'] and raw['RestartCount'] == 0 assert raw['State'].get('Health', {}).get('Status') == 'healthy' recorded = folder / 'baseline-check.json' if recorded.exists(): before = json.loads(recorded.read_text())['others'] for name in OTHERS: now = inspect(name) assert now['id'] == before[name]['id'] and now['running'], 'Unrelated container changed: ' + name result['others_unchanged'] = OTHERS for url in ['http://127.0.0.1:18766/api/v2/health', 'http://127.0.0.1:18770/health']: with urllib.request.urlopen(url, timeout=15) as response: assert response.status == 200, url # New routes exist and refuse anonymous callers. No login is attempted. for method, path in [('GET', '/api/v2/mobile/me'), ('GET', '/api/v2/mobile/desktops'), ('POST', '/api/v2/desktop/commands/poll'), ('GET', '/api/v2/desktop/blacklist')]: request = urllib.request.Request('http://127.0.0.1:18766' + path, method=method, data=b'{}' if method == 'POST' else None, headers={'Content-Type': 'application/json'}) try: urllib.request.urlopen(request, timeout=15) except urllib.error.HTTPError as exc: assert exc.code == 401, (path, exc.code) else: raise AssertionError('Accessible without authentication: ' + path) with urllib.request.urlopen('http://127.0.0.1:18766/m/', timeout=15) as response: body = response.read() assert response.status == 200 and '企微客服助手'.encode() in body, 'mobile console entry missing' code = ("import sqlite3,json;c=sqlite3.connect('file:/data/backend.db?mode=ro',uri=True,timeout=30);" "names={r[0] for r in c.execute(\"SELECT name FROM sqlite_master WHERE type='table'\")};" "print(json.dumps({'tables':sorted(names & {'mobile_sessions','desktop_commands','desktop_status','conversation_blacklist'})," "'quick_check':c.execute('PRAGMA quick_check').fetchone()[0]}))") db = json.loads(subprocess.check_output(['docker', 'exec', '-i', 'im-admin-api-1', 'python', '-c', code])) assert db['quick_check'] == 'ok' and len(db['tables']) == 4, db result['database'] = db result['status'] = 'passed' print(json.dumps(result, ensure_ascii=False))