feat(nodes): exclude small XrayR hosts and use registered credential fields

This commit is contained in:
hellcat committed 2026-10-20 16:24:46 +08:00
1 parent 4d667f37b2
commit d331f45b0e
9 files changed
+117 -3

No files matched your search

+7
View File
@@ -31,6 +31,13 @@ def snapshot(nodes):
return sorted(result, key=lambda n: n['id'])
def candidate_eligibility(total_memory_bytes, xrayr_installed):
"""Use physical MemTotal, excluding swap and currently free memory. 1 GB = 10**9 bytes."""
if type(total_memory_bytes) is not int or total_memory_bytes <= 0 or type(xrayr_installed) is not bool:
raise ValueError('Unknown candidate memory or XrayR installation')
return not (total_memory_bytes < 1_000_000_000 and xrayr_installed)
def validate_impact(impact):
if impact is None:
return
@@ -23,6 +23,8 @@ def configure(plan,site):
plan.pop('command_profile',None)
plan['adapter']=[str(Path(sys.executable).resolve()),str(base/'command-adapter.py')]
paths=[base/x for x in ('batch.py','site_adapter.py','ssh_pool.py','acceptance.py')]+[Path(site[k]) for k in ('ssh_config','exit_registry')]
if any(h.get('credential_format')=='gemdocs-resource' for h in adapter.pool.config['hosts'].values()):
paths += [Path('/Users/hellcat/Documents/GemDocs/AI版/tools')/name for name in ('credentials.py','resources.py')]
plan['runtime']={'kind':'ssh-site-v1','python':str(Path(sys.executable).resolve()),'paramiko_version':library.__version__,'files':{str(p):hashlib.sha256(p.read_bytes()).hexdigest() for p in paths}}
return plan
+23 -1
View File
@@ -3,7 +3,7 @@ import base64,hashlib,json,os,shlex,threading,time
from pathlib import Path
from types import SimpleNamespace
from ssh_pool import Pool,private_json
from acceptance import audit,digest,snapshot,service_change,provider_state,verification_scope,validate_impact
from acceptance import audit,digest,snapshot,service_change,provider_state,verification_scope,validate_impact,candidate_eligibility
class SiteAdapter:
def __init__(self,plan,directory,pool=None,atomic_writer=None,resource_lock=None):
@@ -69,6 +69,26 @@ class SiteAdapter:
for spec in source['nodes']:
if spec['sni'] not in self.exit_registry:raise ValueError('Missing old-node exit expectation')
def capacity_gate(self,node,execute):
script=r"""from pathlib import Path
import json,shutil,subprocess,re
text=Path('/proc/meminfo').read_text()
match=re.search(r'^MemTotal:\s+(\d+) kB$',text,re.M)
if not match:raise RuntimeError('memory_unknown')
units=[]
for unit in ['XrayR','xrayr']:
data=subprocess.check_output(['systemctl','show',unit,'-p','LoadState'],text=True).strip()
if data not in ['LoadState=loaded','LoadState=not-found','LoadState=masked','LoadState=bad-setting','LoadState=error','LoadState=merged']:raise RuntimeError('xrayr_installation_unknown')
if data!='LoadState=not-found':units.append(unit)
paths=[p for p in ['/usr/local/XrayR/XrayR','/usr/local/bin/XrayR','/usr/bin/XrayR','/etc/XrayR/config.yml'] if Path(p).exists()]
installed=bool(units or paths or shutil.which('XrayR') or shutil.which('xrayr'))
print(json.dumps({'total_memory_bytes':int(match.group(1))*1024,'xrayr_installed':installed}))
"""
observation=json.loads(execute(node,"python3 - <<'DPLCAPACITY'\n"+script+"\nDPLCAPACITY"))
if not candidate_eligibility(observation.get('total_memory_bytes'),observation.get('xrayr_installed')):
raise RuntimeError('candidate_low_memory_with_xrayr')
return observation
def certificate_gate(self,n,node,read,execute):
owner=read(node,'/etc/dpl-node/batch-owner.json')
owned=bool(owner and json.loads(owner).get('request_sha256')==n['request_sha256'])
@@ -325,6 +345,8 @@ print(json.dumps({'status':code,'cache':headers.get('cf-cache-status'),'control'
return gateway_py(gateway,'Q='+repr(Q)+';SUB='+repr(ctx['subscription_url'])+'\n'+script)
def phase_impl(panel,gateway,node):
if a.phase=='preflight':
capacity=self.capacity_gate(node,execute)
self.atomic_json(self.directory/(key+'-capacity.json'),dict(capacity,request_sha256=sha,checked_at=int(time.time())))
with self.lock, self.resource_lock(self.directory.parent,ctx['panel_vps']+':panel'):
execute(panel,'install -d -m 700 '+ctx['batch_backup_root'])
execute(panel,'test "$(git -C '+OPS+' rev-parse HEAD)" = '+ctx['provision_commit']+' && test -z "$(git -C '+OPS+' status --porcelain)"')
+9
View File
@@ -19,6 +19,15 @@ def private_json(path):
def credentials(spec):
if spec.get('credential_format') == 'gemdocs-resource':
import importlib.util,sys
root=Path('/Users/hellcat/Documents/GemDocs/AI版')
resource=spec.get('credential_resource','')
if not re.fullmatch(r'VPS-[0-9]{3}',resource):raise ValueError('Exact GemDocs VPS resource required')
if str(root/'tools') not in sys.path:sys.path.insert(0,str(root/'tools'))
module_spec=importlib.util.spec_from_file_location('gemdocs_credentials',root/'tools/credentials.py')
module=importlib.util.module_from_spec(module_spec);module_spec.loader.exec_module(module)
return {'password':module.get_field(root,resource,'password')}
if not spec.get('credential_ref'):return {}
if spec.get('credential_format','json')=='json':return private_json(spec['credential_ref'])
if spec['credential_format']=='codocs-password':