Use Logger for non-result output

This adds the Logger class for log output, all of which goes to STDERR,
and uses puts to send only the command result to STDOUT.
This commit is contained in:
barriserloth 2020-08-11 10:31:14 -07:00 committed by Austin Blatt
parent 4755343df2
commit 8ec90007ca
7 changed files with 81 additions and 67 deletions

View file

@ -68,7 +68,7 @@ class ABS
ret_val.push(req_hash)
rescue NoMethodError
STDERR.puts "Warning: couldn't parse line returned from abs/status/queue: ".yellow
Vmfloaty.logger.warn "Warning: couldn't parse line returned from abs/status/queue: "
end
end
@ -85,7 +85,7 @@ class ABS
conn = Http.get_conn(verbose, url)
conn.headers['X-AUTH-TOKEN'] = token if token
STDERR.puts "Trying to delete hosts #{hosts}" if verbose
Vmfloaty.logger.info "Trying to delete hosts #{hosts}" if verbose
requests = get_active_requests(verbose, url, user)
jobs_to_delete = []
@ -113,7 +113,7 @@ class ABS
}
jobs_to_delete.push(req_hash)
else
STDERR.puts "When using ABS you must delete all vms that you requested at the same time: Can't delete #{req_hash['request']['job']['id']}: #{hosts} does not include all of #{req_hash['allocated_resources']}"
Vmfloaty.logger.info "When using ABS you must delete all vms that you requested at the same time: Can't delete #{req_hash['request']['job']['id']}: #{hosts} does not include all of #{req_hash['allocated_resources']}"
end
end
end
@ -127,7 +127,7 @@ class ABS
'hosts' => job['allocated_resources'],
}
STDERR.puts "Deleting #{req_obj}" if verbose
Vmfloaty.logger.info "Deleting #{req_obj}" if verbose
return_result = conn.post 'return', req_obj.to_json
req_obj['hosts'].each do |host|
@ -220,11 +220,11 @@ class ABS
end
end
STDERR.puts "Posting to ABS #{req_obj.to_json}" if verbose
Vmfloaty.logger.info "Posting to ABS #{req_obj.to_json}" if verbose
# os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
# raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
STDERR.puts "Requesting VMs with job_id: #{saved_job_id}. Will retry for up to an hour."
Vmfloaty.logger.info "Requesting VMs with job_id: #{saved_job_id}. Will retry for up to an hour."
res = conn.post 'request', req_obj.to_json
retries = 360
@ -237,7 +237,7 @@ class ABS
sleep_seconds = 10 if i >= 10
sleep_seconds = i if i < 10
STDERR.puts "Waiting #{sleep_seconds} seconds to check if ABS request has been filled. Queue Position: #{queue_place}... (x#{i})"
Vmfloaty.logger.info "Waiting #{sleep_seconds} seconds to check if ABS request has been filled. Queue Position: #{queue_place}... (x#{i})"
sleep(sleep_seconds)
end
@ -276,7 +276,7 @@ class ABS
end
def self.snapshot(_verbose, _url, _hostname, _token)
STDERR.puts "Can't snapshot with ABS, use '--service vmpooler' (even for vms checked out with ABS)"
Vmfloaty.logger.info "Can't snapshot with ABS, use '--service vmpooler' (even for vms checked out with ABS)"
end
def self.status(verbose, url)
@ -297,7 +297,7 @@ class ABS
def self.query(verbose, url, hostname)
return @active_hostnames if @active_hostnames
STDERR.puts "For vmpooler/snapshot information, use '--service vmpooler' (even for vms checked out with ABS)"
Vmfloaty.logger.info "For vmpooler/snapshot information, use '--service vmpooler' (even for vms checked out with ABS)"
conn = Http.get_conn(verbose, url)
res = conn.get "host/#{hostname}"

View file

@ -8,7 +8,7 @@ class Conf
begin
conf = YAML.load_file("#{Dir.home}/.vmfloaty.yml")
rescue StandardError
STDERR.puts "WARNING: There was no config file at #{Dir.home}/.vmfloaty.yml"
Vmfloaty.logger.warn "WARNING: There was no config file at #{Dir.home}/.vmfloaty.yml"
end
conf
end

11
lib/vmfloaty/logger.rb Normal file
View file

@ -0,0 +1,11 @@
require 'logger'
class FloatyLogger < ::Logger
def initialize
super(STDERR)
self.level = ::Logger::INFO
self.formatter = proc do |severity, datetime, progname, msg|
"#{msg}\n"
end
end
end

View file

@ -60,10 +60,10 @@ class Pooler
while check_ondemandvm(verbose, request_id, url) == false
return false if (Time.now - start_time).to_i > timeout
STDOUT.puts "waiting for request #{request_id} to be fulfilled"
Vmfloaty.logger.info "waiting for request #{request_id} to be fulfilled"
sleep 5
end
STDOUT.puts "The request has been fulfilled"
Vmfloaty.logger.info "The request has been fulfilled"
check_ondemandvm(verbose, request_id, url)
end

View file

@ -36,7 +36,7 @@ class Service
def user
unless @config['user']
STDERR.puts "Enter your #{@config['url']} service username:"
Vmfloaty.logger.info "Enter your #{@config['url']} service username:"
@config['user'] = STDIN.gets.chomp
end
@config['user']
@ -44,7 +44,7 @@ class Service
def token
unless @config['token']
STDERR.puts 'No token found. Retrieving a token...'
Vmfloaty.logger.info 'No token found. Retrieving a token...'
@config['token'] = get_new_token(nil)
end
@config['token']
@ -76,7 +76,7 @@ class Service
end
def retrieve(verbose, os_types, use_token = true, ondemand = nil)
STDERR.puts 'Requesting a vm without a token...' unless use_token
Vmfloaty.logger.info 'Requesting a vm without a token...' unless use_token
token_value = use_token ? token : nil
@service_object.retrieve verbose, os_types, token_value, url, user, @config, ondemand
end
@ -91,8 +91,8 @@ class Service
begin
token_value = token || get_new_token(verbose)
rescue TokenError => e
STDERR.puts e
STDERR.puts 'Could not get token... requesting vm without a token anyway...'
Vmfloaty.logger.error e
Vmfloaty.logger.info 'Could not get token... requesting vm without a token anyway...'
end
end
Ssh.ssh(verbose, self, host_os, token_value)

View file

@ -45,7 +45,6 @@ class Utils
result = {}
STDERR.puts "response body is #{response_body}"
filtered_response_body = response_body.reject { |key, _| key == 'request_id' || key == 'ready' }
filtered_response_body.each do |os, value|
hostnames = Array(value['hostname'])
@ -91,7 +90,7 @@ class Utils
# For ABS, 'hostname' variable is the jobID
if host_data['state'] == 'allocated' || host_data['state'] == 'filled'
host_data['allocated_resources'].each do |vm_name, _i|
STDOUT.puts "- [JobID:#{host_data['request']['job']['id']}] #{vm_name['hostname']} (#{vm_name['type']}) <#{host_data['state']}>"
puts "- [JobID:#{host_data['request']['job']['id']}] #{vm_name['hostname']} (#{vm_name['type']}) <#{host_data['state']}>"
end
end
when 'Pooler'
@ -99,19 +98,19 @@ class Utils
tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" } unless host_data['tags'].nil?
duration = "#{host_data['running']}/#{host_data['lifetime']} hours"
metadata = [host_data['template'], duration, *tag_pairs]
STDOUT.puts "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})"
puts "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})"
when 'NonstandardPooler'
line = "- #{host_data['fqdn']} (#{host_data['os_triple']}"
line += ", #{host_data['hours_left_on_reservation']}h remaining"
line += ", reason: #{host_data['reserved_for_reason']}" unless host_data['reserved_for_reason'].empty?
line += ')'
STDOUT.puts line
puts line
else
raise "Invalid service type #{service.type}"
end
rescue StandardError => e
STDERR.puts("Something went wrong while trying to gather information on #{hostname}:")
STDERR.puts(e)
Vmfloaty.logger.error("Something went wrong while trying to gather information on #{hostname}:")
Vmfloaty.logger.error(e)
end
end
end
@ -133,12 +132,12 @@ class Utils
pending = pool['pending']
missing = max - ready - pending
char = 'o'
STDOUT.puts "#{name.ljust(width)} #{(char * ready).green}#{(char * pending).yellow}#{(char * missing).red}"
puts "#{name.ljust(width)} #{(char * ready).green}#{(char * pending).yellow}#{(char * missing).red}"
rescue StandardError => e
STDERR.puts "#{name.ljust(width)} #{e.red}"
Vmfloaty.logger.error "#{name.ljust(width)} #{e.red}"
end
end
STDOUT.puts message.colorize(status_response['status']['ok'] ? :default : :red)
puts message.colorize(status_response['status']['ok'] ? :default : :red)
when 'NonstandardPooler'
pools = status_response
pools.delete 'ok'
@ -152,14 +151,14 @@ class Utils
pending = pool['pending'] || 0 # not available for nspooler
missing = max - ready - pending
char = 'o'
STDOUT.puts "#{name.ljust(width)} #{(char * ready).green}#{(char * pending).yellow}#{(char * missing).red}"
puts "#{name.ljust(width)} #{(char * ready).green}#{(char * pending).yellow}#{(char * missing).red}"
rescue StandardError => e
STDERR.puts "#{name.ljust(width)} #{e.red}"
Vmfloaty.logger.error "#{name.ljust(width)} #{e.red}"
end
end
when 'ABS'
STDERR.puts 'ABS Not OK'.red unless status_response
STDOUT.puts 'ABS is OK'.green if status_response
Vmfloaty.logger.error 'ABS Not OK' unless status_response
puts 'ABS is OK'.green if status_response
else
raise "Invalid service type #{service.type}"
end