diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index e1cbb20..7d015f3 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -88,6 +88,7 @@ class Vmfloaty c.option '--service STRING', String, 'Configured pooler service name' c.option '--active', 'Prints information about active vms for a given token' c.option '--json', 'Prints information as JSON' + c.option '--hostnameonly', 'When listing active vms, prints only hostnames, one per line' c.option '--token STRING', String, 'Token for pooler service' c.option '--url STRING', String, 'URL of pooler service' c.option '--user STRING', String, 'User to authenticate with' @@ -115,6 +116,10 @@ class Vmfloaty else if options.json puts Utils.get_host_data(verbose, service, running_vms).to_json + elsif options.hostnameonly + Utils.get_host_data(verbose, service, running_vms).each do |hostname, host_data| + Utils.print_fqdn_for_host(service, hostname, host_data) + end else puts "Your VMs on #{host}:" Utils.pretty_print_hosts(verbose, service, running_vms) diff --git a/lib/vmfloaty/utils.rb b/lib/vmfloaty/utils.rb index f6d69ef..65cc053 100644 --- a/lib/vmfloaty/utils.rb +++ b/lib/vmfloaty/utils.rb @@ -84,6 +84,21 @@ class Utils os_types end + def self.print_fqdn_for_host(service, hostname, host_data) + case service.type + when 'ABS' + host_data['allocated_resources'].each do |vm_name, _i| + puts vm_name['hostname'] + end + when 'Pooler' + puts "#{hostname}.#{host_data['domain']}" + when 'NonstandardPooler' + puts host_data['fqdn'] + else + raise "Invalid service type #{service.type}" + end + end + def self.pretty_print_hosts(verbose, service, hostnames = [], print_to_stderr = false, indent = 0) output_target = print_to_stderr ? $stderr : $stdout diff --git a/spec/vmfloaty/utils_spec.rb b/spec/vmfloaty/utils_spec.rb index 3941eb2..48f6a3e 100644 --- a/spec/vmfloaty/utils_spec.rb +++ b/spec/vmfloaty/utils_spec.rb @@ -162,6 +162,89 @@ describe Utils do end end + describe '#print_fqdn_for_host' do + let(:url) { 'http://pooler.example.com' } + + subject { Utils.print_fqdn_for_host(service, hostname, host_data) } + + describe 'with vmpooler host' do + let(:service) { Service.new(MockOptions.new, 'url' => url) } + let(:hostname) { 'mcpy42eqjxli9g2' } + let(:domain) { 'delivery.mycompany.net' } + let(:fqdn) { [hostname, domain].join('.') } + + let(:host_data) do + { + 'template' => 'ubuntu-1604-x86_64', + 'lifetime' => 12, + 'running' => 9.66, + 'state' => 'running', + 'ip' => '127.0.0.1', + 'domain' => domain, + } + end + + it 'outputs fqdn for host' do + expect(STDOUT).to receive(:puts).with(fqdn) + + subject + end + end + + describe 'with nonstandard pooler host' do + let(:service) { Service.new(MockOptions.new, 'url' => url, 'type' => 'ns') } + let(:hostname) { 'sol11-9.delivery.mycompany.net' } + let(:host_data) do + { + 'fqdn' => hostname, + 'os_triple' => 'solaris-11-sparc', + 'reserved_by_user' => 'first.last', + 'reserved_for_reason' => '', + 'hours_left_on_reservation' => 35.89, + } + end + let(:fqdn) { hostname } # for nspooler these are the same + + it 'outputs fqdn for host' do + expect(STDOUT).to receive(:puts).with(fqdn) + + subject + end + end + + describe 'with ABS host' do + let(:service) { Service.new(MockOptions.new, 'url' => url, 'type' => 'abs') } + let(:hostname) { '1597952189390' } + let(:fqdn) { 'example-noun.delivery.puppetlabs.net' } + let(:template) { 'ubuntu-1604-x86_64' } + + # This seems to be the miminal stub response from ABS for the current output + let(:host_data) do + { + 'state' => 'allocated', + 'allocated_resources' => [ + { + 'hostname' => fqdn, + 'type' => template, + 'enging' => 'vmpooler', + }, + ], + 'request' => { + 'job' => { + 'id' => hostname, + } + }, + } + end + + it 'outputs fqdn for host' do + expect(STDOUT).to receive(:puts).with(fqdn) + + subject + end + end + end + describe '#pretty_print_hosts' do let(:url) { 'http://pooler.example.com' }