Files
dj/integration-work/install_gpu3_vdn.py
T

42 lines
3.2 KiB
Python

import pathlib,json,hashlib,subprocess,sys,datetime,shutil
sys.path.insert(0,'/home/ps/huobao-gpu-selection')
from docker_worker_ops import api,inspect,clone,set_env,replace_bind,write_json
root=pathlib.Path('/home/ps/huobao-gpu-selection');work=root/'gpu3-vdn';name='h3-caption-gpu3-canary-8a5f'
assert not work.exists(),'GPU3 preparation already exists; inspect rather than overwrite'
work.mkdir(mode=0o700);source=inspect(name);write_json(work/'source.inspect.json',source)
control=pathlib.Path(next(m['Source'] for m in source['Mounts'] if m['Destination']=='/run/h3-control'))/'control.json'
original=control.read_bytes();(work/'original-control.json').write_bytes(original)
value=json.loads(original);value['maintenance']=True;control.write_text(json.dumps(value))
code='''import asyncio,httpx,json,app
async def main():
p=app.providers()['gpu3']
async with httpx.AsyncClient(timeout=40) as c:
q=(await c.get(p.base_url+'/queue',headers=app.provider_headers(p))).json()
assert not q.get('queue_running') and not q.get('queue_pending'),'GPU3 still has jobs; no restart allowed'
r=await c.get(p.base_url+'/history',headers=app.provider_headers(p));r.raise_for_status();history=r.json()
assert all(v.get('status',{}).get('status_str') in ('success','error') for v in history.values())
q=(await c.get(p.base_url+'/queue',headers=app.provider_headers(p))).json()
assert not q.get('queue_running') and not q.get('queue_pending'),'GPU3 changed while snapshotting'
print(json.dumps(history))
asyncio.run(main())
'''
try:
history=json.loads(subprocess.check_output(['docker','exec','huobao-vdn-normalizer','python','-c',code],text=True))
except Exception:
control.write_bytes(original);raise
hist=work/'history.json';write_json(hist,{'schema':1,'queue_empty':True,'source_container_id':source['Id'],'history':history})
assert hist.stat().st_size<256*1024**2
gpu2=inspect('comfyui');nodes=pathlib.Path(next(m['Source'] for m in gpu2['Mounts'] if m['Destination']=='/app/custom_nodes'))
plugin=work/'ComfyUI-VDN-H3';shutil.copytree(nodes/'ComfyUI-VDN-H3',plugin,ignore=shutil.ignore_patterns('__pycache__','.git'))
assert (plugin/'__init__.py').is_file()
cfg=clone(source,name);replace_bind(cfg,'/run/h3-history.json',hist)
cfg['HostConfig']['Binds'].append(str(plugin)+':/app/custom_nodes/ComfyUI-VDN-H3:ro')
set_env(cfg,'H3_HISTORY_SNAPSHOT_SHA256',hashlib.sha256(hist.read_bytes()).hexdigest());set_env(cfg,'H3_HISTORY_SOURCE_CONTAINER_ID',source['Id'])
cfg['NetworkingConfig']={'EndpointsConfig':{network:{'Aliases':[a for a in info.get('Aliases',[]) if a not in (source['Id'],source['Id'][:12])]} for network,info in source['NetworkSettings']['Networks'].items()}}
write_json(work/'create.json',cfg)
oldname=name+'-before-vdn-'+datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
api('POST','/containers/'+name+'/stop?t=30');api('POST','/containers/'+name+'/rename?name='+oldname)
created=api('POST','/containers/create?name='+name,cfg);write_json(work/'created.json',{'id':created['Id'],'retained':oldname,'history_count':len(history),'control':str(control)})
api('POST','/containers/'+name+'/start')
print('GPU3 VDN added with original networks and tokens; verified terminal history saved:',len(history),'retained:',oldname)