import os,json,subprocess,shutil,time,re,hashlib,socket,urllib.request,sqlite3,threading,urllib.error ROOT='/opt/im-admin' WORK=ROOT+'/deploy/basic-role-edit-20261104' 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 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.load(open(WORK+'/before-directory-fingerprints.json')) assert json.loads(run(['docker','exec','im-admin-api-1','python','-c',fingerprint]))==baseline 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:updated=update_service(updated,name,state['new_image'],environments[name]) staged=ROOT+'/deploy/im-admin/compose.basic-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 # Even a frontend rollback must retain the new non-clearing RBAC policy. rollback=compose_before for name in services:rollback=update_service(rollback,name,state['safe_rollback_image'],environments[name]) rb=ROOT+'/deploy/im-admin/compose.basic-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_job','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(),'knowledge jobs active; retry after current work finishes' 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-basic-api-bridge-20261104',18768,8766),'gateway':('im-admin-basic-gateway-bridge-20261104',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 basic-role-edit-20261104 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 started=[] observed=[];monitor_stop=threading.Event() def monitor(): while not monitor_stop.is_set(): for path in ['/api/v2/health','/gateway/health','/system/roles']: 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) 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/basic-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 json.loads(run(['docker','exec',bridges['api'][0],'python','-c',fingerprint]))==baseline assert idle_worker() run(['docker','stop','--time','30','im-admin-knowledge-worker-1'],timeout=50) 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')=='basic-role-edit-20261104' break except Exception:time.sleep(0.25) else:raise RuntimeError('public bridge route not ready') time.sleep(2) 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'] actual=run(['docker','exec','im-admin-'+name+'-1','python','-c','import hashlib;print(hashlib.sha256(open("rbac.py","rb").read()).hexdigest())']).strip() assert actual==state['rbac_after_sha256'] replace_file(ROOT+'/wechat_rpa/rbac.py',open(WORK+'/payload/backend/rbac.py','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 Exception: 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 configured basic permissions intact; old seed must not be reintroduced. replace_file(ROOT+'/wechat_rpa/rbac.py',open(WORK+'/payload/backend/rbac.py','rb').read()) replace_file(VHOST,original_vhost);reload_nginx() raise 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_directory_unchanged':after==baseline,'cleanup_pending':cleanup,'basic_permissions_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))