Files
Athena-Deck/deploy/prepare_xvc.py

48 lines
3.7 KiB
Python

"""Prepare X-VC without starting services; merge its inactive profile only after Deck stops."""
import json,os,secrets,subprocess,sys,time,uuid
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parent.parent))
from catalog import Catalog
from xvc_runtime import XVCRuntime
def prepare(state):
state=Path(state).resolve();runtime=XVCRuntime(state/'xvc-runtime',None)
python,source=runtime.paths()
# The venv interpreter targets Deck's bundled Python, which lives inside its image.
if not (python.exists() or python.is_symlink()) or not (source/'models/codec/sac/model.py').is_file():raise ValueError('X-VC-Laufzeit zuerst installieren.')
ident=runtime.ensure_library();pending=state/'xvc-profile-pending.json'
if pending.exists():
row=json.loads(pending.read_text())
if row.get('model_id')!=ident or row.get('api_id')!='xvc-studio':raise ValueError('Abweichendes vorbereitetes X-VC-Profil prüfen.')
else:
row=dict(id=uuid.uuid4().hex,revision=1,name='X-VC Studio',api_id='xvc-studio',api_aliases=[],kind='voice',model_id=ident,parameters={},components={},presets=[],prompt_enhancer=None,updated_at=time.time())
pending.write_text(json.dumps(row,indent=2));os.chown(pending,65534,65534)
root=state/'xvc-data';root.mkdir(parents=True,exist_ok=True);os.chown(root,65534,65534)
settings=root/'settings.json'
if not settings.exists():settings.write_text(json.dumps({'device':'5080'}));os.chown(settings,65534,65534)
key=root/'bridge-key'
if not key.exists():key.write_text(secrets.token_urlsafe(48));key.chmod(0o600);os.chown(key,65534,65534)
name='athena-xvc-web';found=subprocess.run(['docker','inspect',name],capture_output=True,text=True)
if found.returncode==0:
row=json.loads(found.stdout)[0];labels=row['Config'].get('Labels') or {}
if labels.get('io.athena-deck.managed')!='true' or labels.get('io.athena-deck.application')!='xvc-web':raise ValueError('Containername anderweitig belegt.')
if row['State']['Running']:raise ValueError('Vorhandene X-VC-UI läuft bereits; Vorbereitung verändert sie nicht.')
else:
subprocess.run(['docker','create','--name',name,'--network','host','--restart','unless-stopped','--label','io.athena-deck.managed=true','--label','io.athena-deck.role=application','--label','io.athena-deck.application=xvc-web','--read-only','--cap-drop','ALL','--security-opt','no-new-privileges:true','--pids-limit','128','--memory','512m','--cpus','1','--tmpfs','/tmp:rw,noexec,nosuid,size=128m,uid=65534,gid=65534','--mount','type=bind,src='+str(key)+',dst=/run/secrets/xvc-bridge,readonly','athena-xvc-web:0.1.0-deck1'],check=True,stdout=subprocess.DEVNULL)
print('X-VC: inaktives Profil vorbereitet, UI-Container gestoppt. Keine Endpunktfreigabe geändert.')
def apply_profile(state,deck='athena-deck-dev'):
from profiles import Profiles
result=subprocess.run(['docker','inspect',deck],capture_output=True,text=True,check=True)
if json.loads(result.stdout)[0]['State']['Running']:raise ValueError('Deck läuft: Profil erst im späteren Wartungsfenster übernehmen.')
state=Path(state);row=json.loads((state/'xvc-profile-pending.json').read_text());profiles=Profiles(state/'profiles.json',Catalog(state/'models'))
existing=next((p for p in profiles.rows if p.get('api_id')==row['api_id']),None)
if existing:
if existing['model_id']!=row['model_id']:raise ValueError('X-VC API-ID bereits anders belegt.')
else:profiles.save(dict(id=None,revision=0,name=row['name'],api_id=row['api_id'],kind='voice',model_id=row['model_id'],parameters={}))
os.chown(state/'profiles.json',65534,65534)
print('Inaktives X-VC-Profil übernommen; Betriebsart und Freigaben unverändert.')
if __name__=='__main__':
if '--apply-profile' in sys.argv:apply_profile(sys.argv[1])
else:prepare(sys.argv[1])