publish-workshop-onboarding-r4.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """Publish only the sealed onboarding and migration release from an isolated repository."""
  2. import argparse
  3. import hashlib
  4. import io
  5. import json
  6. from pathlib import Path
  7. import re
  8. import shutil
  9. import subprocess
  10. import urllib.request
  11. import zipfile
  12. ROOT = Path(__file__).resolve().parents[1]
  13. OUT = ROOT / 'docs/workshop-onboarding-20261109-r4'
  14. STAGING = ROOT / 'work/workshop-onboarding-r4-publication'
  15. BRANCH = 'codex/workshop-onboarding-20261109-r4'
  16. REMOTE = 'https://git.fmode.cn/18307996893/kangshifu-board.git'
  17. PUBLIC = REMOTE[:-4]
  18. sha = lambda value: hashlib.sha256(value).hexdigest()
  19. encode = lambda value: (json.dumps(value, ensure_ascii=False, indent=2) + '\n').encode('utf-8')
  20. def need(ok, code):
  21. if not ok:
  22. raise ValueError(code)
  23. def git(*args, cwd=STAGING, data=None):
  24. result = subprocess.run(['git', *args], cwd=cwd, input=data, capture_output=True, timeout=90)
  25. need(result.returncode == 0, 'GIT_OPERATION_FAILED')
  26. return result.stdout.decode('utf-8').strip()
  27. def secret_scan(files):
  28. # Compare actual private values in memory only; never include them or their fingerprints in output.
  29. private = Path('F:/文档/正式环境连接配置.txt').read_text('utf-8-sig')
  30. match = re.search(r'^\|\s*masterKey\s*\|\s*`?([^|`\s]+)', private, re.M)
  31. need(match is not None, 'LOCAL_SECRET_CHECK_CONFIG_MISSING')
  32. secrets = [match.group(1).encode('utf-8')]
  33. env = Path('F:/文档/康师傅数字人受限连接.env').read_text('utf-8-sig')
  34. for key, value in re.findall(r'^([A-Z0-9_]+)=(.+)$', env, re.M):
  35. if re.search('TOKEN|SECRET|PASSWORD|MASTER_KEY', key):
  36. value = value.strip().strip('\"\'')
  37. if len(value) >= 16:
  38. secrets.append(value.encode('utf-8'))
  39. checked = 0
  40. for name, raw in files.items():
  41. bodies = [(name, raw)]
  42. if name.endswith('.zip'):
  43. with zipfile.ZipFile(io.BytesIO(raw)) as z:
  44. bodies.extend((name + ':' + n, z.read(n)) for n in z.namelist())
  45. for label, body in bodies:
  46. need(not any(value in body for value in secrets), 'SECRET_VALUE_DETECTED')
  47. need(b'-----BEGIN PRIVATE KEY-----' not in body and b'-----BEGIN RSA PRIVATE KEY-----' not in body,
  48. 'PRIVATE_KEY_DETECTED')
  49. checked += 1
  50. return {'ok': True, 'filesAndArchiveMembersChecked': checked, 'credentialValuesIncluded': False}
  51. def prepare():
  52. need(not STAGING.exists(), 'NEW_PUBLICATION_DIRECTORY_REQUIRED')
  53. distribution = json.loads((OUT / 'publish/distribution.json').read_bytes())
  54. archive = distribution['archive']['path']
  55. names = ['INSTALL.md', 'distribution.json', archive,
  56. 'operations/migration.json', 'operations/ksf-unified-database-migration-20261109-r3.zip']
  57. files = {n: (OUT / 'publish' / n).read_bytes() for n in names}
  58. need(sha(files[archive]) == distribution['archive']['sha256'], 'ARCHIVE_CHANGED')
  59. for p in (OUT / 'package').rglob('*'):
  60. if p.is_file():
  61. files['source/' + p.relative_to(OUT / 'package').as_posix()] = p.read_bytes()
  62. files['.gitattributes'] = b'* -text\n'
  63. scan = secret_scan(files)
  64. (OUT / 'public-secret-check.json').write_bytes(encode(scan))
  65. STAGING.mkdir(parents=True)
  66. for name, raw in files.items():
  67. p = STAGING / name
  68. p.parent.mkdir(parents=True, exist_ok=True)
  69. p.write_bytes(raw)
  70. git('init', '--initial-branch=' + BRANCH)
  71. git('config', 'user.name', git('config', '--get', 'user.name', cwd=ROOT))
  72. git('config', 'user.email', git('config', '--get', 'user.email', cwd=ROOT))
  73. git('config', 'core.autocrlf', 'false')
  74. git('remote', 'add', 'origin', REMOTE)
  75. git('add', '--', *sorted(files))
  76. git('commit', '-m', 'release: workshop r9 installation, r7/r8 upgrade and unified database runtime delivery')
  77. need(set(git('ls-files').splitlines()) == set(files), 'UNEXPECTED_PUBLISHED_FILES')
  78. for name, raw in files.items():
  79. oid = git('rev-parse', 'HEAD:' + name)
  80. result = subprocess.run(['git', 'cat-file', 'blob', oid], cwd=STAGING, capture_output=True, check=True)
  81. need(result.stdout == raw, 'GIT_BLOB_CHANGED')
  82. commit = git('rev-parse', 'HEAD')
  83. plan = {'ok': True, 'commit': commit, 'branch': BRANCH, 'files': {n: sha(raw) for n, raw in files.items()},
  84. 'published': False, 'mainBranchModified': False, 'secretCheck': scan}
  85. (OUT / 'git-plan.json').write_bytes(encode(plan))
  86. print(json.dumps({'ok': True, 'prepared': True, 'commit': commit, 'files': len(files)}))
  87. def publish():
  88. plan = json.loads((OUT / 'git-plan.json').read_bytes())
  89. commit = plan['commit']
  90. need(git('rev-parse', 'HEAD') == commit and not git('status', '--porcelain'), 'PUBLICATION_STAGING_CHANGED')
  91. remote = git('ls-remote', 'origin', 'refs/heads/' + BRANCH)
  92. need(not remote or remote.split()[0] == commit, 'REMOTE_BRANCH_ALREADY_DIFFERS')
  93. git('push', 'origin', 'HEAD:refs/heads/' + BRANCH)
  94. need(git('ls-remote', 'origin', 'refs/heads/' + BRANCH).split()[0] == commit, 'PUSH_NOT_CONFIRMED')
  95. checks = []
  96. # Anonymous reads, without platform credentials, verify all public artifacts and sources.
  97. for name, expected in plan['files'].items():
  98. url = PUBLIC + '/raw/' + commit + '/' + name
  99. request = urllib.request.Request(url, headers={'User-Agent': 'ksf-release-verifier/1'})
  100. with urllib.request.urlopen(request, timeout=40) as response:
  101. raw = response.read()
  102. need(response.status == 200 and sha(raw) == expected, 'ONLINE_FILE_HASH_MISMATCH')
  103. checks.append({'path': name, 'url': url, 'status': response.status, 'bytes': len(raw),
  104. 'sha256': sha(raw), 'matchesLocal': True})
  105. guide = PUBLIC + '/raw/' + commit + '/INSTALL.md'
  106. prompt = ('请安装或升级康师傅产品创新工作坊技能。先读取固定版本安装说明:\n' + guide +
  107. '\n按同一固定提交目录的 distribution.json 下载并核验资源,再按说明执行。'
  108. '由你识别当前宿主实际支持的技能目录和接入方式,不需要我上传附件或填写技术路径。'
  109. '已有完整r7或r8按说明备份升级,已有同版只核验;保留本机身份、授权、连接配置和其他技能,'
  110. '不另装旧内部研究Skill。外部VOC工具按官方固定版本安装。'
  111. '实际核验文件、宿主加载、本地工具和企业通道各自的状态,不能把文件可读当成宿主已加载。'
  112. '初始化不采业务资料、不建报告或会话、不续跑旧案例;生产数据库迁移由平台运维另行执行。\n')
  113. (OUT / '一键初始化正文.txt').write_text(prompt, encoding='utf-8')
  114. receipt = {'ok': True, 'published': True, 'releaseId': 'ksf-workshop-onboarding-20261109-r4',
  115. 'repository': PUBLIC, 'branch': BRANCH, 'commit': commit, 'guideUrl': guide,
  116. 'resources': checks, 'allPublicFilesReadBack': True, 'mainBranchModified': False,
  117. 'workshopSkillVersion': 'r9', 'supportedUpgradeFrom': ['r7', 'r8'],
  118. 'operationsRuntimePackagePublished': True, 'productionMigrationExecuted': False,
  119. 'targetHostInstallationVerified': False, 'businessDataModified': False}
  120. (OUT / 'online-release-receipt.json').write_bytes(encode(receipt))
  121. print(json.dumps({'ok': True, 'published': True, 'commit': commit, 'guideUrl': guide,
  122. 'publicFilesVerified': len(checks), 'productionMigrationExecuted': False}))
  123. if __name__ == '__main__':
  124. p = argparse.ArgumentParser(description=__doc__)
  125. p.add_argument('command', choices=('prepare', 'publish'))
  126. args = p.parse_args()
  127. prepare() if args.command == 'prepare' else publish()