Files
compras/scripts/check-pilot-operations.php
T
nala 96b30368bc
tests / ci (push) Canceled after 0s
commit compras5 2911
2026-11-29 19:49:19 +02:00

78 lines
4.5 KiB
PHP

<?php
use App\Services\OperationalHealth;
use App\Services\ReplenishmentTaskDefinition;
use App\Services\StandaloneRuntime;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;
require dirname(__DIR__).'/vendor/autoload.php';
if (! in_array(count($argv), [2, 3], true) || $argv[1] !== '--read-only'
|| (count($argv) === 3 && $argv[2] !== '--installation-conservation')) {
throw new LogicException('Explicit read-only operational inspection required.');
}
$root = dirname(__DIR__);
$path = 'C:/laragon/data/sgf-tempo/secrets/payables-demo-compras.json';
$bytes = file_get_contents($path);
$settings = json_decode($bytes, true, 32, JSON_THROW_ON_ERROR);
if ($settings['APP_ENV'] !== 'payable-testing' || $settings['DB_HOST'] !== '127.0.0.1'
|| (string) $settings['DB_PORT'] !== '5432' || realpath($settings['HOST_ROOT']) !== realpath($root)
|| ! preg_match('/^compras_receipts_test_([a-f0-9]{12})$/D', $settings['DB_DATABASE'], $match)
|| $settings['DB_USERNAME'] !== 'payable_qa_'.$match[1]) {
throw new LogicException('Existing Compras pilot required.');
}
foreach ([...$settings, 'DB_CONNECTION' => 'pgsql', 'DB_URL' => '', 'PGOPTIONS' => '-c default_transaction_read_only=on',
'CACHE_STORE' => 'array', 'SESSION_DRIVER' => 'array', 'QUEUE_CONNECTION' => 'null', 'MAIL_MAILER' => 'array',
'LOG_CHANNEL' => 'stderr', 'APP_DEBUG' => 'false', 'APP_CONFIG_CACHE' => $root.'/storage/framework/absent-operations-check.php'] as $key => $value) {
putenv($key.'='.$value);
$_ENV[$key] = $_SERVER[$key] = $value;
}
$app = require $root.'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
if (DB::selectOne('SHOW default_transaction_read_only')->default_transaction_read_only !== 'on') {
throw new LogicException('Enforced database read-only mode required.');
}
app(StandaloneRuntime::class)->assertLoadedPackages();
$result = app(OperationalHealth::class)->inspect();
$taskProof = $root.'/docs/proofs/replenishment-task-installation.json';
$taskVerification = null;
if (is_file($taskProof)) {
$task = json_decode(file_get_contents($taskProof), true, 32, JSON_THROW_ON_ERROR);
$directory = str_replace('\\', '/', $task['private_backup_directory']);
if (! preg_match('~^C:/laragon/data/compras/tasks/replenishment-[0-9]{8}-[0-9]{6}-[a-f0-9]{12}$~D', $directory)
|| $task['task_name'] !== 'Compras-Pilot-Replenishment') {
throw new LogicException('Owned replenishment task backup required.');
}
$native = new Process([getenv('SystemRoot').'/System32/schtasks.exe', '/Query', '/TN', $task['task_name'], '/XML'], $root, timeout: 15);
$native->run();
if (! $native->isSuccessful()) {
throw new LogicException('Native replenishment task unavailable.');
}
ReplenishmentTaskDefinition::assertMatches(file_get_contents($directory.'/task.private.xml'), $native->getOutput());
$baseline = count($argv) === 3
? json_decode(file_get_contents($directory.'/baseline.private.json'), true, 32, JSON_THROW_ON_ERROR) : [];
$changed = [];
foreach ($baseline as $table => $before) {
$rows = DB::select('SELECT row_to_json(t)::text AS row FROM "'.str_replace('"', '""', $table).'" t ORDER BY row_to_json(t)::text');
$same = hash_equals($before['sha256'], hash('sha256', json_encode(array_column($rows, 'row'), JSON_THROW_ON_ERROR)));
if (! $same) {
$changed[] = $table;
if (! in_array($table, ['replenishment_work', 'supply_records', 'audit_logs'], true)) {
throw new LogicException('Protected pilot table changed during replenishment verification.');
}
}
}
$taskVerification = ['native_definition_verified' => true, 'installation_conservation_requested' => count($argv) === 3, 'baseline_tables' => count($baseline),
'unchanged_tables' => count($baseline) - count($changed), 'changed_native_replenishment_tables' => $changed,
'new_policies_or_grants' => false];
}
if (! hash_equals(hash('sha256', $bytes), hash_file('sha256', $path))) {
throw new LogicException('Pilot settings changed during inspection.');
}
$proof = ['date' => gmdate('c'), 'database_enforced_read_only' => true, 'business_commands' => false,
'replenishment_task_verification' => $taskVerification, 'result' => $result];
file_put_contents($root.'/docs/proofs/pilot-operations-current.json', json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode($proof, JSON_THROW_ON_ERROR).PHP_EOL;
exit($result['ok'] ? 0 : 1);