// Installed outside the website. No HTTP entry point and no arbitrary SQL/action. import {createRequire} from 'node:module'; import {randomBytes} from 'node:crypto'; import {createDevKeyAuthority} from './jarvis-dev-key.mjs'; import {clanResetManifest,executeClanReset} from './jarvis-dev-clan-reset.mjs'; const BATCH='clan-reset-20261118-v1'; if(process.getuid?.()!==0)throw Error('Run the installed CLI with explicit server administrator authority'); if(process.argv.length!==3||!['--plan','--apply'].includes(process.argv[2]))throw Error('Choose --plan or --apply'); process.loadEnvFile('/home/dunerealms/.config/dunerealms.env'); const require=createRequire('/home/dunerealms/apps/dunerealms/current/node/mysql.mjs'),mysql=require('mysql2/promise'); const db=await mysql.createConnection({host:process.env.DB_HOST||'127.0.0.1',port:Number(process.env.DB_PORT||3306),user:process.env.DB_USER,password:process.env.DB_PASSWORD,database:process.env.DB_NAME,supportBigNumbers:true,bigNumberStrings:true,multipleStatements:false}); const tx={query:async(sql,args=[])=>{const [rows]=await db.execute(sql,args);return rows;},first:async(sql,args=[])=>{const [rows]=await db.execute(sql,args);return rows[0]||null;}}; let locked=false,started=false; try{ const lock=await tx.first("SELECT GET_LOCK('jarvis-dev-clan-reset',10) acquired");if(Number(lock.acquired)!==1)throw Error('Another clan reset is running');locked=true; await db.beginTransaction();started=true; const prior=await tx.first("SELECT id FROM account_audit WHERE action='dev-clan-reset-complete' AND details LIKE ?",['%'+BATCH+'%']); if(prior){await db.rollback();started=false;console.log(JSON.stringify({batchId:BATCH,state:'already-completed',replayed:true}));} else{ const manifest=await clanResetManifest(tx,'unimatrix999'); const names=await tx.query('SELECT id,name FROM site_clans WHERE active=1 ORDER BY id'); console.log(JSON.stringify({batchId:BATCH,state:'planned',retained:names.filter(c=>manifest.retainClanIds.includes(c.id)),archive:names.filter(c=>manifest.deleteClanIds.includes(c.id))})); if(process.argv[2]==='--plan'){await db.rollback();started=false;} else{ const actor={id:'server-root'},authority=createDevKeyAuthority({secret:randomBytes(32).toString('hex'),environment:'production',authorize:async a=>process.getuid()===0&&a.id===actor.id}); const issued=await authority.issue(actor,manifest),record=await authority.verify(actor,issued.key,manifest); // Record the receipt without ever printing the bearer key or signing secret. await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[issued.record.id,manifest.ownerId,'dev-key-issued',JSON.stringify({...issued.audit,batchId:BATCH}),Date.now()]); const result=await executeClanReset(tx,{record,expectedManifest:manifest,batchId:BATCH}); const after=await clanResetManifest(tx,'unimatrix999'); if(after.deleteClanIds.length)throw Error('Reset verification failed'); await db.commit();started=false; console.log(JSON.stringify({batchId:BATCH,keyId:record.id,state:'completed',...result,companiesChanged:false,balancesChanged:false})); } } }catch(error){if(started)await db.rollback();console.error(JSON.stringify({batchId:BATCH,state:'failed',reason:'Reset failed; transaction rolled back',code:error.code||error.name}));process.exitCode=1;} finally{if(locked)await tx.query("SELECT RELEASE_LOCK('jarvis-dev-clan-reset')");await db.end();}