gengx
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
"""Assemble the server bundle locally: payload + tests + manifest + tar.gz. No upload, no deployment.
|
||||
|
||||
python prepare_bundle.py
|
||||
|
||||
Backend: production files were fetched into baseline-snapshot/host (2026-10-07, after perm-menu-20261007).
|
||||
For each replaced file the release version is `git merge-file ours=working tree, base=kefu HEAD, theirs=production`,
|
||||
i.e. production plus exactly this change (production == kefu HEAD for all three files, so the result is the working tree;
|
||||
the merge is kept as a guard against drift).
|
||||
Tests: test_admin_api.py ships as the working tree version (the image's own copy is older; it is the file the release
|
||||
code was tested with locally).
|
||||
Frontend: web-antd rebuilt from the publishing source C:/wechat_rpa/admin-web. Evidence that nothing else changed:
|
||||
`frontend-head-build/` is a build of the same tree with only the knowledge files reverted (= what is live), and its page
|
||||
graph must equal production's except the build time (about.js, jse/index-index.js). The new build's added files must equal release-plan 'frontendAdded'.
|
||||
"""
|
||||
from pathlib import Path
|
||||
import difflib, hashlib, json, re, shutil, subprocess, tarfile, tempfile
|
||||
|
||||
folder = Path(__file__).resolve().parent
|
||||
kefu = folder.parent.parent
|
||||
plan = json.loads((folder / 'release-plan.json').read_text(encoding='utf-8'))
|
||||
base = json.loads((folder / 'server-baseline.json').read_text(encoding='utf-8'))
|
||||
sha = lambda data: hashlib.sha256(data).hexdigest()
|
||||
lf = lambda data: data.replace(b'\r\n', b'\n')
|
||||
snapshot = folder / 'baseline-snapshot'
|
||||
RELEASE = plan['release']
|
||||
|
||||
|
||||
def head(name):
|
||||
return lf(subprocess.run(['git', '-C', str(kefu), 'show', 'HEAD:' + name], check=True, capture_output=True).stdout)
|
||||
|
||||
|
||||
def merge(ours, base_text, theirs, label):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
tmp = Path(tmp)
|
||||
for name, data in (('ours', ours), ('base', base_text), ('theirs', theirs)):
|
||||
(tmp / name).write_bytes(data)
|
||||
result = subprocess.run(['git', 'merge-file', '-p', str(tmp / 'ours'), str(tmp / 'base'), str(tmp / 'theirs')],
|
||||
capture_output=True)
|
||||
assert result.returncode == 0, 'Merge conflict: ' + label
|
||||
return result.stdout
|
||||
|
||||
|
||||
# 1. Production baseline snapshot is what the server has now.
|
||||
for name in plan['backendFiles']:
|
||||
assert sha((snapshot / 'host' / Path(name).name).read_bytes()) == base['expected'][name], 'Snapshot drift: ' + name
|
||||
assert sha((snapshot / 'web-dist' / 'index.html').read_bytes()) == base['expected'][plan['frontendDir'] + '/index.html']
|
||||
|
||||
payload = folder / 'payload'
|
||||
if payload.exists():
|
||||
shutil.rmtree(payload)
|
||||
diff_lines = []
|
||||
merged_files = []
|
||||
for name in plan['backendFiles']:
|
||||
production = lf((snapshot / 'host' / Path(name).name).read_bytes())
|
||||
ours = lf((kefu / name).read_bytes())
|
||||
# 工作区就是在线上版本(上一版发布载荷)上改的,三方合并的基准取线上
|
||||
data = merge(ours, production if plan.get('mergeBase') == 'production' else head(name), production, name)
|
||||
if production != head(name):
|
||||
merged_files.append(name)
|
||||
assert b'\r' not in data, 'Stray carriage return: ' + name
|
||||
compile(data, name, 'exec')
|
||||
diff_lines += difflib.unified_diff(production.decode('utf-8').splitlines(), data.decode('utf-8').splitlines(),
|
||||
'production/' + name, 'release/' + name, lineterm='')
|
||||
target = payload / name
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_bytes(data)
|
||||
(folder / 'payload-diff.txt').write_text('\n'.join(diff_lines) + '\n', encoding='utf-8')
|
||||
|
||||
# 2. Frontend.
|
||||
frontend = plan['frontendDir']
|
||||
dist = Path(plan['frontendSource'])
|
||||
live = snapshot / 'web-dist' # 线上 api 容器里的 dist 原样取回(含历次构建残留的旧文件)
|
||||
REF = re.compile(r'[A-Za-z0-9_.-]+?-[A-Za-z0-9_-]{8}\.(?:js|css)')
|
||||
unhash = lambda name: re.sub(r'-[A-Za-z0-9_-]{8}(?=\.(js|css)$)', '', name)
|
||||
|
||||
|
||||
def graph(root):
|
||||
"""index.html 实际会加载的文件(沿引用递归),键去掉内容哈希,值里的文件名也去掉哈希。"""
|
||||
names = {p.name: p.relative_to(root).as_posix() for p in root.rglob('*') if p.is_file()}
|
||||
seen, todo = {}, ['index.html']
|
||||
while todo:
|
||||
rel = todo.pop()
|
||||
if rel in seen:
|
||||
continue
|
||||
seen[rel] = (root / rel).read_text(encoding='utf-8', errors='replace')
|
||||
todo += [names[m] for m in REF.findall(seen[rel]) if m in names and names[m] not in seen]
|
||||
return {unhash(rel): REF.sub(lambda m: unhash(m.group(0)), text) for rel, text in seen.items()}
|
||||
|
||||
|
||||
BUILD_TIME = re.compile(r'buildTime:`[^`]*`|Date Created: \d{4}-\d{2}-\d{2}')
|
||||
old_graph, head_graph, new_graph = graph(live), graph(folder / plan['frontendHeadBuild']), graph(dist)
|
||||
assert set(old_graph) == set(head_graph), 'HEAD build page graph differs from production'
|
||||
head_changed = sorted(k for k in head_graph
|
||||
if BUILD_TIME.sub('', head_graph[k]) != BUILD_TIME.sub('', old_graph[k]))
|
||||
assert head_changed == [], 'HEAD source does not build to production: ' + repr(head_changed)
|
||||
dropped = set(old_graph) - set(new_graph)
|
||||
# 删掉的旧整理向导三页,以及被打包工具并进别的分块的 antd 小工具;其余一页都不能少
|
||||
assert dropped == set(plan['frontendRemovedPages']), 'Unexpected page graph change: ' + repr(dropped)
|
||||
assert set(plan['frontendNewPages']) <= set(new_graph), 'New pages missing from the build'
|
||||
live_files = {p.relative_to(live).as_posix(): sha(p.read_bytes()) for p in live.rglob('*') if p.is_file()}
|
||||
new_files = {p.relative_to(dist).as_posix(): sha(p.read_bytes()) for p in dist.rglob('*') if p.is_file()}
|
||||
assert sorted(k for k in new_files if k not in live_files) == sorted(plan['frontendAdded'])
|
||||
assert [k for k in new_files if k in live_files and live_files[k] != new_files[k]] == plan['frontendReplaced']
|
||||
for rel in plan['frontendReplaced'] + plan['frontendAdded']:
|
||||
data = (dist / rel).read_bytes()
|
||||
target = payload / frontend / rel
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_bytes(data)
|
||||
entry = next(rel for rel in plan['frontendAdded'] if rel.startswith('jse/index-index-'))
|
||||
assert entry in (dist / 'index.html').read_text(encoding='utf-8'), 'index.html does not load the new entry'
|
||||
dialog = next(rel for rel in plan['frontendAdded'] if rel.startswith('js/run-dialog-'))
|
||||
assert '开始整理'.encode() in (dist / dialog).read_bytes()
|
||||
panel = next(rel for rel in plan['frontendAdded'] if rel.startswith('js/runs-panel-'))
|
||||
assert '换模型'.encode() in (dist / panel).read_bytes() and '继续出错的账号'.encode() in (dist / panel).read_bytes()
|
||||
options_page = next(rel for rel in plan['frontendAdded'] if rel.startswith('js/wash-options-'))
|
||||
assert '每次请求合并'.encode() in (dist / options_page).read_bytes()
|
||||
# 旧的单账号整理向导已删除:新入口里不该再有它
|
||||
assert not any('job-wizard' in key for key in new_graph), 'old wizard still referenced'
|
||||
|
||||
# 3. Tests: the working tree versions the release code was tested with.
|
||||
tests = folder / 'tests'
|
||||
if tests.exists():
|
||||
shutil.rmtree(tests)
|
||||
tests.mkdir()
|
||||
for module in sorted({m.split('.')[0] for m in plan['serverTestModules']}):
|
||||
data = lf((kefu / f'wechat_rpa/{module}.py').read_bytes())
|
||||
compile(data, module, 'exec')
|
||||
(tests / f'{module}.py').write_bytes(data)
|
||||
|
||||
scripts = ['release-plan.json', 'server-baseline.json', 'Dockerfile.overlay', 'remote_server_guard.py',
|
||||
'01-preflight.sh', '02-build.sh', '03-deploy.sh', '04-verify.sh', 'rollback.sh']
|
||||
|
||||
# 4. Manifest over everything the server will verify.
|
||||
manifest = []
|
||||
for path in sorted(p for p in list(payload.rglob('*')) + list(tests.rglob('*')) if p.is_file()):
|
||||
data = path.read_bytes()
|
||||
manifest.append({'path': path.relative_to(folder).as_posix(), 'sha256': sha(data), 'bytes': len(data)})
|
||||
(folder / 'manifest.json').write_text(json.dumps(manifest, indent=2), encoding='utf-8')
|
||||
|
||||
# 5. Server bundle (LF line endings for scripts) and archive.
|
||||
bundle = folder / 'server-bundle'
|
||||
if bundle.exists():
|
||||
shutil.rmtree(bundle)
|
||||
bundle.mkdir()
|
||||
shutil.copytree(payload, bundle / 'payload')
|
||||
shutil.copytree(tests, bundle / 'tests')
|
||||
for name in scripts + ['manifest.json']:
|
||||
(bundle / name).write_bytes(lf((folder / name).read_bytes()))
|
||||
archive = folder / 'knowledge-batch-server-bundle.tar.gz'
|
||||
if archive.exists():
|
||||
archive.unlink()
|
||||
with tarfile.open(archive, 'x:gz') as tf:
|
||||
for p in sorted(bundle.rglob('*')):
|
||||
if p.is_file():
|
||||
arc = p.relative_to(bundle).as_posix(); assert not arc.startswith(('/', '..'))
|
||||
info = tf.gettarinfo(str(p), arcname=arc)
|
||||
info.mode = 0o755 if arc.endswith('.sh') else 0o644
|
||||
info.uid = info.gid = 0
|
||||
info.uname = info.gname = ''
|
||||
with p.open('rb') as handle:
|
||||
tf.addfile(info, handle)
|
||||
print(json.dumps({'archive': str(archive), 'bytes': archive.stat().st_size, 'sha256': sha(archive.read_bytes()),
|
||||
'files': len(manifest), 'diff_lines': len(diff_lines), 'merged': merged_files,
|
||||
'frontend_added': len(plan['frontendAdded']), 'uploaded': False},
|
||||
ensure_ascii=False, indent=1))
|
||||
Reference in New Issue
Block a user