'use strict'; exports = module.exports = { getDisks, checkDiskSpace, getMemory, getMemoryAllocation }; const apps = require('./apps.js'), assert = require('assert'), BoxError = require('./boxerror.js'), debug = require('debug')('box:disks'), df = require('@sindresorhus/df'), docker = require('./docker.js'), notifications = require('./notifications.js'), os = require('os'), paths = require('./paths.js'), safe = require('safetydance'), settings = require('./settings.js'), volumes = require('./volumes.js'); async function getVolumeDisks(appsDataDisk) { assert.strictEqual(typeof appsDataDisk, 'string'); let volumeDisks = {}; const allVolumes = await volumes.list(); for (const volume of allVolumes) { const [error, result] = await safe(df(volume.hostPath)); volumeDisks[volume.id] = error ? appsDataDisk : result.filesystem; // ignore any errors } return volumeDisks; } async function getAppDisks(appsDataDisk) { assert.strictEqual(typeof appsDataDisk, 'string'); let appDisks = {}; const allApps = await apps.list(); for (const app of allApps) { if (!app.dataDir) { appDisks[app.id] = appsDataDisk; } else { const [error, result] = await safe(df.file(app.dataDir)); appDisks[app.id] = error ? appsDataDisk : result.filesystem; // ignore any errors } } return appDisks; } async function getBackupsFilesystem() { const backupConfig = await settings.getBackupConfig(); if (backupConfig.provider !== 'filesystem') return null; const result = await df.file(backupConfig.backupFolder); return result.filesystem; } async function getDisks() { const info = await docker.info(); let [error, allDisks] = await safe(df()); if (error) throw 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)); 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 backupsFilesystem = await getBackupsFilesystem(); 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 }; result.apps = await getAppDisks(result.appsDataDisk); result.volumes = await getVolumeDisks(result.appsDataDisk); return result; } async function checkDiskSpace() { debug('checkDiskSpace: checking disk space'); 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`; } }); 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() { const stdout = safe.child_process.execSync('swapon --noheadings --raw --bytes --show=SIZE', { encoding: 'utf8' }); const swap = !stdout ? 0 : stdout.trim().split('\n').map(x => parseInt(x, 10) || 0).reduce((acc, cur) => acc + cur); return swap; } async function getMemory() { return { memory: os.totalmem(), swap: getSwapSize() }; } function getMemoryAllocation(limit) { let ratio = parseFloat(safe.fs.readFileSync(paths.SWAP_RATIO_FILE, 'utf8'), 10); if (!ratio) { const pc = os.totalmem() / (os.totalmem() + getSwapSize()); ratio = Math.round(pc * 10) / 10; // a simple ratio } return Math.round(Math.round(limit * ratio) / 1048576) * 1048576; // nearest MB }