maestro/app/classes/logical/services/SystemDManager.js
2020-02-21 00:36:55 -06:00

56 lines
2.1 KiB
JavaScript

const ServiceManager = require('./ServiceManager')
class SystemDManager extends ServiceManager {
_command_restart_service = 'systemctl restart %%SERVICE%%'
_command_start_service = 'systemctl start %%SERVICE%%'
_command_stop_service = 'systemctl stop %%SERVICE%%'
_command_reload_service = 'systemctl reload %%SERVICE%%'
_command_enable_service = 'systemctl enable %%SERVICE%%'
_command_disable_service = 'systemctl disable %%SERVICE%%'
_command_daemon_reload = 'systemctl daemon-reload'
async status(...services) {
const statuses = []
for ( const service_name of services ) {
const result = await this._host.execute(`systemctl status -l --value --no-pager ${service_name}`)
if ( result.exit_code !== 0 ) {
throw new Error(`Unable to determine service state: ${result.stderr.join('\n')}`)
}
const status = result.clean_out.map(x => x.trim().toLowerCase())
.filter(x => x.startsWith('active:'))[0]
.split(':')[1]
.trim()
statuses.push({
name: service_name,
status: this._determine_status_from_string(status)
})
}
return statuses.length === 1 ? statuses[0] : statuses
}
async install(config) {
if ( !config._build || !config.name ) throw new Error('Config must be instance of classes/logical/services/ServiceConfig')
const file_contents = config._build()
const name = config.name()
const service_file = await this._host.get_path(`/etc/systemd/system/${name}.service`)
await service_file.echo(file_contents)
}
_determine_status_from_string(str) {
if ( str.startsWith('active') ) {
return this.constructor.SERVICE_STATE_RUNNING
} else if ( str.startsWith('fail') ) {
return this.constructor.SERVICE_STATE_ERROR
} else {
return this.constructor.SERVICE_STATE_STOPPED
}
}
}
module.exports = exports = SystemDManager