const Directive = require('flitter-cli/Directive') const path = require('path') class Rollcall extends Directive { static get services() { return [...super.services, 'configs', 'hosts'] } static options() { return [ '--hosts -h {host file} | path to the host definition file' ] } static name() { return 'rollcall' } static help() { return 'Ping the configured hosts to determine if we can access them' } async handle(app, argv) { const host_path = this.option('hosts') if ( host_path ) { try { // Override the host config externally const host_conf = require(path.resolve(host_path)) if (typeof host_conf === 'object') { this.configs.canonical_items.hosts = host_conf this.info('Loaded host definition file.') } else { this.error('Invalid host definition file!') } } catch (e) { this.error('Invalid host definition file!') this.output.debug(e) } } const successes = [] const failures = [] const host_config = this.configs.get('hosts') const hostnames = Object.keys(host_config) for ( const name of hostnames ) { this.info(`Pinging ${name}...`) try { const host = this.hosts.get(name) const is_alive = await host.is_alive() if ( !is_alive ) { failures.push(name) this.error('Unable to ping host and verify execution.') } else { successes.push(name) this.success('Connected to host and verified execution.') } } catch(e) { failures.push(name) this.error('Unable to ping host and verify execution.') this.output.debug(e) } } this.hosts.close() this.info(`Successfully pinged ${successes.length} host(s).`) if ( failures.length > 0 ) this.info(`Unable to ping ${failures.length} host(s): ${failures.join(', ')}`) } } module.exports = exports = Rollcall