Files
cloudron-box/src/collectd.js
T

44 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-01-31 13:33:19 -08:00
'use strict';
exports = module.exports = {
addProfile,
removeProfile
};
const assert = require('assert'),
BoxError = require('./boxerror.js'),
debug = require('debug')('collectd'),
path = require('path'),
paths = require('./paths.js'),
safe = require('safetydance'),
2020-01-31 13:33:19 -08:00
shell = require('./shell.js');
const CONFIGURE_COLLECTD_CMD = path.join(__dirname, 'scripts/configurecollectd.sh');
2021-08-19 13:24:38 -07:00
async function addProfile(name, profile) {
2020-01-31 13:33:19 -08:00
assert.strictEqual(typeof name, 'string');
assert.strictEqual(typeof profile, 'string');
const configFilePath = path.join(paths.COLLECTD_APPCONFIG_DIR, `${name}.conf`);
// skip restarting collectd if the profile already exists with the same contents
const currentProfile = safe.fs.readFileSync(configFilePath, 'utf8') || '';
2021-08-19 13:24:38 -07:00
if (currentProfile === profile) return;
2021-08-19 13:24:38 -07:00
if (!safe.fs.writeFileSync(configFilePath, profile)) throw new BoxError(BoxError.FS_ERROR, `Error writing collectd config: ${safe.error.message}`);
2020-01-31 13:33:19 -08:00
2021-08-19 13:24:38 -07:00
const [error] = await safe(shell.promises.sudo('addCollectdProfile', [ CONFIGURE_COLLECTD_CMD, 'add', name ], {}));
if (error) throw new BoxError(BoxError.COLLECTD_ERROR, 'Could not add collectd config');
2020-01-31 13:33:19 -08:00
}
2021-08-19 13:24:38 -07:00
async function removeProfile(name) {
2020-01-31 13:33:19 -08:00
assert.strictEqual(typeof name, 'string');
2021-08-19 13:24:38 -07:00
if (!safe.fs.unlinkSync(path.join(paths.COLLECTD_APPCONFIG_DIR, `${name}.conf`))) {
if (safe.error.code !== 'ENOENT') debug('Error removing collectd profile', safe.error);
}
2020-01-31 13:33:19 -08:00
2021-08-19 13:24:38 -07:00
const [error] = await safe(shell.promises.sudo('removeCollectdProfile', [ CONFIGURE_COLLECTD_CMD, 'remove', name ], {}));
if (error) throw new BoxError(BoxError.COLLECTD_ERROR, 'Could not remove collectd config');
2020-01-31 13:33:19 -08:00
}