@@ -37,38 +37,39 @@ class ABS
3737 # }
3838 #
3939
40- @@ active_hostnames = Hash . new
40+ @active_hostnames = { }
4141
4242 def self . list_active ( verbose , url , _token , user )
43- all_jobs = Array . new ( )
44- @@ active_hostnames = Hash . new
43+ all_jobs = [ ]
44+ @active_hostnames = { }
4545
46- self . get_active_requests ( verbose , url , user ) . each do |reqHash |
47- all_jobs . push ( reqHash [ 'request' ] [ 'job' ] [ 'id' ] )
48- @@ active_hostnames [ reqHash [ 'request' ] [ 'job' ] [ 'id' ] ] = reqHash
46+ get_active_requests ( verbose , url , user ) . each do |req_hash |
47+ all_jobs . push ( req_hash [ 'request' ] [ 'job' ] [ 'id' ] )
48+ @active_hostnames [ req_hash [ 'request' ] [ 'job' ] [ 'id' ] ] = req_hash
4949 end
5050
5151 all_jobs
5252 end
5353
54- def self . get_active_requests verbose , url , user
54+ def self . get_active_requests ( verbose , url , user )
5555 conn = Http . get_conn ( verbose , url )
5656 res = conn . get 'status/queue'
5757 requests = JSON . parse ( res . body )
5858
59- retVal = [ ]
59+ ret_val = [ ]
6060 requests . each do |req |
61- reqHash = JSON . parse ( req )
62- next unless user == reqHash [ 'request' ] [ 'job' ] [ 'user' ]
63- retVal . push ( reqHash )
61+ req_hash = JSON . parse ( req )
62+ next unless user == req_hash [ 'request' ] [ 'job' ] [ 'user' ]
63+
64+ ret_val . push ( req_hash )
6465 end
6566
66- retVal
67+ ret_val
6768 end
6869
6970 def self . all_job_resources_accounted_for ( allocated_resources , hosts )
70- allocated_host_list = allocated_resources . map { |ar | ar [ " hostname" ] }
71- return ( allocated_host_list - hosts ) . empty?
71+ allocated_host_list = allocated_resources . map { |ar | ar [ ' hostname' ] }
72+ ( allocated_host_list - hosts ) . empty?
7273 end
7374
7475 def self . delete ( verbose , url , hosts , token , user )
@@ -77,19 +78,19 @@ def self.delete(verbose, url, hosts, token, user)
7778 conn . headers [ 'X-AUTH-TOKEN' ] = token if token
7879
7980 puts "Trying to delete hosts #{ hosts } " if verbose
80- requests = self . get_active_requests ( verbose , url , user )
81+ requests = get_active_requests ( verbose , url , user )
8182
8283 jobs_to_delete = [ ]
8384
84- requests . each do |reqHash |
85- if reqHash [ 'state' ] == 'allocated' || reqHash [ 'state' ] == 'filled'
86- reqHash [ 'allocated_resources' ] . each do | vm_name , i |
87- if hosts . include? vm_name [ "hostname" ]
88- if ( all_job_resources_accounted_for ( job [ 'allocated_resources' ] , hosts ) )
89- jobs_to_delete . push ( reqHash )
90- else
91- puts "Can't delete #{ job_id } : #{ hosts } does not include all of #{ job [ 'allocated_resources' ] } "
92- end
85+ requests . each do |req_hash |
86+ next unless req_hash [ 'state' ] == 'allocated' || req_hash [ 'state' ] == 'filled'
87+
88+ req_hash [ 'allocated_resources' ] . each do | vm_name , _i |
89+ if hosts . include? vm_name [ 'hostname' ]
90+ if all_job_resources_accounted_for ( job [ 'allocated_resources' ] , hosts )
91+ jobs_to_delete . push ( req_hash )
92+ else
93+ puts "Can't delete #{ job_id } : #{ hosts } does not include all of #{ job [ 'allocated_resources' ] } "
9394 end
9495 end
9596 end
@@ -98,21 +99,20 @@ def self.delete(verbose, url, hosts, token, user)
9899 response_body = { }
99100
100101 jobs_to_delete . each do |job |
101- reqObj = {
102- 'job_id' : job [ 'request' ] [ 'job' ] [ 'id' ] ,
103- 'hosts' : job [ 'allocated_resources' ] ,
102+ req_obj = {
103+ 'job_id' => job [ 'request' ] [ 'job' ] [ 'id' ] ,
104+ 'hosts' => job [ 'allocated_resources' ] ,
104105 }
105106
106- puts "Deleting #{ reqObj } " if verbose
107+ puts "Deleting #{ req_obj } " if verbose
107108
108- res = conn . post 'api/v2/return' , reqObj . to_json
109+ _ = conn . post 'api/v2/return' , req_obj . to_json
109110 response_body [ job_id ] = res_body
110111 end
111112
112- return response_body
113+ response_body
113114 end
114115
115-
116116 # List available VMs in ABS
117117 def self . list ( verbose , url , os_filter = nil )
118118 conn = Http . get_conn ( verbose , url )
@@ -155,40 +155,37 @@ def self.retrieve(verbose, os_types, token, url, user)
155155 # "job": {
156156 # "id": "12345",
157157 # "tags": {
158- # "user": "jenkins",
159- # "jenkins_build_url": "https://jenkins/job/platform_puppet_intn-van-sys_master"
158+ # "user": "username",
160159 # }
161160 # }
162161 # }
163162
164163 conn = Http . get_conn ( verbose , url )
165164 conn . headers [ 'X-AUTH-TOKEN' ] = token if token
166165
167- saved_job_id = Time . now . to_i
166+ saved_job_id = DateTime . now . strftime ( '%Q' )
168167
169- reqObj = {
168+ req_obj = {
170169 :resources => os_types ,
171170 :job => {
172171 :id => saved_job_id ,
173172 :tags => {
174- :user => user ,
175- :url_string => "floaty://#{ user } /#{ saved_job_id } " ,
173+ :user => user ,
176174 } ,
177175 } ,
178176 }
179177
180178 # os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
181179 # raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
182180 puts "Requesting VMs with job_id: #{ saved_job_id } . Will retry for up to an hour."
183- res = conn . post 'api/v2/request' , reqObj . to_json
181+ res = conn . post 'api/v2/request' , req_obj . to_json
184182
185- i = 0
186183 retries = 360
187184
188185 raise AuthError , "HTTP #{ res . status } : The token provided could not authenticate to the pooler.\n #{ res_body } " if res . status == 401
189186
190187 ( 1 ..retries ) . each do |i |
191- queue_place , res_body = check_queue ( conn , saved_job_id , reqObj )
188+ queue_place , res_body = check_queue ( conn , saved_job_id , req_obj )
192189 return translated ( res_body ) if res_body
193190
194191 puts "Waiting 10 seconds to check if ABS request has been filled. Queue Position: #{ queue_place } ... (x#{ i } )"
@@ -215,11 +212,11 @@ def self.translated(res_body)
215212 vmpooler_formatted_body
216213 end
217214
218- def self . check_queue ( conn , job_id , reqObj )
215+ def self . check_queue ( conn , job_id , req_obj )
219216 queue_info_res = conn . get "/status/queue/info/#{ job_id } "
220217 queue_info = JSON . parse ( queue_info_res . body )
221218
222- res = conn . post 'api/v2/request' , reqObj . to_json
219+ res = conn . post 'api/v2/request' , req_obj . to_json
223220
224221 unless res . body . empty?
225222 res_body = JSON . parse ( res . body )
@@ -228,7 +225,7 @@ def self.check_queue(conn, job_id, reqObj)
228225 [ queue_info [ 'queue_place' ] , nil ]
229226 end
230227
231- def self . snapshot ( verbose , url , hostname , token )
228+ def self . snapshot ( _verbose , _url , _hostname , _token )
232229 puts "Can't snapshot with ABS, use '--service vmpooler' (even for vms checked out with ABS)"
233230 end
234231
@@ -247,7 +244,8 @@ def self.summary(verbose, url)
247244 end
248245
249246 def self . query ( verbose , url , hostname )
250- return @@active_hostnames if @@active_hostnames
247+ return @active_hostnames if @active_hostnames
248+
251249 puts "For vmpooler/snapshot information, use '--service vmpooler' (even for vms checked out with ABS)"
252250 conn = Http . get_conn ( verbose , url )
253251
0 commit comments