From e401809ad53f03a344fc9b553546ad14ec73d204 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Fri, 6 Mar 2020 21:14:15 -0600 Subject: [PATCH] Add service stopped state --- app/classes/state/os/ServiceStoppedState.js | 29 +++++++++++++++++++++ app/services/states.service.js | 3 ++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 app/classes/state/os/ServiceStoppedState.js diff --git a/app/classes/state/os/ServiceStoppedState.js b/app/classes/state/os/ServiceStoppedState.js new file mode 100644 index 0000000..696860c --- /dev/null +++ b/app/classes/state/os/ServiceStoppedState.js @@ -0,0 +1,29 @@ +const State = require('../State') + +class ServiceStoppedState extends State { + constructor(host, config) { + if ( !host.services ) { + throw new Error(`Cannot apply service stopped state to host ${host.name}: missing service manager API.`) + } + super(host, config) + } + + async reverse() { + const services = Array.isArray(this._config.service) ? this._config.service : [this._config.service] + return this._host.services.start(...services) + } + + async check() { + const services = Array.isArray(this._config.service) ? this._config.service : [this._config.service] + let states = await this._host.services.status(...services) + if ( !Array.isArray(states) ) states = [states] + return !states.some(x => x.status === 'running') + } + + async apply() { + const services = Array.isArray(this._config.service) ? this._config.service : [this._config.service] + return this._host.services.stop(...services) + } +} + +module.exports = exports = ServiceStoppedState diff --git a/app/services/states.service.js b/app/services/states.service.js index 00b5a20..54ae2d7 100644 --- a/app/services/states.service.js +++ b/app/services/states.service.js @@ -13,7 +13,7 @@ class StatesService extends Service { static #state_map = { // TODO apache and nginx states - virtual host, reverse proxy // TODO package repository states - import keys, install repository - // TODO service manager states - service enabled, service installed, stopped + // TODO service manager states - service enabled, service installed 'fs.file': require('../classes/state/fs/FileState'), 'fs.directory': require('../classes/state/fs/DirectoryState'), @@ -34,6 +34,7 @@ class StatesService extends Service { 'package.cache.clear': require('../classes/state/os/PackageCacheClearedState'), 'service.running': require('../classes/state/os/ServiceState'), + 'service.stopped': require('../classes/state/os/ServiceStoppedState'), 'service.restarted': require('../classes/state/os/ServiceRestartState'), 'service.daemon.reloaded': require('../classes/state/os/ServiceDaemonReloadState'),