Add support for routines; state messages
This commit is contained in:
30
app/services/routines.service.js
Normal file
30
app/services/routines.service.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const { Service } = require('flitter-di')
|
||||
const Routine = require('../classes/routine/Routine')
|
||||
|
||||
/*
|
||||
* routines Service
|
||||
* -------------------------------------------------------------
|
||||
* This is a service file that will be made available through Flitter's
|
||||
* dependency injector to the rest of the application based on its given
|
||||
* canonical name.
|
||||
*
|
||||
* e.g. app.di().service("routines")
|
||||
*/
|
||||
class RoutinesService extends Service {
|
||||
static get services() {
|
||||
return [...super.services, 'app', 'configs', 'hosts']
|
||||
}
|
||||
|
||||
async get(name) {
|
||||
this.app.make(Routine)
|
||||
const config = this.configs.get(`routines:${name}`)
|
||||
const hosts = {}
|
||||
for ( const host_name of config.hosts ) {
|
||||
hosts[host_name] = await this.hosts.get(host_name)
|
||||
}
|
||||
|
||||
return new Routine(hosts, config, config.type)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = RoutinesService
|
||||
@@ -27,6 +27,7 @@ class StatesService extends Service {
|
||||
'git.tag': require('../classes/state/git/TagState'),
|
||||
|
||||
'os.cmd': require('../classes/state/os/CommandState'),
|
||||
'os.alive': require('../classes/state/os/IsAliveState'),
|
||||
|
||||
'package.present': require('../classes/state/os/PackageState'),
|
||||
'package.absent': require('../classes/state/os/PackageAbsentState'),
|
||||
@@ -45,13 +46,18 @@ class StatesService extends Service {
|
||||
return [...super.services, 'app', 'configs']
|
||||
}
|
||||
|
||||
map() {
|
||||
return this.constructor.#state_map
|
||||
}
|
||||
|
||||
from_config(host, state_config) {
|
||||
const type = state_config.type
|
||||
state_config = {...state_config}
|
||||
delete state_config.type
|
||||
|
||||
const StepClass = this.constructor.#state_map[type]
|
||||
this.app.di().make(StepClass)
|
||||
if ( !StepClass ) throw new Error(`Invalid or unknown step type: ${type}`)
|
||||
this.app.make(StepClass)
|
||||
|
||||
return new StepClass(host, state_config)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user