MC/website/ops/provision-smuggler-institutions.mjs
2026-11-27 23:43:05 +00:00

26 lines
3.4 KiB
JavaScript

import {readFileSync,realpathSync} from 'node:fs';import {fileURLToPath} from 'node:url';import {createHash} from 'node:crypto';
import mysql from 'mysql2/promise';
import {OFFICIAL_ACCOUNT} from '../node/official-account.mjs';
import {createClandestineInstitutions,readSmugglerInstitutions} from '../node/clandestine-institutions.mjs';
if(process.geteuid?.()!==0)throw Error('A root server operator is required');
const args=process.argv.slice(2);if(args.length>1||args.length===1&&args[0]!=='--apply')throw Error('Choose read-only inspection or --apply');
const root=realpathSync(fileURLToPath(new URL('..',import.meta.url)));if(root!==realpathSync('/home/dunerealms/apps/dunerealms/current'))throw Error('Run the reviewed current published operator');
process.loadEnvFile('/home/dunerealms/.config/dunerealms.env');const e=process.env,config=JSON.parse(readFileSync('/home/dunerealms/.config/integrations.json'));
const native=await mysql.createConnection({host:config.host,user:config.username,password:config.password,database:config.database});
try{const [rows]=await native.execute('SELECT approval,title FROM dune_dev_tasks WHERE ticket_id=412');if(rows.length!==1||rows[0].title!=='Implement POL XP, House questmasters and clandestine quests using existing J.A.R.V.I.S. flows'||args.length&&rows[0].approval!=='approved')throw Error('Current approved ticket 412 required');}finally{await native.end();}
const raw=await mysql.createConnection({host:e.DB_HOST||'127.0.0.1',port:Number(e.DB_PORT||3306),user:e.DB_USER,password:e.DB_PASSWORD,database:e.DB_NAME});
const reader={query:async(sql,args=[])=>{if(!/^SELECT /i.test(sql))throw Error('Inspection is read-only');return(await raw.execute(sql,args))[0];}};
const authorizeOperator=async(actor,store)=>(await store.query("SELECT p.principal_kind FROM org_control_principals p JOIN site_accounts a ON a.id=p.user_id WHERE p.user_id=? AND p.principal_kind='owner' AND a.username='unimatrix999' AND a.password_hash NOT LIKE '!%'",[actor.id])).length===1;
const memberHash=async store=>createHash('sha256').update(JSON.stringify([
await store.query("SELECT m.* FROM site_clan_members m JOIN site_accounts a ON a.id=m.user_id WHERE a.role<>'system_account' ORDER BY m.user_id"),
await store.query("SELECT m.* FROM site_company_members m JOIN site_accounts a ON a.id=m.user_id WHERE a.role<>'system_account' ORDER BY m.company_id,m.user_id")
])).digest('hex');
let db;
try{
const actor={id:OFFICIAL_ACCOUNT.ownerId};if(!await authorizeOperator(actor,reader))throw Error('Current owner principal required');
if(!args.length){const current=await readSmugglerInstitutions(reader,null);console.log(JSON.stringify({ticketId:412,readOnly:true,release:root.split('/').at(-1),existing:!!current,...(current||{})}));}
else{
const before=await memberHash(reader),{openDatabase}=await import('../node/mysql.mjs'),{openOrganisationKeyService}=await import('../node/organisation-key-service.mjs');db=await openDatabase(e);const keys=await openOrganisationKeyService(db,e),result=await createClandestineInstitutions(db,keys,{authorizeOperator}).provision(actor),after=await memberHash(reader);
console.log(JSON.stringify({ticketId:412,applied:true,release:root.split('/').at(-1),...result,ordinaryMembershipHashBefore:before,ordinaryMembershipHashAfter:after,ordinaryMembershipsUnchanged:before===after,gameSynchronizationPerformed:false}));
}
}finally{await db?.close();await raw.end();}