Files
zyt/identity-admin/.test-runtime/kf-knowledge-sections-20261105/preflight_remote.py
T
2026-11-11 14:22:02 +08:00

31 lines
2.4 KiB
Python

import os,json,subprocess,hashlib,shutil,sqlite3,time
ROOT='/opt/im-admin';WORK=ROOT+'/deploy/knowledge-sections-20261105'
assert not os.path.lexists(WORK)
os.mkdir(WORK,0o700);os.mkdir(WORK+'/before',0o700)
def save(name,data):
p=WORK+'/'+name
with open(p,'w') as f:json.dump(data,f,indent=2)
os.chmod(p,0o600)
services=json.loads(subprocess.check_output(['docker','inspect','im-admin-api-1','im-admin-gateway-1','im-admin-knowledge-worker-1']))
assert all(c['Config']['Image']=='zyt/wecom-admin:assignment-menu-20261105-e078294536' for c in services)
assert all(c['State']['Running'] for c in services)
save('services-before.json',services)
for src,dest in [(ROOT+'/deploy/im-admin/compose.yaml','compose.yaml'),('/www/server/panel/vhost/nginx/im.zhenyangtang.cn.conf','im-vhost.conf')]:
shutil.copy2(src,WORK+'/before/'+dest);os.chmod(WORK+'/before/'+dest,0o600)
shutil.copytree(ROOT+'/admin-web/apps/web-antd/dist',WORK+'/before/frontend-dist')
shutil.copy2(ROOT+'/deploy/assignment-menu-20261105/directory-fingerprint.py',WORK+'/directory-fingerprint.py')
def sha(p):return hashlib.sha256(open(p,'rb').read()).hexdigest()
probe="import pathlib,json,hashlib;print(json.dumps({p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in pathlib.Path('/app/wechat_rpa').glob('*.py')}))"
sources={}
for c in services:sources[c['Name']]=json.loads(subprocess.check_output(['docker','exec',c['Id'],'python','-c',probe]))
host={n:sha(ROOT+'/wechat_rpa/'+n) for n in sources['/im-admin-api-1'] if os.path.isfile(ROOT+'/wechat_rpa/'+n)}
save('source-before.json',{'services':sources,'host':host})
started=time.time()
with sqlite3.connect('file:'+ROOT+'/deploy/im-admin/data/backend.db?mode=ro',uri=True,timeout=30) as src,sqlite3.connect(WORK+'/backend-before.sqlite') as dst:
src.execute('BEGIN');src.execute('SELECT COUNT(*) FROM sqlite_master').fetchone()
src.backup(dst,pages=8192,sleep=0.05);src.rollback()
os.chmod(WORK+'/backend-before.sqlite',0o600)
save('preflight.json',{'services_running':3,'snapshot_bytes':os.path.getsize(WORK+'/backend-before.sqlite'),'backup_seconds':round(time.time()-started),'before_image':services[0]['Config']['Image']})
export='/home/ps/knowledge-sections-source-before-20261105.json';shutil.copy2(WORK+'/source-before.json',export);os.chown(export,1000,1000);os.chmod(export,0o600)
print(json.dumps({'snapshot_ready':True,'services_running':3,'snapshot_bytes':os.path.getsize(WORK+'/backend-before.sqlite')}))