forked from unimatrix/web
Provision audited Rihla cash custody and persistent activation
This commit is contained in:
parent
484f121fcc
commit
0097a856f7
1 changed files with 134 additions and 0 deletions
32
website/ops/configure-rihla-cash.mjs
Normal file
32
website/ops/configure-rihla-cash.mjs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import {readFile,writeFile,mkdir,stat,chmod,chown,rename,realpath} from 'node:fs/promises';
|
||||
import {resolve} from 'node:path';
|
||||
import {pathToFileURL} from 'node:url';
|
||||
import mysql from 'mysql2/promise';
|
||||
import {provisionRihlaBacking,rihlaEnvironment,RIHLA_CONFIGURATION} from '../node/rihla-configuration.mjs';
|
||||
import {createRihlaPostbank} from '../node/rihla-postbank.mjs';
|
||||
import {openRihlaCashReceiptReader} from '../node/rihla-cash.mjs';
|
||||
|
||||
async function main(){
|
||||
const envPath='/home/dunerealms/.config/dunerealms.env',apply=process.argv.includes('--apply');
|
||||
const old=await readFile(envPath,'utf8'),meta=await stat(envPath);process.loadEnvFile(envPath);
|
||||
const settings=process.env,config=JSON.parse(await readFile(settings.POSTBANK_DB_CONFIG,'utf8'));
|
||||
const pool=mysql.createPool({...config,supportBigNumbers:true,bigNumberStrings:true}),api=c=>({async query(sql,args=[]){return(await c.execute(sql,args))[0]},async first(sql,args=[]){return(await c.execute(sql,args))[0][0]||null}});
|
||||
const bank={...api(pool),async transaction(work){const c=await pool.getConnection();try{await c.beginTransaction();const value=await work(api(c));await c.commit();return value}catch(error){await c.rollback();throw error}finally{c.release()}}};
|
||||
let native;
|
||||
try{
|
||||
const url=new URL(settings.DUNE_INVENTORY_DATABASE_URL);if(url.protocol!=='mysql:')throw Error('Verified native MySQL connection required');
|
||||
native=mysql.createPool(url.href);
|
||||
await openRihlaCashReceiptReader(bank,{dialect:'mysql',async query(sql){return{rows:(await native.query(sql))[0]}}},url.href);
|
||||
const candidate=rihlaEnvironment(old),plan=await provisionRihlaBacking(bank);
|
||||
if(!apply){console.log(JSON.stringify({applied:false,backing:plan,configuration:RIHLA_CONFIGURATION,financialTransactions:false}));return;}
|
||||
const backup=resolve('/root/dunerealms-rihla-configuration-backups',new Date().toISOString().replace(/[:.]/g,'-'));await mkdir(backup,{recursive:true,mode:0o700});
|
||||
await writeFile(resolve(backup,'environment.before'),old,{mode:0o600,flag:'wx'});
|
||||
await writeFile(resolve(backup,'plan.json'),JSON.stringify(plan),{mode:0o600,flag:'wx'});
|
||||
const backing=await provisionRihlaBacking(bank,{apply:true});
|
||||
await createRihlaPostbank(bank,{authority:async()=>{throw Error('No financial operation allowed during setup')},backingSystemKey:RIHLA_CONFIGURATION.RIHLA_POSTBANK_BACKING_SYSTEM_KEY}).verifyConfiguration();
|
||||
if(await readFile(envPath,'utf8')!==old)throw Error('Environment changed during setup; zero-balance custody retained, activation not applied');
|
||||
const temporary=envPath+'.rihla-'+process.pid;await writeFile(temporary,candidate,{mode:meta.mode&0o777,flag:'wx'});await chown(temporary,meta.uid,meta.gid);await chmod(temporary,meta.mode&0o777);await rename(temporary,envPath);
|
||||
console.log(JSON.stringify({applied:true,backing,configuration:RIHLA_CONFIGURATION,backup,restartRequired:true,financialTransactions:false}));
|
||||
}finally{await pool.end();if(native)await native.end()}
|
||||
}
|
||||
if(process.argv[1]&&import.meta.url===pathToFileURL(await realpath(resolve(process.argv[1]))).href)await main();
|
||||
Loading…
Reference in a new issue

Staff