Files
SGF/scripts/ledger-cycle-native-run.mjs
T
2026-11-04 09:37:20 +02:00

14 lines
3.0 KiB
JavaScript

import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import {spawnSync} from 'node:child_process';
const read=p=>JSON.parse(fs.readFileSync(p,'utf8')),state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json');if(state.port!==55439)throw Error('Owned cluster required');
const packaged=process.argv[2]==='new-bundle',resume=['new','new-bundle'].includes(process.argv[2])?undefined:process.argv[2];if(resume&&!/^[a-f0-9]{12}$/.test(resume))throw Error('Invalid tag');
const tag=resume??crypto.randomBytes(6).toString('hex'),root=path.join(state.root,'private/ledger-native-'+tag),settings=path.join(root,'settings.json');
if(!resume){
const admin=fs.readFileSync(path.join(state.root,'private/admin-password.txt'),'utf8').trim(),pg=input=>{const r=spawnSync('C:/Program Files/PostgreSQL/17/bin/psql.exe',['-h','127.0.0.1','-p','55439','-U','sgf_regression_owner','-d','postgres','-Atq','-v','ON_ERROR_STOP=1'],{input,encoding:'utf8',windowsHide:true,env:{...process.env,PGPASSWORD:admin}});if(r.status!==0)throw Error('Native cycle provisioning failed');return r.stdout.trim();};if(pg("SELECT current_setting('cluster_name')")!=='sgf_modular_regression_'+state.id)throw Error('Wrong actual cluster');
const role='payable_history_native_'+tag,database='compras_receipts_test_'+tag,password=crypto.randomBytes(32).toString('hex');pg(`CREATE ROLE ${role} LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS; CREATE DATABASE ${database} OWNER ${role}; REVOKE ALL ON DATABASE ${database} FROM PUBLIC;`);
const storage=path.join(root,'storage');for(const d of ['framework/cache/data','framework/views','framework/sessions','logs','cache'])fs.mkdirSync(path.join(storage,d),{recursive:true});
const artifact=packaged?read(path.join(state.root,'finance-bundle-latest.json')):null;
fs.writeFileSync(settings,JSON.stringify({tag,root,cluster:'sgf_modular_regression_'+state.id,source_instance:'qa-native-ledger-'+tag,app_key:'base64:'+crypto.randomBytes(32).toString('base64'),...(artifact?{common_bundle:artifact.root,common_bundle_sha256:artifact.manifest_sha256}:{}),native:{root:path.resolve('../compras'),database,role,password,storage}},null,2));
}
const phases=process.argv[3]?.split(',')??(resume?[]:['native-cycle-setup']);for(const phase of phases){if(!['native-cycle-setup','native-cycle-cases','native-cycle-workflow'].includes(phase))throw Error('Phase refused');const output=path.join(root,phase==='native-cycle-setup'?'ledger-fixture.json':phase+'.json');if(fs.existsSync(output))throw Error('Completed proof preserved');const r=spawnSync('C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe',['scripts/payable-history-sgf-proof.php',settings,phase],{encoding:'utf8',windowsHide:true,maxBuffer:1024*1024});fs.writeFileSync(path.join(root,phase+'-'+Date.now()+'.log'),(r.stdout??'')+(r.stderr??''));if(r.status!==0||!fs.existsSync(output))throw Error('Native phase failed; inspect private diagnostics: '+root);process.stdout.write(r.stdout);}
console.log(JSON.stringify({tag,root,native_only:true}));