Fix print_to_stderr option for Utils.pretty_print_hosts

Previously this option had no effect on the output. This commit makes
the option effective and also refactors the tests for this method to
cover this case.
This commit is contained in:
Jesse Scott 2020-08-21 11:42:12 -07:00
parent f3cd540455
commit 40072e90e4
No known key found for this signature in database
GPG key ID: 285826E26E0AAD93

View file

@ -85,6 +85,8 @@ class Utils
end end
def self.pretty_print_hosts(verbose, service, hostnames = [], print_to_stderr = false, indent = 0) def self.pretty_print_hosts(verbose, service, hostnames = [], print_to_stderr = false, indent = 0)
output_target = print_to_stderr ? $stderr : $stdout
fetched_data = self.get_host_data(verbose, service, hostnames) fetched_data = self.get_host_data(verbose, service, hostnames)
fetched_data.each do |hostname, host_data| fetched_data.each do |hostname, host_data|
case service.type case service.type
@ -96,7 +98,8 @@ class Utils
vmpooler_service = service.clone vmpooler_service = service.clone
vmpooler_service.silent = true vmpooler_service.silent = true
vmpooler_service.maybe_use_vmpooler vmpooler_service.maybe_use_vmpooler
puts "- [JobID:#{host_data['request']['job']['id']}] <#{host_data['state']}>" output_target.puts "- [JobID:#{host_data['request']['job']['id']}] <#{host_data['state']}>"
host_data['allocated_resources'].each do |vm_name, _i| host_data['allocated_resources'].each do |vm_name, _i|
self.pretty_print_hosts(verbose, vmpooler_service, vm_name['hostname'].split('.')[0], print_to_stderr, indent+2) self.pretty_print_hosts(verbose, vmpooler_service, vm_name['hostname'].split('.')[0], print_to_stderr, indent+2)
end end
@ -105,13 +108,13 @@ class Utils
tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" } unless host_data['tags'].nil? tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" } unless host_data['tags'].nil?
duration = "#{host_data['running']}/#{host_data['lifetime']} hours" duration = "#{host_data['running']}/#{host_data['lifetime']} hours"
metadata = [host_data['template'], duration, *tag_pairs] metadata = [host_data['template'], duration, *tag_pairs]
puts "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent) output_target.puts "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})".gsub(/^/, ' ' * indent)
when 'NonstandardPooler' when 'NonstandardPooler'
line = "- #{host_data['fqdn']} (#{host_data['os_triple']}" line = "- #{host_data['fqdn']} (#{host_data['os_triple']}"
line += ", #{host_data['hours_left_on_reservation']}h remaining" line += ", #{host_data['hours_left_on_reservation']}h remaining"
line += ", reason: #{host_data['reserved_for_reason']}" unless host_data['reserved_for_reason'].empty? line += ", reason: #{host_data['reserved_for_reason']}" unless host_data['reserved_for_reason'].empty?
line += ')' line += ')'
puts line output_target.puts line
else else
raise "Invalid service type #{service.type}" raise "Invalid service type #{service.type}"
end end