@@ -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,29 @@ 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
+ retStatus = { }
86
+ hosts . each do |host |
87
+ retStatus [ host ] = {
88
+ 'ok' => false
89
+ }
90
+ end
91
+
92
+ requests . each do |req_hash |
93
+ next unless req_hash [ 'state' ] == 'allocated' || req_hash [ 'state' ] == 'filled'
94
+
95
+ req_hash [ 'allocated_resources' ] . each do |vm_name , _i |
96
+ if hosts . include? vm_name [ 'hostname' ]
97
+ if all_job_resources_accounted_for ( req_hash [ 'allocated_resources' ] , hosts )
98
+ retStatus [ vm_name [ 'hostname' ] ] = {
99
+ 'ok' => true
100
+ }
101
+ jobs_to_delete . push ( req_hash )
102
+ else
103
+ 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' ] } "
93
104
end
94
105
end
95
106
end
@@ -98,21 +109,22 @@ def self.delete(verbose, url, hosts, token, user)
98
109
response_body = { }
99
110
100
111
jobs_to_delete . each do |job |
101
- reqObj = {
102
- 'job_id' : job [ 'request' ] [ 'job' ] [ 'id' ] ,
103
- 'hosts' : job [ 'allocated_resources' ] ,
112
+ req_obj = {
113
+ 'job_id' => job [ 'request' ] [ 'job' ] [ 'id' ] ,
114
+ 'hosts' => job [ 'allocated_resources' ] ,
104
115
}
105
116
106
- puts "Deleting #{ reqObj } " if verbose
117
+ puts "Deleting #{ req_obj } " if verbose
107
118
108
- res = conn . post 'api/v2/return' , reqObj . to_json
109
- response_body [ job_id ] = res_body
119
+ return_result = conn . post 'return' , req_obj . to_json
120
+ req_obj [ 'hosts' ] . each do |host |
121
+ response_body [ host [ "hostname" ] ] = { 'ok' => true } if return_result . body == "OK"
122
+ end
110
123
end
111
124
112
- return response_body
125
+ response_body
113
126
end
114
127
115
-
116
128
# List available VMs in ABS
117
129
def self . list ( verbose , url , os_filter = nil )
118
130
conn = Http . get_conn ( verbose , url )
@@ -143,7 +155,7 @@ def self.list(verbose, url, os_filter = nil)
143
155
end
144
156
145
157
# Retrieve an OS from ABS.
146
- def self . retrieve ( verbose , os_types , token , url , user )
158
+ def self . retrieve ( verbose , os_types , token , url , user , options )
147
159
#
148
160
# Contents of post must be like:
149
161
#
@@ -155,44 +167,59 @@ def self.retrieve(verbose, os_types, token, url, user)
155
167
# "job": {
156
168
# "id": "12345",
157
169
# "tags": {
158
- # "user": "jenkins",
159
- # "jenkins_build_url": "https://jenkins/job/platform_puppet_intn-van-sys_master"
170
+ # "user": "username",
160
171
# }
161
172
# }
162
173
# }
163
174
164
175
conn = Http . get_conn ( verbose , url )
165
176
conn . headers [ 'X-AUTH-TOKEN' ] = token if token
166
177
167
- saved_job_id = Time . now . to_i
178
+ saved_job_id = DateTime . now . strftime ( '%Q' )
168
179
169
- reqObj = {
180
+ req_obj = {
170
181
:resources => os_types ,
171
182
:job => {
172
183
:id => saved_job_id ,
173
184
:tags => {
174
- :user => user ,
175
- :url_string => "floaty://#{ user } /#{ saved_job_id } " ,
185
+ :user => user ,
176
186
} ,
177
187
} ,
178
188
}
179
189
190
+ if options [ "priority" ]
191
+ if options [ "priority" ] == "high"
192
+ req_obj [ :priority ] = 1
193
+ elsif options [ "priority" ] == "medium"
194
+ req_obj [ :priority ] = 2
195
+ elsif options [ "priority" ] == "low"
196
+ req_obj [ :priority ] = 3
197
+ else
198
+ req_obj [ :priority ] = options [ "priority" ] . to_i
199
+ end
200
+ end
201
+
202
+ puts "Posting to ABS #{ req_obj . to_json } " if verbose
203
+
180
204
# os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
181
205
# raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
182
206
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
207
+ res = conn . post 'request' , req_obj . to_json
184
208
185
- i = 0
186
209
retries = 360
187
210
188
211
raise AuthError , "HTTP #{ res . status } : The token provided could not authenticate to the pooler.\n #{ res_body } " if res . status == 401
189
212
190
213
( 1 ..retries ) . each do |i |
191
- queue_place , res_body = check_queue ( conn , saved_job_id , reqObj )
214
+ queue_place , res_body = check_queue ( conn , saved_job_id , req_obj )
192
215
return translated ( res_body ) if res_body
193
216
194
- puts "Waiting 10 seconds to check if ABS request has been filled. Queue Position: #{ queue_place } ... (x#{ i } )"
195
- sleep ( 10 )
217
+
218
+ sleepSeconds = 10 if i >= 10
219
+ sleepSeconds = i if i < 10
220
+ puts "Waiting #{ sleepSeconds } seconds to check if ABS request has been filled. Queue Position: #{ queue_place } ... (x#{ i } )"
221
+
222
+ sleep ( sleepSeconds )
196
223
end
197
224
nil
198
225
end
@@ -215,11 +242,11 @@ def self.translated(res_body)
215
242
vmpooler_formatted_body
216
243
end
217
244
218
- def self . check_queue ( conn , job_id , reqObj )
219
- queue_info_res = conn . get "/ status/queue/info/#{ job_id } "
245
+ def self . check_queue ( conn , job_id , req_obj )
246
+ queue_info_res = conn . get "status/queue/info/#{ job_id } "
220
247
queue_info = JSON . parse ( queue_info_res . body )
221
248
222
- res = conn . post 'api/v2/ request' , reqObj . to_json
249
+ res = conn . post 'request' , req_obj . to_json
223
250
224
251
unless res . body . empty?
225
252
res_body = JSON . parse ( res . body )
@@ -228,26 +255,28 @@ def self.check_queue(conn, job_id, reqObj)
228
255
[ queue_info [ 'queue_place' ] , nil ]
229
256
end
230
257
231
- def self . snapshot ( verbose , url , hostname , token )
258
+ def self . snapshot ( _verbose , _url , _hostname , _token )
232
259
puts "Can't snapshot with ABS, use '--service vmpooler' (even for vms checked out with ABS)"
233
260
end
234
261
235
262
def self . status ( verbose , url )
236
263
conn = Http . get_conn ( verbose , url )
237
264
238
- res = conn . get '/status'
239
- JSON . parse ( res . body )
265
+ res = conn . get 'status'
266
+
267
+ return res . body == "OK"
240
268
end
241
269
242
270
def self . summary ( verbose , url )
243
271
conn = Http . get_conn ( verbose , url )
244
272
245
- res = conn . get '/ summary'
273
+ res = conn . get 'summary'
246
274
JSON . parse ( res . body )
247
275
end
248
276
249
277
def self . query ( verbose , url , hostname )
250
- return @@active_hostnames if @@active_hostnames
278
+ return @active_hostnames if @active_hostnames
279
+
251
280
puts "For vmpooler/snapshot information, use '--service vmpooler' (even for vms checked out with ABS)"
252
281
conn = Http . get_conn ( verbose , url )
253
282
0 commit comments