server reboot too

This commit is contained in:
legop3
2026-02-18 15:13:38 -05:00
parent 40e21d4ac5
commit 7e772ab607
6 changed files with 98 additions and 11 deletions
@@ -0,0 +1,52 @@
const { spawn } = require('child_process');
const io = require('../globals/io');
const logger = require('../globals/logger').child('serverControlService');
const { isAdmin } = require('./roleService');
const { sendAlert } = require('./alertService');
const ALERT_COLOR = '#ff5722';
let rebootPending = false;
function scheduleSystemReboot() {
if (rebootPending) {
throw new Error('Server reboot already pending');
}
rebootPending = true;
setTimeout(() => {
logger.warn('Issuing system reboot command');
try {
const child = spawn('systemctl', ['reboot'], {
detached: true,
stdio: 'ignore',
});
child.unref();
} catch (err) {
rebootPending = false;
logger.error('Server reboot command failed', err.message);
}
}, 400);
}
io.on('connection', (socket) => {
socket.on('server:reboot', (_, cb = () => {}) => {
if (!isAdmin(socket)) {
cb({ error: 'Not authorized' });
return;
}
try {
scheduleSystemReboot();
const who = socket?.data?.user?.username || socket.id;
logger.warn('Server reboot requested', { by: who });
sendAlert({
color: ALERT_COLOR,
title: 'Server Reboot',
message: `Reboot requested by ${who}`,
});
cb({ success: true });
} catch (err) {
cb({ error: err.message });
}
});
});