Files

14 lines
3.1 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('Command failed exit '.$c);return $o;}
$root='/mnt/nvme-storage/appdata/librechat';$xp='/boot/config/plugins/dockerMan/templates-user/my-LibreChat.xml';$x=simplexml_load_file($xp);$back=$root.'/install-backups/media-'.gmdate('Ymd-His');mkdir($back,0700,true);copy($xp,$back.'/my-LibreChat.xml');chmod($back.'/my-LibreChat.xml',0600);copy($root.'/librechat.yaml',$back.'/librechat.yaml');
if(!is_dir($root.'/compat'))mkdir($root.'/compat',0755,true);copy('/tmp/OpenAIImageTools.js',$root.'/compat/OpenAIImageTools.js');chmod($root.'/compat/OpenAIImageTools.js',0644);
run(['docker','cp','/tmp/configure-media.js','LibreChat:/tmp/configure-media.js']);
echo run(['docker','exec','LibreChat','node','-e','eval(require("fs").readFileSync("/tmp/configure-media.js","utf8"))']);
run(['docker','cp','LibreChat:/tmp/librechat-media.yaml',$root.'/librechat.yaml']);
$key=null;foreach($x->Config as$c)if((string)$c['Target']==='ATHENA_API_KEY')$key=(string)$c;if(!$key)throw new Exception('Existing Athena key missing');
$env=['IMAGE_GEN_OAI_API_KEY'=>$key,'IMAGE_GEN_OAI_BASEURL'=>'http://192.168.1.212:8120/v1','IMAGE_GEN_OAI_MODEL'=>'athena-image','IMAGE_GEN_OAI_DESCRIPTION'=>'Generate one image using the local Athena image generator. Image dimensions and settings follow the Athena profile.','IMAGE_GEN_OAI_PROMPT_DESCRIPTION'=>'Describe the requested image.'];
foreach($env as$k=>$v){$found=false;foreach($x->Config as$c)if((string)$c['Target']===$k){$c[0]=$v;$found=true;}if(!$found){$c=$x->addChild('Config',htmlspecialchars($v,ENT_XML1));foreach(['Name'=>$k,'Target'=>$k,'Type'=>'Variable','Mode'=>'','Display'=>'always','Mask'=>strpos($k,'API_KEY')!==false?'true':'false']as$a=>$z)$c->addAttribute($a,$z);}}
$target='/app/api/app/clients/tools/structured/OpenAIImageTools.js';$found=false;foreach($x->Config as$c)if((string)$c['Target']===$target)$found=true;if(!$found){$c=$x->addChild('Config',$root.'/compat/OpenAIImageTools.js');foreach(['Name'=>'Athena Image API compatibility','Target'=>$target,'Type'=>'Path','Mode'=>'ro','Display'=>'always']as$a=>$z)$c->addAttribute($a,$z);}$x->asXML($xp);chmod($xp,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(['media_configured'=>true,'backup'=>$back])."\n";