maestro/app/services/hosts.service.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-02-21 06:36:55 +00:00
const { Service } = require('flitter-di')
const LocalHost = require('../classes/metal/LocalHost')
const RemoteHost = require('../classes/metal/RemoteHost')
class hosts extends Service {
static get services() {
2020-06-06 22:43:31 +00:00
return [...super.services, 'configs', 'app']
2020-02-21 06:36:55 +00:00
}
_running_hosts = []
get config() {
return this.configs.get('hosts')
}
get(name) {
const config = this.config[name]
config.name = name
if ( !config ) {
throw new Error(`Could not get host ${name}: No such host configured.`)
}
if ( config.type === 'localhost' ) {
2020-06-06 22:43:31 +00:00
const host = this.app.di().make(LocalHost, config)
2020-02-21 06:36:55 +00:00
this._running_hosts.push(host)
return host
} else if ( config.type === 'ssh' ) {
2020-06-06 22:43:31 +00:00
const host = this.app.di().make(RemoteHost, config)
2020-02-21 06:36:55 +00:00
this._running_hosts.push(host)
return host
} else {
throw new Error(`Unknown host type ${config.type} for host ${name}.`)
}
}
close() {
this._running_hosts.forEach(h => h._cleanup())
}
}
module.exports = exports = hosts