41 lines
6.3 KiB
PHP
41 lines
6.3 KiB
PHP
<?php
|
|
ini_set('zend.exception_ignore_args','1');
|
|
set_exception_handler(function(Throwable $e){fwrite(STDERR,'Restored clone boot stopped: '.$e::class.' line '.$e->getLine().PHP_EOL);exit(1);});
|
|
$root=dirname(__DIR__,2);$p=json_decode(file_get_contents(__DIR__.'/FRESH-BACKUP.json'),true,512,JSON_THROW_ON_ERROR);
|
|
if(!$p['restore_verified']||!$p['files_verified']||$p['candidate']!=='parques-operacionais-QA-6aacf653a98e')throw new RuntimeException('Exact restored backup required');
|
|
$clone=$p['private_directory'].'/application-boot-2';
|
|
if(is_dir($clone)||!mkdir($clone,0700,true))throw new RuntimeException('Fresh private clone required');
|
|
$backup=$p['private_directory'].'/files';
|
|
foreach(['app','config','routes','resources','database','bootstrap','public/build']as$folder){
|
|
$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($backup.'/'.$folder,FilesystemIterator::SKIP_DOTS));
|
|
foreach($it as$f)if($f->isFile()){$rel=str_replace('\\','/',substr($f->getPathname(),strlen($backup)+1));$dest=$clone.'/'.$rel;if(!is_dir(dirname($dest)))mkdir(dirname($dest),0700,true);if(!copy($f->getPathname(),$dest)||hash_file('sha256',$dest)!==hash_file('sha256',$f->getPathname()))throw new RuntimeException('Restored source copy mismatch');}
|
|
}
|
|
foreach(['.env','artisan','composer.json','composer.lock']as$n)if(!copy($backup.'/'.$n,$clone.'/'.$n))throw new RuntimeException('Runtime file copy');
|
|
foreach(['storage/framework/views','storage/framework/cache/data','storage/framework/sessions','storage/logs','storage/app/private','storage/app/public','bootstrap/cache']as$d)if(!is_dir($clone.'/'.$d))mkdir($clone.'/'.$d,0700,true);
|
|
$loader=require $root.'/vendor/autoload.php';$loader->setPsr4('App\\',$clone.'/app');
|
|
$map=[];foreach($loader->getClassMap()as$class=>$file)if(str_starts_with($class,'App\\')){$path=$clone.'/app/'.str_replace('\\','/',substr($class,4)).'.php';if(is_file($path))$map[$class]=$path;}$loader->addClassMap($map);
|
|
$values=Dotenv\Dotenv::parse(file_get_contents($clone.'/.env'));
|
|
foreach(['APP_BASE_PATH'=>$clone,'APP_ENV'=>'testing','APP_URL'=>'https://parks-restore.test','DB_CONNECTION'=>'pgsql','DB_HOST'=>'127.0.0.1','DB_PORT'=>'55441','DB_DATABASE'=>$p['restore_target']['database'],'DB_USERNAME'=>'postgres','DB_PASSWORD'=>'','DB_URL'=>'','APP_CONFIG_CACHE'=>$clone.'/bootstrap/cache/config.php','APP_ROUTES_CACHE'=>$clone.'/bootstrap/cache/routes-v7.php','VIEW_COMPILED_PATH'=>$clone.'/storage/framework/views','SESSION_DRIVER'=>'array','CACHE_STORE'=>'array','QUEUE_CONNECTION'=>'sync','MAIL_MAILER'=>'log','INERTIA_SSR_ENABLED'=>'false','SUITECRM_ENABLED'=>'false','INVESTMOZ_AI_ENABLED'=>'false','PORTAL_NOTIFICATIONS_ENABLED'=>'false','PGOPTIONS'=>'-c default_transaction_read_only=on']as$k=>$v)$values[$k]=$v;
|
|
foreach($values as$k=>$v)if($v!==null){putenv($k.'='.$v);$_ENV[$k]=$_SERVER[$k]=$v;}
|
|
$app=require $clone.'/bootstrap/app.php';$app->usePublicPath($clone.'/public');$app->useStoragePath($clone.'/storage');
|
|
(new Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables)->bootstrap($app);(new Illuminate\Foundation\Bootstrap\LoadConfiguration)->bootstrap($app);
|
|
$c=config('database.connections.'.config('database.default'));
|
|
if($c['host']!=='127.0.0.1'||(int)$c['port']!==55441||$c['database']!==$p['restore_target']['database']||!preg_match('/^parks_before_\d{8}_\d{6}_test$/D',$c['database'])||!empty($c['url']))throw new RuntimeException('Isolated restored clone only');
|
|
$app->afterBootstrapping(Illuminate\Foundation\Bootstrap\RegisterProviders::class,function(){
|
|
$c=config('database.connections.'.config('database.default'));if($c['host']!=='127.0.0.1'||(int)$c['port']!==55441||!preg_match('/^parks_before_\d{8}_\d{6}_test$/D',$c['database']))throw new RuntimeException('Read-only setup must target restored clone');
|
|
config(['session.driver'=>'array','cache.default'=>'array','inertia.ssr.enabled'=>false,'suitecrm.enabled'=>false,'investmoz_ai.enabled'=>false,'portal.notifications_enabled'=>false]);
|
|
Illuminate\Support\Facades\DB::connection()->statement('SET default_transaction_read_only = on');
|
|
});
|
|
$kernel=$app->make(Illuminate\Contracts\Http\Kernel::class);$kernel->bootstrap();
|
|
config(['inertia.ssr.enabled'=>false,'suitecrm.enabled'=>false,'investmoz_ai.enabled'=>false,'portal.notifications_enabled'=>false]);
|
|
$db=Illuminate\Support\Facades\DB::connection();if($db->selectOne('SHOW default_transaction_read_only')->default_transaction_read_only!=='on')throw new RuntimeException('Read-only clone required');
|
|
$actual=(array)$db->selectOne('SELECT current_database() AS database,inet_server_port() AS port');
|
|
if($actual['database']!==$p['restore_target']['database']||(int)$actual['port']!==55441)throw new RuntimeException('Actual clone identity');
|
|
$db->beginTransaction();$db->statement('SET TRANSACTION READ ONLY');$routes=[];
|
|
try{
|
|
foreach(['/up','/login','/zonas','/api/v2/investment/zones']as$path){$request=Illuminate\Http\Request::create('https://parks-restore.test'.$path,'GET');$response=$kernel->handle($request);$body=$response->getContent();file_put_contents($clone.'/response-'.str_replace('/','_',trim($path,'/')).'.html',$body);if($response->getStatusCode()!==200)throw new RuntimeException('Restored clone route failed');$routes[]=['method'=>'GET','path'=>$path,'status'=>200,'body_bytes'=>strlen($body),'body_sha256'=>hash('sha256',$body)];$kernel->terminate($request,$response);}
|
|
$origin=(new ReflectionClass(App\Models\Opportunity::class))->getFileName();if(!str_starts_with(str_replace('\\','/',$origin),$clone.'/app/'))throw new RuntimeException('Application source must be the private restored clone');
|
|
$proof=['at'=>gmdate('c'),'candidate'=>$p['candidate'],'restore_target'=>$actual,'application_base_is_private_clone'=>true,'source_and_build_from_fresh_backup'=>true,'shared_vendor_dependencies_unchanged'=>true,'DB_read_only'=>true,'kernel_bootstrap'=>'passed','HTTP_kernel_routes'=>$routes,'view_rendering_and_Vite_manifest'=>'passed','session_and_cache'=>'array in clone only','external_connectors_disabled'=>true,'database_changes'=>0,'active_changes'=>0,'accepted_QA_tests_repeated'=>false,'real_web_server_TLS_worker_boot_test'=>false];
|
|
if(is_file(__DIR__.'/ARRANQUE-CLONE-RESTAURADO.json'))throw new RuntimeException('Preserve clone proof');file_put_contents(__DIR__.'/ARRANQUE-CLONE-RESTAURADO.json',json_encode($proof,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR).PHP_EOL);echo json_encode($proof,JSON_UNESCAPED_UNICODE).PHP_EOL;
|
|
}finally{$db->rollBack();}
|