44 lines
1.7 KiB
JavaScript
44 lines
1.7 KiB
JavaScript
'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'),
|
|
shell = require('./shell.js');
|
|
|
|
const CONFIGURE_COLLECTD_CMD = path.join(__dirname, 'scripts/configurecollectd.sh');
|
|
|
|
async function addProfile(name, profile) {
|
|
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') || '';
|
|
if (currentProfile === profile) return;
|
|
|
|
if (!safe.fs.writeFileSync(configFilePath, profile)) throw new BoxError(BoxError.FS_ERROR, `Error writing collectd config: ${safe.error.message}`);
|
|
|
|
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');
|
|
}
|
|
|
|
async function removeProfile(name) {
|
|
assert.strictEqual(typeof name, 'string');
|
|
|
|
if (!safe.fs.unlinkSync(path.join(paths.COLLECTD_APPCONFIG_DIR, `${name}.conf`))) {
|
|
if (safe.error.code !== 'ENOENT') debug('Error removing collectd profile', safe.error);
|
|
}
|
|
|
|
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');
|
|
}
|