diff --git a/src/apptask.js b/src/apptask.js index 3ba9ef437..2aa9dc414 100644 --- a/src/apptask.js +++ b/src/apptask.js @@ -27,6 +27,7 @@ var addons = require('./addons.js'), auditSource = require('./auditsource.js'), backups = require('./backups.js'), BoxError = require('./boxerror.js'), + collectd = require('./collectd.js'), constants = require('./constants.js'), debug = require('debug')('box:apptask'), df = require('@sindresorhus/df'), @@ -52,7 +53,6 @@ var addons = require('./addons.js'), _ = require('underscore'); const COLLECTD_CONFIG_EJS = fs.readFileSync(__dirname + '/collectd.config.ejs', { encoding: 'utf8' }), - CONFIGURE_COLLECTD_CMD = path.join(__dirname, 'scripts/configurecollectd.sh'), MV_VOLUME_CMD = path.join(__dirname, 'scripts/mvvolume.sh'), LOGROTATE_CONFIG_EJS = fs.readFileSync(__dirname + '/logrotate.ejs', { encoding: 'utf8' }), CONFIGURE_LOGROTATE_CMD = path.join(__dirname, 'scripts/configurelogrotate.sh'); @@ -229,29 +229,14 @@ function addCollectdProfile(app, callback) { assert.strictEqual(typeof callback, 'function'); var collectdConf = ejs.render(COLLECTD_CONFIG_EJS, { appId: app.id, containerId: app.containerId, appDataDir: apps.getDataDir(app, app.dataDir) }); - fs.writeFile(path.join(paths.COLLECTD_APPCONFIG_DIR, app.id + '.conf'), collectdConf, function (error) { - if (error) return callback(new BoxError(BoxError.FS_ERROR, `Error writing collectd config: ${error.message}`)); - - shell.sudo('addCollectdProfile', [ CONFIGURE_COLLECTD_CMD, 'add', app.id ], {}, function (error) { - if (error) return callback(new BoxError(BoxError.COLLECTD_ERROR, 'Could not add collectd config')); - - callback(null); - }); - }); + collectd.addProfile(app.id, collectdConf, callback); } function removeCollectdProfile(app, callback) { assert.strictEqual(typeof app, 'object'); assert.strictEqual(typeof callback, 'function'); - fs.unlink(path.join(paths.COLLECTD_APPCONFIG_DIR, app.id + '.conf'), function (error) { - if (error && error.code !== 'ENOENT') debugApp(app, 'Error removing collectd profile', error); - shell.sudo('removeCollectdProfile', [ CONFIGURE_COLLECTD_CMD, 'remove', app.id ], {}, function (error) { - if (error) return callback(new BoxError(BoxError.COLLECTD_ERROR, 'Could not remove collectd config')); - - callback(null); - }); - }); + collectd.removeProfile(app.id, callback); } function addLogrotateConfig(app, callback) { diff --git a/src/collectd.js b/src/collectd.js new file mode 100644 index 000000000..d91f510c6 --- /dev/null +++ b/src/collectd.js @@ -0,0 +1,48 @@ +'use strict'; + +exports = module.exports = { + addProfile, + removeProfile +}; + +const assert = require('assert'), + BoxError = require('./boxerror.js'), + debug = require('debug')('collectd'), + fs = require('fs'), + path = require('path'), + paths = require('./paths.js'), + shell = require('./shell.js'); + +const CONFIGURE_COLLECTD_CMD = path.join(__dirname, 'scripts/configurecollectd.sh'); + +function addProfile(name, profile, callback) { + assert.strictEqual(typeof name, 'string'); + assert.strictEqual(typeof profile, 'string'); + assert.strictEqual(typeof callback, 'function'); + + fs.writeFile(path.join(paths.COLLECTD_APPCONFIG_DIR, `${name}.conf`), profile, function (error) { + if (error) return callback(new BoxError(BoxError.FS_ERROR, `Error writing collectd config: ${error.message}`)); + + shell.sudo('addCollectdProfile', [ CONFIGURE_COLLECTD_CMD, 'add', name ], {}, function (error) { + if (error) return callback(new BoxError(BoxError.COLLECTD_ERROR, 'Could not add collectd config')); + + callback(null); + }); + }); + +} + +function removeProfile(name, callback) { + assert.strictEqual(typeof name, 'string'); + assert.strictEqual(typeof callback, 'function'); + + fs.unlink(path.join(paths.COLLECTD_APPCONFIG_DIR, `${name}.conf`), function (error) { + if (error && error.code !== 'ENOENT') debug('Error removing collectd profile', error); + + shell.sudo('removeCollectdProfile', [ CONFIGURE_COLLECTD_CMD, 'remove', name ], {}, function (error) { + if (error) return callback(new BoxError(BoxError.COLLECTD_ERROR, 'Could not remove collectd config')); + + callback(null); + }); + }); +} diff --git a/src/scripts/configurecollectd.sh b/src/scripts/configurecollectd.sh index 4b7bf49ed..975c947c3 100755 --- a/src/scripts/configurecollectd.sh +++ b/src/scripts/configurecollectd.sh @@ -13,7 +13,7 @@ if [[ $# == 1 && "$1" == "--check" ]]; then fi cmd="$1" -appid="$2" +metric="$2" # note that this can also be 'cloudron-backup' or appid if [[ "${BOX_ENV}" == "cloudron" ]]; then # when restoring the cloudron with many apps, the apptasks rush in to restart @@ -30,10 +30,10 @@ if [[ "${BOX_ENV}" == "cloudron" ]]; then # delete old stats when uninstalling an app if [[ "${cmd}" == "remove" ]]; then - echo "Removing collectd stats of ${appid}" + echo "Removing collectd stats of ${metric}" for i in {1..10}; do - if rm -rf ${HOME}/platformdata/graphite/whisper/collectd/localhost/*${appid}*; then + if rm -rf ${HOME}/platformdata/graphite/whisper/collectd/localhost/*${metric}*; then break fi echo "Failed to remove collectd directory. collectd possibly generated data in the middle of removal"