@@ -13,6 +13,7 @@ def self.list(verbose, url, os_filter = nil)
1313 os_list = [ ]
1414
1515 response = conn . get 'status/platforms/vmpooler'
16+
1617 response_body = JSON . parse ( response . body )
1718 os_list << "*** VMPOOLER Pools ***"
1819 os_list = os_list + JSON . parse ( response_body [ "vmpooler_platforms" ] )
@@ -31,89 +32,98 @@ def self.list(verbose, url, os_filter = nil)
3132
3233 os_list . delete 'ok'
3334
34- puts os_list
35-
3635 os_filter ? os_list . select { |i | i [ /#{ os_filter } / ] } : os_list
3736 end
3837
39- # List active VMs from ABS
40- def self . list_active ( verbose , url , token )
41- status = Auth . token_status ( verbose , url , token )
42- status [ 'reserved_hosts' ] || [ ]
43- end
38+ # Retrieve an OS from ABS.
39+ def self . retrieve ( verbose , os_types , token , url , user )
40+ #
41+ # Contents of post must be:j
42+ #
43+ # {
44+ # "resources": {
45+ # "centos-7-i386": 1,
46+ # "ubuntu-1404-x86_64": 2
47+ # },
48+ # "job": {
49+ # "id": "12345",
50+ # "tags": {
51+ # "user": "jenkins",
52+ # "jenkins_build_url": "https://jenkins/job/platform_puppet_intn-van-sys_master"
53+ # }
54+ # }
55+ # }
4456
45- def self . retrieve ( verbose , os_type , token , url )
4657 conn = Http . get_conn ( verbose , url )
4758 conn . headers [ 'X-AUTH-TOKEN' ] = token if token
4859
49- os_string = os_type . map { |os , num | Array ( os ) * num } . flatten . join ( '+' )
50- raise MissingParamError , 'No operating systems provided to obtain.' if os_string . empty?
51-
52- response = conn . post "host/#{ os_string } "
53-
54- res_body = JSON . parse ( response . body )
60+ saved_job_id = Time . now . to_i
5561
56- if res_body [ 'ok' ]
57- res_body
58- elsif response . status == 401
59- raise AuthError , "HTTP #{ response . status } : The token provided could not authenticate to the pooler.\n #{ res_body } "
60- else
61- raise "HTTP #{ response . status } : Failed to obtain VMs from the pooler at #{ url } /host/#{ os_string } . #{ res_body } "
62- end
63- end
62+ reqObj = {
63+ resources : os_types ,
64+ job : {
65+ id : saved_job_id ,
66+ tags : {
67+ user : user ,
68+ url_string : "floaty://#{ user } /#{ saved_job_id } "
69+ }
70+ }
71+ }
6472
65- def self . modify ( verbose , url , hostname , token , modify_hash )
66- raise TokenError , 'Token provided was nil; Request cannot be made to modify VM' if token . nil?
73+ # os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
74+ # raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
75+ puts "Requesting VMs with job_id: #{ saved_job_id } . Will retry for up to an hour."
76+ response = conn . post "api/v2/request" , reqObj . to_json
6777
68- modify_hash . each do |key , _value |
69- raise ModifyError , "Configured service type does not support modification of #{ key } " unless %i[ reason reserved_for_reason ] . include? key
70- end
78+ i = 0
79+ retries = 360
7180
72- if modify_hash [ :reason ]
73- # "reason" is easier to type than "reserved_for_reason", but nspooler needs the latter
74- modify_hash [ :reserved_for_reason ] = modify_hash . delete :reason
81+ if response . status == 401
82+ raise AuthError , "HTTP #{ response . status } : The token provided could not authenticate to the pooler.\n #{ res_body } "
7583 end
7684
77- conn = Http . get_conn ( verbose , url )
78- conn . headers [ 'X-AUTH-TOKEN' ] = token
85+ ( 1 ..retries ) . each do |i |
86+ queue_place , res_body = check_queue ( conn , saved_job_id , reqObj )
87+ if res_body
88+ return translated ( res_body )
89+ end
7990
80- response = conn . put do |req |
81- req . url "host/#{ hostname } "
82- req . body = modify_hash . to_json
91+ puts "Waiting 10 seconds to check if ABS request has been filled. Queue Position: #{ queue_place } ... (x#{ i } )"
92+ sleep ( 10 )
8393 end
84-
85- response . body . empty? ? { } : JSON . parse ( response . body )
94+ return nil
8695 end
8796
88- def self . disk ( _verbose , _url , _hostname , _token , _disk )
89- raise ModifyError , 'Configured service type does not support modification of disk space'
90- end
91-
92- def self . snapshot ( _verbose , _url , _hostname , _token )
93- raise ModifyError , 'Configured service type does not support snapshots'
94- end
97+ #
98+ # We should fix the ABS API to be more like the vmpooler or nspooler api, but for now
99+ #
100+ def self . translated res_body
101+ vmpooler_formatted_body = Hash . new
102+
103+ res_body . each do |host |
104+ if vmpooler_formatted_body [ host [ "type" ] ] && vmpooler_formatted_body [ host [ "type" ] ] [ "hostname" ] . class == Array
105+ vmpooler_formatted_body [ host [ "type" ] ] [ "hostname" ] << host [ "hostname" ]
106+ else
107+ vmpooler_formatted_body [ host [ "type" ] ] = { "hostname" => [ host [ "hostname" ] ] }
108+ end
109+ end
110+ vmpooler_formatted_body [ "ok" ] = true
95111
96- def self . revert ( _verbose , _url , _hostname , _token , _snapshot_sha )
97- raise ModifyError , 'Configured service type does not support snapshots'
112+ return vmpooler_formatted_body
98113 end
99114
100- def self . delete ( verbose , url , hosts , token )
101- raise TokenError , 'Token provided was nil; Request cannot be made to delete VM' if token . nil?
115+ def self . check_queue conn , job_id , reqObj
116+ queue_info_response = conn . get "/status/queue/info/#{ job_id } "
117+ queue_info = JSON . parse ( queue_info_response . body )
102118
103- conn = Http . get_conn ( verbose , url )
119+ response = conn . post "api/v2/request" , reqObj . to_json
104120
105- conn . headers [ 'X-AUTH-TOKEN' ] = token if token
106121
107- response_body = { }
108-
109- hosts = hosts . split ( ',' ) unless hosts . is_a? Array
110- hosts . each do |host |
111- response = conn . delete "host/#{ host } "
122+ if response . body . length > 0
112123 res_body = JSON . parse ( response . body )
113- response_body [ host ] = res_body
124+ return queue_info [ "queue_place" ] , res_body
114125 end
115-
116- response_body
126+ return queue_info [ "queue_place" ] , nil
117127 end
118128
119129 def self . status ( verbose , url )
0 commit comments