42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
const { Service } = require('flitter-di')
|
|
const LocalHost = require('../classes/metal/LocalHost')
|
|
const RemoteHost = require('../classes/metal/RemoteHost')
|
|
|
|
class hosts extends Service {
|
|
static get services() {
|
|
return [...super.services, 'configs']
|
|
}
|
|
|
|
_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' ) {
|
|
const host = new LocalHost(config)
|
|
this._running_hosts.push(host)
|
|
return host
|
|
} else if ( config.type === 'ssh' ) {
|
|
const host = new RemoteHost(config)
|
|
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
|