maestro/app/services/hosts.service.js
2020-06-06 17:43:31 -05:00

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', 'app']
}
_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 = this.app.di().make(LocalHost, config)
this._running_hosts.push(host)
return host
} else if ( config.type === 'ssh' ) {
const host = this.app.di().make(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