Skip to content

Commit

Permalink
(maint) Fail in ABS when queue requests don't return 200/202
Browse files Browse the repository at this point in the history
When ABS returns an http status code other than 200 or 202, the ABS code
should fail
  • Loading branch information
mcdonaldseanp committed Aug 12, 2020
1 parent 506df6c commit 17c0085
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/vmfloaty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def run # rubocop:disable Metrics/AbcSize
running_vms = service.list_active(verbose)
host = URI.parse(service.url).host
if running_vms.empty?
if option.json
if options.json
puts {}.to_json
else
puts "You have no running VMs on #{host}"
Expand Down Expand Up @@ -210,6 +210,7 @@ def run # rubocop:disable Metrics/AbcSize
c.option '--all', 'Deletes all vms acquired by a token'
c.option '--json', 'Outputs list of returned hosts as JSON'
c.option '-f', 'Does not prompt user when deleting all vms'
c.option '--json', 'Outputs hosts scheduled for deletion as JSON'
c.option '--token STRING', String, 'Token for pooler service'
c.option '--url STRING', String, 'URL of pooler service'
c.action do |args, options|
Expand Down
24 changes: 21 additions & 3 deletions lib/vmfloaty/abs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ def self.retrieve(verbose, os_types, token, url, user, options, _ondemand = nil)

retries = 360

raise AuthError, "HTTP #{res.status}: The token provided could not authenticate to the pooler.\n#{res_body}" if res.status == 401
validate_queue_status_response(res.status, res.body, "Initial request", verbose)

(1..retries).each do |i|
queue_place, res_body = check_queue(conn, saved_job_id, req_obj)
queue_place, res_body = check_queue(conn, saved_job_id, req_obj, verbose)
return translated(res_body) if res_body

sleep_seconds = 10 if i >= 10
Expand Down Expand Up @@ -262,11 +262,12 @@ def self.translated(res_body)
vmpooler_formatted_body
end

def self.check_queue(conn, job_id, req_obj)
def self.check_queue(conn, job_id, req_obj, verbose)
queue_info_res = conn.get "status/queue/info/#{job_id}"
queue_info = JSON.parse(queue_info_res.body)

res = conn.post 'request', req_obj.to_json
validate_queue_status_response(res.status, res.body, "Check queue request", verbose)

unless res.body.empty?
res_body = JSON.parse(res.body)
Expand Down Expand Up @@ -315,4 +316,21 @@ def self.disk(_verbose, _url, _hostname, _token, _disk)
def self.revert(_verbose, _url, _hostname, _token, _snapshot_sha)
raise NoMethodError, 'revert is not defined for ABS'
end

# Validate the http code returned during a queue status request.
#
# Return a success message that can be displayed if the status code is
# success, otherwise raise an error.
def self.validate_queue_status_response(status_code, body, request_name, verbose)
case status_code
when 200
"#{request_name} returned success (Code 200)" if verbose
when 202
"#{request_name} returned accepted, processing (Code 202)" if verbose
when 401
raise AuthError, "HTTP #{status_code}: The token provided could not authenticate.\n#{body}"
else
raise "HTTP #{status_code}: #{request_name} request to ABS failed!\n#{body}"
end
end
end

0 comments on commit 17c0085

Please sign in to comment.