Files
Athena-Deck/integrations/librechat-unraid/mcp/transfer-keys.php
T

10 lines
2.3 KiB
PHP

<?php
function run($a){$p=proc_open($a,[1=>['pipe','w'],2=>['pipe','w']],$ps);$o=stream_get_contents($ps[1]);$e=stream_get_contents($ps[2]);foreach($ps as$s)fclose($s);$c=proc_close($p);if($c)throw new Exception('Operation failed; output suppressed');return $o;}
$root='/mnt/nvme-storage/appdata/librechat';$path='/boot/config/plugins/dockerMan/templates-user/my-LibreChat.xml';$x=simplexml_load_file($path);
$raw=run(['docker','exec','Open-WebUI','python','-c',"import sqlite3;c=sqlite3.connect('file:/app/backend/data/webui.db?mode=ro',uri=True);print(c.execute('select value from config where key=?',('tool_server.connections',)).fetchone()[0])"]);$rows=json_decode($raw,true);$updated=[];
$back=$root.'/install-backups/mcp-keys-'.gmdate('Ymd-His');mkdir($back,0700,true);copy($path,$back.'/my-LibreChat.xml');chmod($back.'/my-LibreChat.xml',0600);
foreach($rows as$s){if(($s['type']??'')!=='mcp'||($s['auth_type']??'')!=='bearer')continue;$id=$s['info']['id'];$key=$s['key']??'';$var='MCP_KEY_'.strtoupper(preg_replace('/[^a-zA-Z0-9]/','_',$id));$found=false;foreach($x->Config as$c)if((string)$c['Type']==='Variable'&&(string)$c['Target']===$var){if(!$key)throw new Exception('Source key missing for '.$id);$c[0]=$key;$found=true;}if(!$found)throw new Exception('Target missing for '.$id);$updated[]=$id;}
if(count($updated)!==7)throw new Exception('Unexpected key count');$x->asXML($path);chmod($path,0600);
$a=['docker','create','--name','LibreChat','--network','bridge','--restart','unless-stopped','--pids-limit','2048','-l','net.unraid.docker.managed=dockerman'];foreach(['WebUI'=>'net.unraid.docker.webui','Icon'=>'net.unraid.docker.icon']as$k=>$v)if((string)$x->$k!='')array_push($a,'-l',$v.'='.(string)$x->$k);foreach($x->Config as$c){$v=(string)$c;$target=(string)$c['Target'];$type=(string)$c['Type'];if($type==='Variable')array_push($a,'-e',$target.'='.$v);elseif($type==='Port')array_push($a,'-p',$v.':'.$target.'/'.(string)$c['Mode']);elseif($type==='Path')array_push($a,'-v',$v.':'.$target.':'.(string)$c['Mode']);else throw new Exception('Unknown config type');}$a[]=(string)$x->Repository;
run(['docker','stop','--time','30','LibreChat']);run(['docker','rm','LibreChat']);run($a);run(['docker','start','LibreChat']);echo json_encode(['transferred'=>count($updated),'servers'=>$updated,'backup'=>$back,'key_values_printed'=>false])."\n";