docker.js and services.js: async'ify

This commit is contained in:
Girish Ramakrishnan
2021-08-25 19:41:46 -07:00
parent 1cc11fece8
commit 42774eac8c
24 changed files with 1618 additions and 2130 deletions

View File

@@ -9,7 +9,6 @@ exports = module.exports = {
const apps = require('./apps.js'),
assert = require('assert'),
async = require('async'),
BoxError = require('./boxerror.js'),
debug = require('debug')('box:disks'),
df = require('@sindresorhus/df'),
@@ -21,8 +20,6 @@ const apps = require('./apps.js'),
settings = require('./settings.js'),
volumes = require('./volumes.js');
const dfAsync = async.asyncify(df), dfFileAsync = async.asyncify(df.file);
async function getVolumeDisks(appsDataDisk) {
assert.strictEqual(typeof appsDataDisk, 'string');
@@ -55,7 +52,7 @@ async function getAppDisks(appsDataDisk) {
return appDisks;
}
async function getBackupDisk() {
async function getBackupsFilesystem() {
const backupConfig = await settings.getBackupConfig();
if (backupConfig.provider !== 'filesystem') return null;
@@ -64,82 +61,67 @@ async function getBackupDisk() {
return result.filesystem;
}
function getDisks(callback) {
assert.strictEqual(typeof callback, 'function');
async function getDisks() {
const info = await docker.info();
docker.info(function (error, info) {
if (error) return callback(error);
let [error, allDisks] = await safe(df());
if (error) throw new BoxError(BoxError.FS_ERROR, error);
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),
getBackupDisk,
], async function (error, values) {
if (error) return callback(new BoxError(BoxError.FS_ERROR, error));
// filter by ext4 and then sort to make sure root disk is first
const ext4Disks = allDisks.filter((r) => r.type === 'ext4').sort((a, b) => a.mountpoint.localeCompare(b.mountpoint));
// 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 diskInfos = [];
for (const p of [ paths.BOX_DATA_DIR, paths.PLATFORM_DATA_DIR, paths.APPS_DATA_DIR, info.DockerRootDir ]) {
const [dfError, diskInfo] = await safe(df.file(p));
if (dfError) throw new BoxError(BoxError.FS_ERROR, dfError);
diskInfos.push(diskInfo);
}
const disks = {
disks: ext4Disks, // root disk is first. { filesystem, type, size, used, avialable, capacity, mountpoint }
boxDataDisk: values[1].filesystem,
mailDataDisk: values[1].filesystem,
platformDataDisk: values[2].filesystem,
appsDataDisk: values[3].filesystem,
dockerDataDisk: values[4].filesystem,
backupsDisk: values[5],
apps: {}, // filled below
volumes: {} // filled below
};
const backupsFilesystem = await getBackupsFilesystem();
[error, disks.apps] = await safe(getAppDisks(disks.appsDataDisk));
if (error) return callback(error);
const result = {
disks: ext4Disks, // root disk is first. { filesystem, type, size, used, avialable, capacity, mountpoint }
boxDataDisk: diskInfos[0].filesystem,
mailDataDisk: diskInfos[0].filesystem,
platformDataDisk: diskInfos[1].filesystem,
appsDataDisk: diskInfos[2].filesystem,
dockerDataDisk: diskInfos[3].filesystem,
backupsDisk: backupsFilesystem,
apps: {}, // filled below
volumes: {} // filled below
};
[error, disks.volumes] = await safe(getVolumeDisks(disks.appsDataDisk));
if (error) return callback(error);
result.apps = await getAppDisks(result.appsDataDisk);
result.volumes = await getVolumeDisks(result.appsDataDisk);
callback(null, disks);
});
});
return result;
}
function checkDiskSpace(callback) {
assert.strictEqual(typeof callback, 'function');
async function checkDiskSpace() {
debug('checkDiskSpace: checking disk space');
getDisks(async function (error, disks) {
if (error) {
debug('checkDiskSpace: error getting disks %s', error.message);
return callback();
const disks = await getDisks();
let markdownMessage = '';
disks.disks.forEach(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
&& entry.filesystem !== disks.dockerDataDisk) return false;
if (entry.available <= (1.25 * 1024 * 1024 * 1024)) { // 1.5G
markdownMessage += `* ${entry.filesystem} is at ${entry.capacity*100}% capacity.\n`;
}
let markdownMessage = '';
disks.disks.forEach(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
&& entry.filesystem !== disks.dockerDataDisk) return false;
if (entry.available <= (1.25 * 1024 * 1024 * 1024)) { // 1.5G
markdownMessage += `* ${entry.filesystem} is at ${entry.capacity*100}% capacity.\n`;
}
});
debug(`checkDiskSpace: disk space checked. out of space: ${markdownMessage || 'no'}`);
if (markdownMessage) markdownMessage = `One or more file systems are running out of space. Please increase the disk size at the earliest.\n\n${markdownMessage}`;
await notifications.alert(notifications.ALERT_DISK_SPACE, 'Server is running out of disk space', markdownMessage);
callback();
});
debug(`checkDiskSpace: disk space checked. out of space: ${markdownMessage || 'no'}`);
if (markdownMessage) markdownMessage = `One or more file systems are running out of space. Please increase the disk size at the earliest.\n\n${markdownMessage}`;
await notifications.alert(notifications.ALERT_DISK_SPACE, 'Server is running out of disk space', markdownMessage);
}
function getSwapSize() {
@@ -149,13 +131,11 @@ function getSwapSize() {
return swap;
}
function getMemory(callback) {
assert.strictEqual(typeof callback, 'function');
callback(null, {
async function getMemory() {
return {
memory: os.totalmem(),
swap: getSwapSize()
});
};
}
function getMemoryAllocation(limit) {