Files
dpl/scripts/fuc-xb-refresh/node-host.py
T

36 lines
3.6 KiB
Python

#!/usr/bin/env python3
import pathlib,subprocess,json,os,hashlib,time,argparse
BASE=pathlib.Path('/var/www/wwwroot/fuc-xb-acceptance');DIR=BASE/'private/v2-20261105';BIN='/opt/dpl-node/xboard-node'
SHA='55bf71fa9d9f2048d3255ae7c0af929a41897ca7743f6ead34132a6ca4c79043'
def run(a):
p=subprocess.run(a,capture_output=True,text=True,timeout=40);assert p.returncode==0,'QA command failed';return p.stdout.strip()
def free(port):return not run(['ss','-H','-ltn','sport = :'+str(port)])
def baseline():
return {'dpl_pid':run(['systemctl','show','dpl-node','-p','MainPID','--value']),'xrayr_pid':run(['systemctl','show','XrayR','-p','MainPID','--value']),'binary_sha256':hashlib.sha256(pathlib.Path(BIN).read_bytes()).hexdigest(),'unit_sha256':hashlib.sha256(run(['systemctl','cat','dpl-node']).encode()).hexdigest()}
def main():
os.umask(0o077);p=argparse.ArgumentParser();p.add_argument('action',choices=['start','status','stop']);args=p.parse_args();DIR.mkdir(mode=0o700,parents=True,exist_ok=True)
if args.action=='start':
assert free(25443)and free(28766);assert not(DIR/'before.json').exists(),'Already started; inspect before retry'
before=baseline();assert before['binary_sha256']==SHA and before['dpl_pid']=='110612'and before['xrayr_pid']=='107692';(DIR/'before.json').write_text(json.dumps(before))
c=json.loads((DIR/'context.json').read_text());assert c['expected_exit']=='193.111.31.52'and c['port']==25443
cfg={'panel':{'url':'http://127.0.0.1:28766'},'machine':{'machine_id':c['machine_id'],'token_env':'FUC_XB_TEST_MACHINE_TOKEN'},'kernel':{'type':'singbox','config_dir':str(DIR/'kernel')},'log':{'level':'info'},'runtime':{'gomemlimit':'256MiB','gogc':75},'node':{'push_interval':5,'pull_interval':5}}
(DIR/'config.json').write_text(json.dumps(cfg));(DIR/'node-secret.env').write_text('FUC_XB_TEST_MACHINE_TOKEN='+c['machine_token']+'\n')
run(['systemd-run','--quiet','--collect','--unit=fuc-xb-v2-qa-api','--property=RuntimeMaxSec=1800','--property=MemoryMax=96M','/usr/bin/python3',str(pathlib.Path(__file__).with_name('node-bridge.py'))])
for _ in range(30):
if not free(28766):break
time.sleep(.2)
assert not free(28766)
run(['systemd-run','--quiet','--collect','--unit=fuc-xb-v2-qa-node','--property=RuntimeMaxSec=1800','--property=MemoryMax=384M','--property=CPUQuota=30%','--property=EnvironmentFile='+str(DIR/'node-secret.env'),'--working-directory='+str(DIR),BIN,'-c',str(DIR/'config.json')])
print(json.dumps({'started':True,'program':'Xboard-Node','native_v2_machine_interface':True,'binary_matches_dpl_hn':True,'original_unchanged':baseline()==before}))
elif args.action=='status':
before=json.loads((DIR/'before.json').read_text());print(json.dumps({'node_active':run(['systemctl','show','fuc-xb-v2-qa-node','-p','ActiveState','--value']),'api_active':run(['systemctl','show','fuc-xb-v2-qa-api','-p','ActiveState','--value']),'node_listening':not free(25443),'metrics':json.loads((DIR/'bridge-metrics.json').read_text())if(DIR/'bridge-metrics.json').exists()else{},'original_unchanged':before==baseline()}))
else:
for unit in ['fuc-xb-v2-qa-node','fuc-xb-v2-qa-api']:subprocess.run(['systemctl','stop',unit],capture_output=True,timeout=40)
assert free(25443)and free(28766);before=json.loads((DIR/'before.json').read_text());assert before==baseline()
for name in ['context.json','node-secret.env','config.json']:(DIR/name).unlink(missing_ok=True)
# Generated kernel configs contain only synthetic QA credentials; remove them as well.
import shutil
shutil.rmtree(DIR/'kernel',ignore_errors=True)
print(json.dumps({'stopped':True,'temporary_listeners_absent':True,'copied_credentials_removed':True,'original_unchanged':True}))
if __name__=='__main__':main()