Files
cloudron-box/src/system.js

126 lines
4.6 KiB
JavaScript
Raw Normal View History

2019-08-19 13:50:44 -07:00
'use strict';
exports = module.exports = {
getDisks: getDisks,
2019-11-21 12:55:17 -08:00
checkDiskSpace: checkDiskSpace,
getMemory: getMemory
2019-08-19 13:50:44 -07:00
};
const apps = require('./apps.js'),
assert = require('assert'),
async = require('async'),
2019-10-22 11:11:41 -07:00
BoxError = require('./boxerror.js'),
2019-08-19 13:50:44 -07:00
debug = require('debug')('box:disks'),
df = require('@sindresorhus/df'),
docker = require('./docker.js'),
notifications = require('./notifications.js'),
2019-11-21 12:55:17 -08:00
os = require('os'),
paths = require('./paths.js'),
2020-01-31 13:37:07 -08:00
safe = require('safetydance'),
settings = require('./settings.js');
2019-08-19 13:50:44 -07:00
function getDisks(callback) {
assert.strictEqual(typeof callback, 'function');
const dfAsync = async.asyncify(df), dfFileAsync = async.asyncify(df.file);
docker.info(function (error, info) {
2019-10-22 11:11:41 -07:00
if (error) return callback(error);
2019-08-19 13:50:44 -07:00
async.series([
dfAsync,
dfFileAsync.bind(null, paths.BOX_DATA_DIR),
dfFileAsync.bind(null, paths.PLATFORM_DATA_DIR),
dfFileAsync.bind(null, paths.APPS_DATA_DIR),
dfFileAsync.bind(null, info.DockerRootDir)
], function (error, values) {
2019-10-22 11:11:41 -07:00
if (error) return callback(new BoxError(BoxError.FS_ERROR, error));
2019-08-19 13:50:44 -07:00
// filter by ext4 and then sort to make sure root disk is first
const ext4Disks = values[0].filter((r) => r.type === 'ext4').sort((a, b) => a.mountpoint.localeCompare(b.mountpoint));
const disks = {
2020-04-01 16:26:16 -07:00
disks: ext4Disks, // root disk is first. { filesystem, type, size, used, avialable, capacity, mountpoint }
2019-08-19 13:50:44 -07:00
boxDataDisk: values[1].filesystem,
mailDataDisk: values[1].filesystem,
platformDataDisk: values[2].filesystem,
appsDataDisk: values[3].filesystem,
dockerDataDisk: values[4].filesystem,
2020-01-31 13:37:07 -08:00
backupsDisk: null,
2019-08-19 13:50:44 -07:00
apps: {}
};
apps.getAll(function (error, allApps) {
if (error) return callback(error);
2019-08-19 13:50:44 -07:00
async.eachSeries(allApps, function (app, iteratorDone) {
if (!app.dataDir) {
disks.apps[app.id] = disks.appsDataDisk;
return iteratorDone();
}
dfFileAsync(app.dataDir, function (error, result) {
disks.apps[app.id] = error ? disks.appsDataDisk : result.filesystem; // ignore any errors
iteratorDone();
});
}, function (error) {
if (error) return callback(error);
2019-08-19 13:50:44 -07:00
2020-01-31 13:37:07 -08:00
settings.getBackupConfig(function (error, backupConfig) {
if (error) return callback(error);
if (backupConfig.provider !== 'filesystem') return callback(null, disks);
dfFileAsync(backupConfig.backupFolder, function (error, result) {
if (error) return callback(error);
disks.backupsDisk = result.filesystem;
callback(null, disks);
});
});
2019-08-19 13:50:44 -07:00
});
});
});
});
}
function checkDiskSpace(callback) {
assert.strictEqual(typeof callback, 'function');
debug('Checking disk space');
getDisks(function (error, disks) {
if (error) {
debug('checkDiskSpace: error getting disks %s', error.message);
return callback();
}
var oos = disks.disks.some(function (entry) {
// ignore other filesystems but where box, app and platform data is
if (entry.filesystem !== disks.boxDataDisk
&& entry.filesystem !== disks.platformDataDisk
&& entry.filesystem !== disks.appsDataDisk
&& entry.filesystem !== disks.backupsDisk
2019-08-19 13:50:44 -07:00
&& entry.filesystem !== disks.dockerDataDisk) return false;
return (entry.available <= (1.25 * 1024 * 1024 * 1024)); // 1.5G
});
debug('checkDiskSpace: disk space checked. ok: %s', !oos);
notifications.alert(notifications.ALERT_DISK_SPACE, 'Server is running out of disk space', oos ? JSON.stringify(disks.disks, null, 4) : '', callback);
});
}
2019-11-21 12:55:17 -08:00
function getMemory(callback) {
assert.strictEqual(typeof callback, 'function');
const stdout = safe.child_process.execSync('swapon --noheadings --raw --bytes --show=SIZE', { encoding: 'utf8' });
2019-11-21 13:48:17 -08:00
const swap = !stdout ? 0 : stdout.trim().split('\n').map(x => parseInt(x, 10) || 0).reduce((acc, cur) => acc + cur);
2019-11-21 12:55:17 -08:00
callback(null, {
memory: os.totalmem(),
swap: swap
});
}