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

215 lines
12 KiB
Python

import os,json,subprocess,shutil,time,re,hashlib,socket,urllib.request,sqlite3,threading,urllib.error
from contextlib import closing
ROOT='/opt/im-admin'
WORK=ROOT+'/deploy/knowledge-sections-20261105'
DATA=ROOT+'/deploy/im-admin/data'
COMPOSE=ROOT+'/deploy/im-admin/compose.yaml'
VHOST='/www/server/panel/vhost/nginx/im.zhenyangtang.cn.conf'
NGINX='/www/server/nginx/sbin/nginx'
exec(open(WORK+'/deploy-common.py').read())
state=json.load(open(WORK+'/prepared.json'))
before=json.load(open(WORK+'/services-before.json'))
old={c['Config']['Labels']['com.docker.compose.service']:c for c in before}
services=['api','gateway','knowledge-worker']
assert set(old)==set(services)
assert json.load(open(WORK+'/candidate-checks.json'))['unchanged'] is True
proof=json.load(open(WORK+'/candidate-http-result.json'))
assert proof['independent_task_and_search'] is True
assert proof['operations_need_extra_permission'] is True
assert proof['same_session_revocation'] is True
assert sha(COMPOSE)==sha(WORK+'/before/compose.yaml')
assert sha(VHOST)==sha(WORK+'/before/im-vhost.conf')
fingerprint=open(WORK+'/directory-fingerprint.py').read()
baseline=json.loads(run(['docker','exec','im-admin-api-1','python','-c',fingerprint]))
with open(WORK+'/activation-before-fingerprints.json','w') as f:json.dump(baseline,f)
def same_access(a,b):return a==b
def update_service(compose,name,image,env):
lines=compose.splitlines(keepends=True)
start=next(i for i,l in enumerate(lines) if l==' '+name+':\n')
end=next((i for i in range(start+1,len(lines)) if re.match(r'^ [A-Za-z_-]+:',lines[i])),len(lines))
block=lines[start:end]
images=[i for i,l in enumerate(block) if re.match(r'^ image:',l)]
assert len(images)<2
if images:block[images[0]]=' image: '+json.dumps(image)+'\n'
else:block.insert(1,' image: '+json.dumps(image)+'\n')
e=next((i for i,l in enumerate(block) if l==' environment:\n'),None)
if e is None:block[2:2]=[' environment:\n',env_yaml(env)]
else:
stop=next((i for i in range(e+1,len(block)) if re.match(r'^ \S',block[i])),len(block))
block[e:stop]=[' environment:\n',env_yaml(env)]
return ''.join(lines[:start]+block+lines[end:])
compose_before=open(COMPOSE).read();updated=compose_before
environments={name:dict(x.split('=',1) for x in old[name]['Config']['Env'] if '=' in x) for name in services}
for name in services:environments[name].update(state['environment_updates'])
for name in services:updated=update_service(updated,name,state['new_image'],environments[name])
staged=ROOT+'/deploy/im-admin/compose.knowledge-sections-staged.yaml'
assert not os.path.lexists(staged)
with open(staged,'w') as f:f.write(updated)
os.chmod(staged,0o600)
rendered=json.loads(run(['docker','compose','-f',staged,'config','--format','json']))
for name in services:
s=rendered['services'][name]
assert s['image']==state['new_image']
assert any(v.get('source')==DATA and v.get('target')=='/data' for v in s['volumes'])
for k,v in environments[name].items():assert str(s['environment'][k])==v
# A frontend rollback must retain the review authorization and schema.
rollback=compose_before
for name in services:rollback=update_service(rollback,name,state['safe_rollback_image'],environments[name])
rb=ROOT+'/deploy/im-admin/compose.knowledge-sections-safe-staged.yaml'
assert not os.path.lexists(rb)
with open(rb,'w') as f:f.write(rollback)
os.chmod(rb,0o600)
rr=json.loads(run(['docker','compose','-f',rb,'config','--format','json']))
for name in services:
assert rr['services'][name]['image']==state['safe_rollback_image']
for k,v in environments[name].items():assert str(rr['services'][name]['environment'][k])==v
def idle_worker():
with sqlite3.connect('file:'+DATA+'/backend.db?mode=ro',uri=True) as c:
for table in ['knowledge_review_job']:
if c.execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?",(table,)).fetchone():
cols=[r[1] for r in c.execute('PRAGMA table_info('+table+')')]
if 'status' in cols and c.execute("SELECT COUNT(*) FROM "+table+" WHERE status IN ('queued','running','processing')").fetchone()[0]:return False
return True
assert idle_worker(),'review transaction active; retry after it finishes'
# Hold new claims without changing job status, content, pending state or ownership.
# The current worker may finish its active batch; queued jobs remain queued.
DRAIN_TRIGGER='iam_deploy_knowledge_sections_20261105'
def release_claim_hold():
with closing(sqlite3.connect(DATA+'/backend.db',timeout=30)) as db:
db.execute('DROP TRIGGER IF EXISTS '+DRAIN_TRIGGER)
def drain_worker():
with closing(sqlite3.connect(DATA+'/backend.db',timeout=30)) as db:
assert db.execute("SELECT 1 FROM sqlite_master WHERE type='trigger' AND name=?",(DRAIN_TRIGGER,)).fetchone() is None
db.execute("CREATE TRIGGER "+DRAIN_TRIGGER+" BEFORE UPDATE OF status ON knowledge_job WHEN NEW.status='running' AND NEW.lease_owner!='' AND CAST(strftime('%s','now') AS INTEGER) < "+str(int(time.time())+1200)+" BEGIN SELECT RAISE(IGNORE); END")
deadline=time.monotonic()+900
while time.monotonic()<deadline:
with closing(sqlite3.connect('file:'+DATA+'/backend.db?mode=ro',uri=True,timeout=10)) as db:
running=db.execute("SELECT COUNT(*) FROM knowledge_job WHERE status='running' AND lease_owner!=''").fetchone()[0]
if not running:
run(['docker','stop','--time','30','im-admin-knowledge-worker-1'],timeout=50)
stopped=json.loads(run(['docker','inspect','im-admin-knowledge-worker-1']))[0]
assert not stopped['State']['Running'],'old worker must stop before combined jobs can be queued'
return
time.sleep(0.5)
raise RuntimeError('current cleaning batch has not drained; no service switch performed')
def gateway_ready(port):
for _ in range(50):
try:
with urllib.request.urlopen('http://127.0.0.1:'+str(port)+'/health',timeout=2) as r:assert r.status==200
return
except Exception:time.sleep(1)
raise RuntimeError('gateway not ready')
bridges={'api':('im-admin-knowledge-sections-api-bridge-20261105',18768,8766),'gateway':('im-admin-knowledge-sections-gateway-bridge-20261105',18771,8770)}
for _,port,_ in bridges.values():
probe=socket.socket()
try:probe.bind(('127.0.0.1',port))
finally:probe.close()
original_vhost=open(VHOST).read()
assert original_vhost.count('http://127.0.0.1:18766')==3 and original_vhost.count('http://127.0.0.1:18770')==1
bridge_vhost=original_vhost.replace('server {','server {\n add_header X-Deploy-Bridge knowledge-sections-20261105 always;',1).replace('http://127.0.0.1:18766','http://127.0.0.1:18768').replace('http://127.0.0.1:18770','http://127.0.0.1:18771')
assert bridge_vhost!=original_vhost and 'X-Deploy-Bridge' in bridge_vhost
import signal,atexit
atexit.register(release_claim_hold)
def controlled_stop(signum, frame):raise KeyboardInterrupt('deployment interrupted')
signal.signal(signal.SIGTERM,controlled_stop)
signal.signal(signal.SIGHUP,controlled_stop)
def perform_activation():
try:
started=[]
observed=[];monitor_stop=threading.Event()
def monitor():
while not monitor_stop.is_set():
for path in ['/api/v2/health','/gateway/health','/system/roles','/knowledge/center','/knowledge/assignments','/knowledge/tasks','/knowledge/search']:
status=0
try:
req=urllib.request.Request('http://127.0.0.1:8369'+path,headers={'Host':'im.zhenyangtang.cn:8369'})
with urllib.request.urlopen(req,timeout=2) as r:status=r.status
except urllib.error.HTTPError as e:status=e.code
except Exception:pass
observed.append({'path':path,'status':status})
monitor_stop.wait(0.5)
try:drain_worker()
except BaseException:
release_claim_hold()
raise
thread=threading.Thread(target=monitor,daemon=True);thread.start()
try:
for service,(name,hostport,innerport) in bridges.items():
cmd=list(old[service]['Config']['Cmd'])
if '--runtime-file' in cmd:cmd[cmd.index('--runtime-file')+1]='/data/group-identity/knowledge-sections-bridge-runtime.json'
run(['docker','run','-d','--name',name,'--restart=no','--read-only','--tmpfs','/tmp:size=64m,mode=1777','--cap-drop=ALL',
'--security-opt','no-new-privileges:true','--env-file',WORK+'/'+service+'-runtime.env','-v',DATA+':/data',
'-p','127.0.0.1:'+str(hostport)+':'+str(innerport),state['new_image']]+cmd)
started.append(name)
health(18768,True);gateway_ready(18771)
assert same_access(json.loads(run(['docker','exec',bridges['api'][0],'python','-c',fingerprint])),baseline)
replace_file(VHOST,bridge_vhost);reload_nginx()
for _ in range(50):
try:
for path in ['/api/v2/health','/gateway/health']:
req=urllib.request.Request('http://127.0.0.1:8369'+path,headers={'Host':'im.zhenyangtang.cn:8369'})
with urllib.request.urlopen(req,timeout=2) as r:assert r.status==200 and r.headers.get('X-Deploy-Bridge')=='knowledge-sections-20261105'
break
except Exception:time.sleep(0.25)
else:raise RuntimeError('public bridge route not ready')
run(['docker','stop','--time','180','im-admin-api-1'],timeout=195)
run(['docker','stop','--time','180','im-admin-gateway-1'],timeout=195)
replace_file(COMPOSE,updated)
run(['docker','compose','-f',COMPOSE,'up','-d','--no-deps','--no-build']+services,timeout=150)
health(18766,True);gateway_ready(18770)
for name in services:
c=json.loads(run(['docker','inspect','im-admin-'+name+'-1']))[0]
assert c['State']['Running'] and c['Config']['Image']==state['new_image']
for item in state['backend']:
actual=run(['docker','exec','im-admin-'+name+'-1','python','-c','import hashlib;print(hashlib.sha256(open('+repr(item['name'])+',"rb").read()).hexdigest())']).strip()
assert actual==item['after_sha256']
for item in state['backend']:
path=ROOT+'/wechat_rpa/'+item['name']
if not os.path.exists(path):
with open(path,'wb'):pass
replace_file(path,open(WORK+'/payload/backend/'+item['name'],'rb').read())
dest=ROOT+'/admin-web/apps/web-antd/dist'
manifest=json.load(open(WORK+'/payload/frontend-manifest.json'))
for item in sorted(manifest['files'],key=lambda x:x['relative_path']=='index.html'):
rel=item['relative_path'];target=os.path.realpath(dest+'/'+rel);assert target.startswith(dest+'/')
os.makedirs(os.path.dirname(target),exist_ok=True)
if not os.path.exists(target):
with open(target,'wb'):pass
replace_file(target,open(WORK+'/payload/frontend/'+rel,'rb').read())
replace_file(VHOST,original_vhost);reload_nginx()
time.sleep(2)
monitor_stop.set();thread.join(timeout=8)
with open(WORK+'/traffic-checks.json','w') as f:json.dump({'samples':len(observed),'failures':[x for x in observed if x['status']!=200]},f,indent=2)
assert observed and all(x['status']==200 for x in observed),'traffic readiness failed; rollback required'
except BaseException:
replace_file(COMPOSE,rollback)
run(['docker','compose','-f',COMPOSE,'up','-d','--no-deps','--no-build']+services,timeout=150)
health(18766,True);gateway_ready(18770)
# Keep the frontend-independent authorization in either version.
for item in state['backend']:
path=ROOT+'/wechat_rpa/'+item['name']
if not os.path.exists(path):
with open(path,'wb'):pass
replace_file(path,open(WORK+'/payload/backend/'+item['name'],'rb').read())
replace_file(VHOST,original_vhost);reload_nginx()
raise
finally:
release_claim_hold()
cleanup=[]
for name in started+[state['candidate']]:
try:run(['docker','stop','--time','35',name],timeout=55);run(['docker','rm',name])
except Exception:cleanup.append(name)
after=json.loads(run(['docker','exec','im-admin-api-1','python','-c',fingerprint]))
monitor_stop.set();thread.join(timeout=8)
with open(WORK+'/traffic-checks.json','w') as f:json.dump({'samples':len(observed),'failures':[x for x in observed if x['status']!=200]},f,indent=2)
result={'deployed':True,'services':services,'image':state['new_image'],'safe_rollback_image':state['safe_rollback_image'],
'initial_access_unchanged':same_access(after,baseline),'cleanup_pending':cleanup,'production_grants_changed_by_agent':False,
'vhost_restored_exactly':sha(VHOST)==sha(WORK+'/before/im-vhost.conf'),
'traffic_samples':len(observed),'traffic_failures':sum(x['status']!=200 for x in observed)}
with open(WORK+'/deployment-result.json','w') as f:json.dump(result,f,indent=2)
print(json.dumps(result))
finally:
signal.signal(signal.SIGTERM,signal.SIG_IGN)
release_claim_hold()
perform_activation()