2020-02-27 06:31:07 +00:00
|
|
|
const { Service } = require('flitter-di')
|
|
|
|
|
|
|
|
/*
|
|
|
|
* states 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("states")
|
|
|
|
*/
|
|
|
|
class StatesService extends Service {
|
|
|
|
static #state_map = {
|
|
|
|
// TODO apache and nginx states - virtual host, reverse proxy
|
|
|
|
// TODO package repository states - import keys, install repository
|
2020-03-07 03:14:15 +00:00
|
|
|
// TODO service manager states - service enabled, service installed
|
2020-02-27 06:31:07 +00:00
|
|
|
|
|
|
|
'fs.file': require('../classes/state/fs/FileState'),
|
|
|
|
'fs.directory': require('../classes/state/fs/DirectoryState'),
|
2020-03-03 22:10:10 +00:00
|
|
|
'fs.unpack': require('../classes/state/fs/UnpackState'),
|
2020-03-04 03:06:59 +00:00
|
|
|
'fs.pack': require('../classes/state/fs/PackState'),
|
2020-03-03 22:47:51 +00:00
|
|
|
'fs.permission': require('../classes/state/fs/PermissionState'),
|
|
|
|
'fs.ownership': require('../classes/state/fs/OwnerState'),
|
2020-02-27 06:31:07 +00:00
|
|
|
|
2020-03-05 17:48:38 +00:00
|
|
|
'git.clone': require('../classes/state/git/CloneState'),
|
|
|
|
'git.checkout': require('../classes/state/git/CheckoutState'),
|
2020-03-05 18:15:00 +00:00
|
|
|
'git.tag': require('../classes/state/git/TagState'),
|
2020-03-05 17:48:38 +00:00
|
|
|
|
2020-03-05 16:30:24 +00:00
|
|
|
'os.cmd': require('../classes/state/os/CommandState'),
|
2020-04-15 14:11:10 +00:00
|
|
|
'os.alive': require('../classes/state/os/IsAliveState'),
|
2020-03-05 16:30:24 +00:00
|
|
|
|
2020-02-27 06:31:07 +00:00
|
|
|
'package.present': require('../classes/state/os/PackageState'),
|
2020-03-06 15:49:08 +00:00
|
|
|
'package.absent': require('../classes/state/os/PackageAbsentState'),
|
2020-02-27 06:31:07 +00:00
|
|
|
'package.updates': require('../classes/state/os/UpdateState'),
|
|
|
|
'package.cache.clear': require('../classes/state/os/PackageCacheClearedState'),
|
|
|
|
|
|
|
|
'service.running': require('../classes/state/os/ServiceState'),
|
2020-03-07 03:14:15 +00:00
|
|
|
'service.stopped': require('../classes/state/os/ServiceStoppedState'),
|
2020-02-27 06:31:07 +00:00
|
|
|
'service.restarted': require('../classes/state/os/ServiceRestartState'),
|
|
|
|
'service.daemon.reloaded': require('../classes/state/os/ServiceDaemonReloadState'),
|
2020-03-03 22:10:10 +00:00
|
|
|
|
|
|
|
'web.download': require('../classes/state/fs/DownloadState'),
|
2020-02-27 06:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'app', 'configs']
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:11:10 +00:00
|
|
|
map() {
|
|
|
|
return this.constructor.#state_map
|
|
|
|
}
|
|
|
|
|
2020-02-27 06:31:07 +00:00
|
|
|
from_config(host, state_config) {
|
|
|
|
const type = state_config.type
|
2020-04-15 14:11:10 +00:00
|
|
|
state_config = {...state_config}
|
2020-02-27 06:31:07 +00:00
|
|
|
delete state_config.type
|
|
|
|
|
|
|
|
const StepClass = this.constructor.#state_map[type]
|
|
|
|
if ( !StepClass ) throw new Error(`Invalid or unknown step type: ${type}`)
|
2020-04-15 14:11:10 +00:00
|
|
|
this.app.make(StepClass)
|
2020-02-27 06:31:07 +00:00
|
|
|
|
|
|
|
return new StepClass(host, state_config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = StatesService
|