move server routes into /system

This commit is contained in:
Girish Ramakrishnan
2023-08-04 13:41:13 +05:30
parent 1264cd1dd7
commit 2cdbf4d2c5
12 changed files with 457 additions and 509 deletions

View File

@@ -1,13 +1,19 @@
'use strict';
exports = module.exports = {
reboot,
isRebootRequired,
getDisks,
getSwaps,
checkDiskSpace,
getMemory,
getMemoryAllocation,
getDiskUsage,
updateDiskUsage
updateDiskUsage,
startUpdateDiskUsage,
getLogs,
getBlockDevices,
runSystemChecks
};
const apps = require('./apps.js'),
@@ -17,16 +23,20 @@ const apps = require('./apps.js'),
debug = require('debug')('box:disks'),
df = require('./df.js'),
docker = require('./docker.js'),
fs = require('fs'),
logs = require('./logs.js'),
notifications = require('./notifications.js'),
os = require('os'),
path = require('path'),
paths = require('./paths.js'),
safe = require('safetydance'),
shell = require('./shell.js'),
tasks = require('./tasks.js'),
volumes = require('./volumes.js');
const DU_CMD = path.join(__dirname, 'scripts/du.sh');
const HDPARM_CMD = path.join(__dirname, 'scripts/hdparm.sh');
const REBOOT_CMD = path.join(__dirname, 'scripts/reboot.sh');
async function du(file) {
assert.strictEqual(typeof file, 'string');
@@ -249,3 +259,86 @@ async function updateDiskUsage(progressCallback) {
return disks;
}
async function reboot() {
await notifications.clearAlert(notifications.ALERT_REBOOT, 'Reboot Required');
const [error] = await safe(shell.promises.sudo('reboot', [ REBOOT_CMD ], {}));
if (error) debug('reboot: could not reboot. %o', error);
}
async function isRebootRequired() {
// https://serverfault.com/questions/92932/how-does-ubuntu-keep-track-of-the-system-restart-required-flag-in-motd
return fs.existsSync('/var/run/reboot-required');
}
async function startUpdateDiskUsage() {
const taskId = await tasks.add(tasks.TASK_UPDATE_DISK_USAGE, []);
tasks.startTask(taskId, {});
return taskId;
}
async function getLogs(unit, options) {
assert.strictEqual(typeof unit, 'string');
assert(options && typeof options === 'object');
debug(`Getting logs for ${unit}`);
let logFile = '';
if (unit === 'box') logFile = path.join(paths.LOG_DIR, 'box.log'); // box.log is at the top
else throw new BoxError(BoxError.BAD_FIELD, `No such unit '${unit}'`);
const cp = logs.tail([logFile], { lines: options.lines, follow: options.follow });
const logStream = new logs.LogStream({ format: options.format || 'json', source: unit });
logStream.close = cp.kill.bind(cp, 'SIGKILL'); // hook for caller. closing stream kills the child process
cp.stdout.pipe(logStream);
return logStream;
}
async function getBlockDevices() {
const info = safe.JSON.parse(safe.child_process.execSync('lsblk --paths --json --list --fs', { encoding: 'utf8' }));
if (!info) throw new BoxError(BoxError.INTERNAL_ERROR, safe.error.message);
const devices = info.blockdevices.filter(d => d.fstype === 'ext4' || d.fstype === 'xfs');
debug(`getBlockDevices: Found ${devices.length} devices. ${devices.map(d => d.name).join(', ')}`);
return devices.map(function (d) {
return {
path: d.name,
size: d.fsavail || 0,
type: d.fstype,
mountpoint: d.mountpoints ? d.mountpoints[0] : d.mountpoint // we only support one mountpoint here old lsblk only exposed one via .mountpoint
};
});
}
async function checkRebootRequired() {
const rebootRequired = await isRebootRequired();
if (rebootRequired) {
await notifications.alert(notifications.ALERT_REBOOT, 'Reboot Required', 'To finish ubuntu security updates, a reboot is necessary.', { persist: true });
} else {
await notifications.clearAlert(notifications.ALERT_REBOOT, 'Reboot Required');
}
}
async function checkUbuntuVersion() {
const isXenial = fs.readFileSync('/etc/lsb-release', 'utf-8').includes('16.04');
if (!isXenial) return;
await notifications.alert(notifications.ALERT_UPDATE_UBUNTU, 'Ubuntu upgrade required', 'Ubuntu 16.04 has reached end of life and will not receive security and maintenance updates. Please follow https://docs.cloudron.io/guides/upgrade-ubuntu-18/ to upgrade to Ubuntu 18 at the earliest.', { persist: true });
}
async function runSystemChecks() {
debug('runSystemChecks: checking status');
const checks = [
checkRebootRequired(),
checkUbuntuVersion()
];
await Promise.allSettled(checks);
}