pbc/libs/systemd.js

50 lines
1.8 KiB
JavaScript

const logger = require('./logger.js');
const commands = require('./commands.js');
const constants = require('./constants.js');
const blinky = require('./blinky.js');
async function getServiceState(name) {
if (name === undefined) {
return;
}
logger.debug('checking service \'' + name + '\'...');
const state = await commands.execute('systemctl', ['is-active', name], true);
logger.debug('state of service \'' + name + '\' is \'' + state + '\'');
return { service: name, state: state };
}
async function setServiceState(name, args) {
if (name === undefined || args === undefined) {
return;
}
let state = args.get('state');
if (state === undefined) {
return;
}
if (state !== constants.SYSTEMD_STATE_ACTIVE && state !== constants.SYSTEMD_STATE_INACTIVE && state !== constants.SYSTEMD_STATE_TOGGLE) {
const msg = 'can not set state of service \'' + name + '\' to unknown state \'' + state + '\'';
logger.debug(msg);
return { err: msg };
}
let service = await getServiceState(name);
if (state !== constants.SYSTEMD_STATE_TOGGLE && service.state === state) {
const msg = 'service \'' + name + '\' is already in state \'' + service.state + '\'';
logger.debug(msg);
return { msg: msg };
}
let command;
if (service.state === constants.SYSTEMD_STATE_ACTIVE) {
command = 'stop';
state = constants.SYSTEMD_STATE_INACTIVE;
} else if (service.state === constants.SYSTEMD_STATE_INACTIVE) {
command = 'start';
state = constants.SYSTEMD_STATE_ACTIVE;
}
logger.debug('setting state of service \'' + name + '\' to \'' + state + '\'...');
return { service: name, state: state, result: await commands.execute('systemctl', [command, name], true)};
}
module.exports = {
getServiceState,
setServiceState
}