53 lines
3.3 KiB
Python
53 lines
3.3 KiB
Python
import hashlib,json,subprocess,shutil,urllib.request,re,time
|
|
from pathlib import Path
|
|
release=Path('/home/ps/token-ui-restore-20260924');target=Path('/opt/im-admin');compose=target/'deploy/im-admin'
|
|
baseline=json.loads((release/'baseline.json').read_text());base=baseline['im-admin-api-1']['image']
|
|
current=json.loads(subprocess.check_output(['docker','inspect','im-admin-api-1']))[0]
|
|
assert current['Image']==base and current['Id']==baseline['im-admin-api-1']['id'], 'API changed during staging'
|
|
manifest=json.loads((release/'manifest.json').read_text())
|
|
restored=[]
|
|
for row in manifest:
|
|
dest=(target/row['path']).resolve();assert target in dest.parents
|
|
source=release/'payload'/row['path'];assert hashlib.sha256(source.read_bytes()).hexdigest()==row['sha256']
|
|
existed=dest.exists();restored.append({'path':row['path'],'existed':existed})
|
|
if existed:
|
|
backup=release/'source-before'/row['path'];backup.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(dest,backup)
|
|
(release/'restore-files.json').write_text(json.dumps(restored,indent=2))
|
|
def compose_up():
|
|
subprocess.run(['docker','compose','up','-d','--no-deps','--no-build','--wait','--wait-timeout','120','api'],cwd=compose,check=True)
|
|
def rollback():
|
|
for row in restored:
|
|
dest=(target/row['path']).resolve();assert target in dest.parents
|
|
if row['existed']:shutil.copyfile(release/'source-before'/row['path'],dest)
|
|
elif dest.exists():dest.unlink()
|
|
subprocess.run(['docker','tag',base,'zyt/wecom-admin:current'],check=True);compose_up()
|
|
try:
|
|
for row in sorted(manifest,key=lambda row:row['path'].endswith('/index.html')):
|
|
dest=target/row['path'];dest.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(release/'payload'/row['path'],dest)
|
|
subprocess.run(['docker','tag','zyt/wecom-admin:token-ui-20260924','zyt/wecom-admin:current'],check=True)
|
|
compose_up()
|
|
dist=release/'payload/admin-web/apps/web-antd/dist'
|
|
index=(dist/'index.html').read_text()
|
|
entry=re.search(r'src="(/jse/[^"]+)',index)[1]
|
|
bootstrap='js/'+re.search(r'bootstrap-[\w-]+\.js',(dist/entry.lstrip('/')).read_text())[0]
|
|
stats=list((dist/'js').glob('call-stats-*.js'));usage=list((dist/'js').glob('ModelUsageStats-*.js'));api=list((dist/'js').glob('console-*.js'))
|
|
assert len(stats)==1 and len(usage)==1 and 'ModelUsageStats-' in stats[0].read_text()
|
|
assert '/stats/model-usage/log' in api[0].read_text()
|
|
paths=['/','/'+entry.lstrip('/'),'/'+bootstrap]+['/'+str(p.relative_to(dist)) for p in stats+usage+api]
|
|
verified={}
|
|
for path in paths:
|
|
with urllib.request.urlopen('http://127.0.0.1:8369'+path,timeout=20) as resp:
|
|
body=resp.read();local=dist/('index.html' if path=='/' else path.lstrip('/'))
|
|
assert resp.status==200 and body==local.read_bytes(),path
|
|
verified[path]={'http':200,'sha256':hashlib.sha256(body).hexdigest()}
|
|
services={}
|
|
for c in json.loads(subprocess.check_output(['docker','inspect',*baseline])):
|
|
name=c['Name'].lstrip('/');assert c['State']['Running']
|
|
if name!='im-admin-api-1':assert c['Id']==baseline[name]['id'],'Unrelated service changed'
|
|
else:assert c['State']['Health']['Status']=='healthy'
|
|
services[name]={'image':c['Image'],'health':c['State'].get('Health',{}).get('Status','not_configured')}
|
|
result={'http':verified,'services':services,'source_files':len(manifest)}
|
|
(release/'verification.json').write_text(json.dumps(result,indent=2));print(json.dumps(result))
|
|
except Exception:
|
|
rollback();raise
|