@@ -37,38 +37,39 @@ class ABS
37
37
# }
38
38
#
39
39
40
- @@ active_hostnames = Hash . new
40
+ @active_hostnames = { }
41
41
42
42
def self . list_active ( verbose , url , _token , user )
43
- all_jobs = Array . new ( )
44
- @@ active_hostnames = Hash . new
43
+ all_jobs = [ ]
44
+ @active_hostnames = { }
45
45
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
49
49
end
50
50
51
51
all_jobs
52
52
end
53
53
54
- def self . get_active_requests verbose , url , user
54
+ def self . get_active_requests ( verbose , url , user )
55
55
conn = Http . get_conn ( verbose , url )
56
56
res = conn . get 'status/queue'
57
57
requests = JSON . parse ( res . body )
58
58
59
- retVal = [ ]
59
+ ret_val = [ ]
60
60
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 )
64
65
end
65
66
66
- retVal
67
+ ret_val
67
68
end
68
69
69
70
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?
72
73
end
73
74
74
75
def self . delete ( verbose , url , hosts , token , user )
@@ -77,19 +78,19 @@ def self.delete(verbose, url, hosts, token, user)
77
78
conn . headers [ 'X-AUTH-TOKEN' ] = token if token
78
79
79
80
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 )
81
82
82
83
jobs_to_delete = [ ]
83
84
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' ] } "
93
94
end
94
95
end
95
96
end
@@ -98,21 +99,20 @@ def self.delete(verbose, url, hosts, token, user)
98
99
response_body = { }
99
100
100
101
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' ] ,
104
105
}
105
106
106
- puts "Deleting #{ reqObj } " if verbose
107
+ puts "Deleting #{ req_obj } " if verbose
107
108
108
- res = conn . post 'api/v2/ return' , reqObj . to_json
109
+ _ = conn . post 'return' , req_obj . to_json
109
110
response_body [ job_id ] = res_body
110
111
end
111
112
112
- return response_body
113
+ response_body
113
114
end
114
115
115
-
116
116
# List available VMs in ABS
117
117
def self . list ( verbose , url , os_filter = nil )
118
118
conn = Http . get_conn ( verbose , url )
@@ -155,40 +155,39 @@ def self.retrieve(verbose, os_types, token, url, user)
155
155
# "job": {
156
156
# "id": "12345",
157
157
# "tags": {
158
- # "user": "jenkins",
159
- # "jenkins_build_url": "https://jenkins/job/platform_puppet_intn-van-sys_master"
158
+ # "user": "username",
160
159
# }
161
160
# }
162
161
# }
163
162
164
163
conn = Http . get_conn ( verbose , url )
165
164
conn . headers [ 'X-AUTH-TOKEN' ] = token if token
166
165
167
- saved_job_id = Time . now . to_i
166
+ saved_job_id = DateTime . now . strftime ( '%Q' )
168
167
169
- reqObj = {
168
+ req_obj = {
170
169
:resources => os_types ,
171
170
:job => {
172
171
:id => saved_job_id ,
173
172
:tags => {
174
- :user => user ,
175
- :url_string => "floaty://#{ user } /#{ saved_job_id } " ,
173
+ :user => user ,
176
174
} ,
177
175
} ,
178
176
}
179
177
178
+ puts "Posting to ABS #{ req_obj . to_json } " if verbose
179
+
180
180
# os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
181
181
# raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
182
182
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
183
+ res = conn . post 'request' , req_obj . to_json
184
184
185
- i = 0
186
185
retries = 360
187
186
188
187
raise AuthError , "HTTP #{ res . status } : The token provided could not authenticate to the pooler.\n #{ res_body } " if res . status == 401
189
188
190
189
( 1 ..retries ) . each do |i |
191
- queue_place , res_body = check_queue ( conn , saved_job_id , reqObj )
190
+ queue_place , res_body = check_queue ( conn , saved_job_id , req_obj )
192
191
return translated ( res_body ) if res_body
193
192
194
193
puts "Waiting 10 seconds to check if ABS request has been filled. Queue Position: #{ queue_place } ... (x#{ i } )"
@@ -215,11 +214,11 @@ def self.translated(res_body)
215
214
vmpooler_formatted_body
216
215
end
217
216
218
- def self . check_queue ( conn , job_id , reqObj )
219
- queue_info_res = conn . get "/ status/queue/info/#{ job_id } "
217
+ def self . check_queue ( conn , job_id , req_obj )
218
+ queue_info_res = conn . get "status/queue/info/#{ job_id } "
220
219
queue_info = JSON . parse ( queue_info_res . body )
221
220
222
- res = conn . post 'api/v2/ request' , reqObj . to_json
221
+ res = conn . post 'request' , req_obj . to_json
223
222
224
223
unless res . body . empty?
225
224
res_body = JSON . parse ( res . body )
@@ -228,26 +227,27 @@ def self.check_queue(conn, job_id, reqObj)
228
227
[ queue_info [ 'queue_place' ] , nil ]
229
228
end
230
229
231
- def self . snapshot ( verbose , url , hostname , token )
230
+ def self . snapshot ( _verbose , _url , _hostname , _token )
232
231
puts "Can't snapshot with ABS, use '--service vmpooler' (even for vms checked out with ABS)"
233
232
end
234
233
235
234
def self . status ( verbose , url )
236
235
conn = Http . get_conn ( verbose , url )
237
236
238
- res = conn . get '/ status'
237
+ res = conn . get 'status'
239
238
JSON . parse ( res . body )
240
239
end
241
240
242
241
def self . summary ( verbose , url )
243
242
conn = Http . get_conn ( verbose , url )
244
243
245
- res = conn . get '/ summary'
244
+ res = conn . get 'summary'
246
245
JSON . parse ( res . body )
247
246
end
248
247
249
248
def self . query ( verbose , url , hostname )
250
- return @@active_hostnames if @@active_hostnames
249
+ return @active_hostnames if @active_hostnames
250
+
251
251
puts "For vmpooler/snapshot information, use '--service vmpooler' (even for vms checked out with ABS)"
252
252
conn = Http . get_conn ( verbose , url )
253
253
0 commit comments