76 lines
4.7 KiB
PHP
76 lines
4.7 KiB
PHP
<?php
|
|
|
|
use App\Services\StandaloneRuntime;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
|
$installed = in_array('--installed-read-only', $argv, true);
|
|
if (! in_array('--read-only', $argv, true) && ! $installed) {
|
|
throw new LogicException('READ_ONLY_ACTIVATION_PREFLIGHT_REQUIRED');
|
|
}
|
|
$root = dirname(__DIR__);
|
|
$plan = json_decode(file_get_contents($root.'/docs/proofs/compras-standalone-pilot-activation-proposal.json'), true, 512, JSON_THROW_ON_ERROR);
|
|
$sourcePath = 'C:/laragon/data/sgf-tempo/secrets/payables-demo-compras.json';
|
|
$source = json_decode(file_get_contents($sourcePath), true, 32, JSON_THROW_ON_ERROR);
|
|
$expectedSettings = $plan['active_settings_sha256'];
|
|
$expectedManifest = $plan['frontend']['previous_manifest_sha256'];
|
|
if ($installed) {
|
|
$activation = json_decode(file_get_contents($root.'/docs/proofs/compras-standalone-pilot-activation.json'), true, 32, JSON_THROW_ON_ERROR);
|
|
$previous = json_decode(file_get_contents($activation['backup_directory'].'/settings.before.private.json'), true, 32, JSON_THROW_ON_ERROR);
|
|
if ($activation['state'] !== 'passed' || $source !== [...$previous, ...$plan['proposed_runtime_settings']]) {
|
|
throw new LogicException('ACTIVATION_POSTCHECK_SETTINGS_DIFFER');
|
|
}
|
|
$expectedSettings = hash_file('sha256', $sourcePath);
|
|
$expectedManifest = $plan['frontend']['manifest_sha256'];
|
|
}
|
|
if (! hash_equals($expectedSettings, hash_file('sha256', $sourcePath))
|
|
|| $source['APP_ENV'] !== 'payable-testing' || $source['DB_HOST'] !== '127.0.0.1'
|
|
|| realpath($source['HOST_ROOT']) !== realpath($root)
|
|
|| ! preg_match('/^compras_receipts_test_([a-f0-9]{12})$/D', $source['DB_DATABASE'], $match)
|
|
|| $source['DB_USERNAME'] !== 'payable_qa_'.$match[1]
|
|
|| trim($source['PAYABLE_SOURCE_INSTANCE'] ?? '') === '') {
|
|
throw new LogicException('ACTIVATION_PREFLIGHT_SOURCE_CHANGED');
|
|
}
|
|
$assets = $root.'/'.$plan['frontend']['candidate_root'];
|
|
if (! hash_equals($plan['frontend']['manifest_sha256'], hash_file('sha256', $assets.'/manifest.json'))
|
|
|| ! hash_equals($expectedManifest, hash_file('sha256', $root.'/public/build/manifest.json'))) {
|
|
throw new LogicException('ACTIVATION_PREFLIGHT_MANIFEST_CHANGED');
|
|
}
|
|
foreach ($plan['frontend']['files'] as $file => $hash) {
|
|
if (! str_starts_with($file, 'assets/') || str_contains($file, '..') || ! hash_equals($hash, hash_file('sha256', $assets.'/'.$file))
|
|
|| (is_file($root.'/public/build/'.$file) && ! hash_equals($hash, hash_file('sha256', $root.'/public/build/'.$file)))) {
|
|
throw new LogicException('ACTIVATION_PREFLIGHT_ASSET_MISMATCH');
|
|
}
|
|
}
|
|
$environment = [...$source, ...$plan['proposed_runtime_settings'], 'APP_ENV' => 'pilot-readonly', 'APP_DEBUG' => 'false',
|
|
'DB_CONNECTION' => 'pgsql', 'DB_URL' => '', 'PGOPTIONS' => '-c default_transaction_read_only=on',
|
|
'CACHE_STORE' => 'array', 'SESSION_DRIVER' => 'array', 'QUEUE_CONNECTION' => 'null', 'MAIL_MAILER' => 'array',
|
|
'APP_CONFIG_CACHE' => $root.'/storage/framework/absent-pilot-readonly-config.php', 'LOG_CHANNEL' => 'stderr'];
|
|
foreach ($environment as $key => $value) {
|
|
putenv($key.'='.$value);
|
|
$_ENV[$key] = $_SERVER[$key] = $value;
|
|
}
|
|
$app = require $root.'/bootstrap/app.php';
|
|
$app->make(Kernel::class)->bootstrap();
|
|
$db = DB::connection();
|
|
if ($db->selectOne('SHOW default_transaction_read_only')->default_transaction_read_only !== 'on') {
|
|
throw new LogicException('ACTIVATION_PREFLIGHT_DATABASE_NOT_READ_ONLY');
|
|
}
|
|
$runtime = app(StandaloneRuntime::class);
|
|
$runtime->configure($db);
|
|
$runtime->assertLoadedPackages();
|
|
if (! $runtime->enabled() || count(array_filter(StandaloneRuntime::FEATURES, fn ($name) => $runtime->requested($name))) !== 24) {
|
|
throw new LogicException('ACTIVATION_PREFLIGHT_CAPABILITIES_INCOMPLETE');
|
|
}
|
|
if (! hash_equals($expectedSettings, hash_file('sha256', $sourcePath))) {
|
|
throw new LogicException('ACTIVATION_PREFLIGHT_SETTINGS_CHANGED');
|
|
}
|
|
$result = ['date' => gmdate('c'), 'state' => 'passed', 'database_enforced_read_only' => true,
|
|
'all_24_capabilities_start_with_pinned_installed_packages' => true, 'source_instance_preserved' => true,
|
|
'frontend_assets_verified' => count($plan['frontend']['files']), 'active_settings_changed' => false,
|
|
'mode' => $installed ? 'installed_configuration_read_only_postcheck' : 'prospective_read_only_preflight',
|
|
'assets_published' => $installed, 'new_grants_or_business_commands' => false];
|
|
file_put_contents($root.'/docs/proofs/'.($installed ? 'compras-standalone-pilot-activation-check.json' : 'compras-standalone-pilot-activation-preflight.json'), json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
|
|
echo json_encode($result, JSON_THROW_ON_ERROR).PHP_EOL;
|