diff --git a/setup/start/sudoers b/setup/start/sudoers index 855c0f82b..12dc1e08c 100644 --- a/setup/start/sudoers +++ b/setup/start/sudoers @@ -65,5 +65,7 @@ yellowtent ALL=(root) NOPASSWD: /home/yellowtent/box/src/scripts/rmmount.sh Defaults!/home/yellowtent/box/src/scripts/remountmount.sh env_keep="HOME BOX_ENV" yellowtent ALL=(root) NOPASSWD: /home/yellowtent/box/src/scripts/remountmount.sh -cloudron-support ALL=(ALL) NOPASSWD: ALL +Defaults!/home/yellowtent/box/src/scripts/du.sh env_keep="HOME BOX_ENV" +yellowtent ALL=(root) NOPASSWD: /home/yellowtent/box/src/scripts/du.sh +cloudron-support ALL=(ALL) NOPASSWD: ALL diff --git a/src/scripts/du.sh b/src/scripts/du.sh new file mode 100755 index 000000000..6cd24c9e9 --- /dev/null +++ b/src/scripts/du.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +set -eu -o pipefail + +if [[ ${EUID} -ne 0 ]]; then + echo "This script should be run as root." > /dev/stderr + exit 1 +fi + +if [[ $# -eq 0 ]]; then + echo "No arguments supplied" + exit 1 +fi + +if [[ "$1" == "--check" ]]; then + echo "OK" + exit 0 +fi + +path="$1" +exclude="${2:-}" + +# -B1 makes du print block sizes and not apparent sizes (to match df which also uses block sizes) +if [[ -z "${exclude}" ]]; then + du -DsB1 "${path}" +else + du -DsB1 "${path}" --exclude "${exclude}" +fi + diff --git a/src/system.js b/src/system.js index 4b65f3e45..ca83f8f92 100644 --- a/src/system.js +++ b/src/system.js @@ -4,7 +4,8 @@ exports = module.exports = { getDisks, checkDiskSpace, getMemory, - getMemoryAllocation + getMemoryAllocation, + du }; const apps = require('./apps.js'), @@ -16,11 +17,22 @@ const apps = require('./apps.js'), docker = require('./docker.js'), notifications = require('./notifications.js'), os = require('os'), + path = require('path'), paths = require('./paths.js'), safe = require('safetydance'), settings = require('./settings.js'), + shell = require('./shell.js'), volumes = require('./volumes.js'); +const DU_CMD = path.join(__dirname, 'scripts/du.sh'); + +async function du(file) { + const [error, stdoutResult] = await safe(shell.promises.sudo('system', [ DU_CMD, file ], {})); + if (error) throw new BoxError(BoxError.FS_ERROR, error); + + return parseInt(stdoutResult.trim(), 10); +} + async function getVolumeDisks(appsDataDisk) { assert.strictEqual(typeof appsDataDisk, 'string'); diff --git a/src/test/applinks-test.js b/src/test/applinks-test.js index e0aef82c7..71b16c7d5 100644 --- a/src/test/applinks-test.js +++ b/src/test/applinks-test.js @@ -52,7 +52,7 @@ describe('Applinks', function () { APPLINK_2.id = await applinks.add(JSON.parse(JSON.stringify(APPLINK_2))); const result = await applinks.get(APPLINK_2.id); - expect(result.upstreamUri).to.eql('https://www.google.com/'); + expect(result.upstreamUri).to.eql('https://google.com/'); expect(result.icon.length).to.not.eql(0); }); diff --git a/src/test/checkInstall b/src/test/checkInstall index fd5505430..8f3edd64f 100755 --- a/src/test/checkInstall +++ b/src/test/checkInstall @@ -18,7 +18,7 @@ scripts=("${SOURCE_DIR}/src/scripts/clearvolume.sh" \ "${SOURCE_DIR}/src/scripts/restartservice.sh" \ "${SOURCE_DIR}/src/scripts/update.sh" \ "${SOURCE_DIR}/src/scripts/collectlogs.sh" \ - "${SOURCE_DIR}/src/scripts/configurecollectd.sh" \ + "${SOURCE_DIR}/src/scripts/du.sh" \ "${SOURCE_DIR}/src/scripts/remotesupport.sh" \ "${SOURCE_DIR}/src/scripts/starttask.sh" \ "${SOURCE_DIR}/src/scripts/stoptask.sh" \ diff --git a/src/test/system-test.js b/src/test/system-test.js index f1854f4fa..7f6515dff 100644 --- a/src/test/system-test.js +++ b/src/test/system-test.js @@ -36,5 +36,12 @@ describe('System', function () { expect(memory.memory).to.be.a('number'); expect(memory.swap).to.be.a('number'); }); + + it('can get du', async function () { + const usage = await system.du(__dirname); + expect(usage).to.be.a('number'); + console.log(usage); + }); + });