Skip to content

Commit d963e35

Browse files
committed
Adding delete and get active requests
1 parent 7e27542 commit d963e35

File tree

9 files changed

+109
-75
lines changed

9 files changed

+109
-75
lines changed

lib/vmfloaty.rb

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def run
3333
c.option '--user STRING', String, 'User to authenticate with'
3434
c.option '--url STRING', String, 'URL of pooler service'
3535
c.option '--token STRING', String, 'Token for pooler service'
36+
c.option '--priority STRING', 'Priority for supported backends(ABS) (High(1), Medium(2), Low(3))'
3637
c.option '--notoken', 'Makes a request without a token'
3738
c.option '--force', 'Forces vmfloaty to get requested vms'
3839
c.option '--json', 'Prints retrieved vms in JSON format'

lib/vmfloaty/abs.rb

+81-52
Original file line numberDiff line numberDiff line change
@@ -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,29 @@ 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+
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']}"
93104
end
94105
end
95106
end
@@ -98,21 +109,22 @@ def self.delete(verbose, url, hosts, token, user)
98109
response_body = {}
99110

100111
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'],
104115
}
105116

106-
puts "Deleting #{reqObj}" if verbose
117+
puts "Deleting #{req_obj}" if verbose
107118

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
110123
end
111124

112-
return response_body
125+
response_body
113126
end
114127

115-
116128
# List available VMs in ABS
117129
def self.list(verbose, url, os_filter = nil)
118130
conn = Http.get_conn(verbose, url)
@@ -143,7 +155,7 @@ def self.list(verbose, url, os_filter = nil)
143155
end
144156

145157
# 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)
147159
#
148160
# Contents of post must be like:
149161
#
@@ -155,44 +167,59 @@ def self.retrieve(verbose, os_types, token, url, user)
155167
# "job": {
156168
# "id": "12345",
157169
# "tags": {
158-
# "user": "jenkins",
159-
# "jenkins_build_url": "https://jenkins/job/platform_puppet_intn-van-sys_master"
170+
# "user": "username",
160171
# }
161172
# }
162173
# }
163174

164175
conn = Http.get_conn(verbose, url)
165176
conn.headers['X-AUTH-TOKEN'] = token if token
166177

167-
saved_job_id = Time.now.to_i
178+
saved_job_id = DateTime.now.strftime('%Q')
168179

169-
reqObj = {
180+
req_obj = {
170181
:resources => os_types,
171182
:job => {
172183
:id => saved_job_id,
173184
:tags => {
174-
:user => user,
175-
:url_string => "floaty://#{user}/#{saved_job_id}",
185+
:user => user,
176186
},
177187
},
178188
}
179189

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+
180204
# os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
181205
# raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
182206
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
184208

185-
i = 0
186209
retries = 360
187210

188211
raise AuthError, "HTTP #{res.status}: The token provided could not authenticate to the pooler.\n#{res_body}" if res.status == 401
189212

190213
(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)
192215
return translated(res_body) if res_body
193216

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)
196223
end
197224
nil
198225
end
@@ -215,11 +242,11 @@ def self.translated(res_body)
215242
vmpooler_formatted_body
216243
end
217244

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}"
220247
queue_info = JSON.parse(queue_info_res.body)
221248

222-
res = conn.post 'api/v2/request', reqObj.to_json
249+
res = conn.post 'request', req_obj.to_json
223250

224251
unless res.body.empty?
225252
res_body = JSON.parse(res.body)
@@ -228,26 +255,28 @@ def self.check_queue(conn, job_id, reqObj)
228255
[queue_info['queue_place'], nil]
229256
end
230257

231-
def self.snapshot(verbose, url, hostname, token)
258+
def self.snapshot(_verbose, _url, _hostname, _token)
232259
puts "Can't snapshot with ABS, use '--service vmpooler' (even for vms checked out with ABS)"
233260
end
234261

235262
def self.status(verbose, url)
236263
conn = Http.get_conn(verbose, url)
237264

238-
res = conn.get '/status'
239-
JSON.parse(res.body)
265+
res = conn.get 'status'
266+
267+
return res.body == "OK"
240268
end
241269

242270
def self.summary(verbose, url)
243271
conn = Http.get_conn(verbose, url)
244272

245-
res = conn.get '/summary'
273+
res = conn.get 'summary'
246274
JSON.parse(res.body)
247275
end
248276

249277
def self.query(verbose, url, hostname)
250-
return @@active_hostnames if @@active_hostnames
278+
return @active_hostnames if @active_hostnames
279+
251280
puts "For vmpooler/snapshot information, use '--service vmpooler' (even for vms checked out with ABS)"
252281
conn = Http.get_conn(verbose, url)
253282

lib/vmfloaty/nonstandard_pooler.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def self.list_active(verbose, url, token, _user)
2222
status['reserved_hosts'] || []
2323
end
2424

25-
def self.retrieve(verbose, os_type, token, url, _user)
25+
def self.retrieve(verbose, os_type, token, url, _user, _options)
2626
conn = Http.get_conn(verbose, url)
2727
conn.headers['X-AUTH-TOKEN'] = token if token
2828

lib/vmfloaty/pooler.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def self.list_active(verbose, url, token, _user)
2828
vms
2929
end
3030

31-
def self.retrieve(verbose, os_type, token, url, user)
31+
def self.retrieve(verbose, os_type, token, url, _user, _options)
3232
# NOTE:
3333
# Developers can use `Utils.generate_os_hash` to
3434
# generate the os_type param.

lib/vmfloaty/service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def list_active(verbose)
7878
def retrieve(verbose, os_types, use_token = true)
7979
puts 'Requesting a vm without a token...' unless use_token
8080
token_value = use_token ? token : nil
81-
@service_object.retrieve verbose, os_types, token_value, url, user
81+
@service_object.retrieve verbose, os_types, token_value, url, user, @config
8282
end
8383

8484
def ssh(verbose, host_os, use_token = true)

lib/vmfloaty/utils.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def self.pretty_print_hosts(verbose, service, hostnames = [])
8888
when 'ABS'
8989
# For ABS, 'hostname' variable is the jobID
9090
if host_data['state'] == 'allocated' || host_data['state'] == 'filled'
91-
host_data['allocated_resources'].each do |vm_name, i|
92-
puts "- [JobID:#{host_data['request']['job']['id']}] #{vm_name["hostname"]} (#{vm_name["type"]}) <#{host_data['state']}>"
91+
host_data['allocated_resources'].each do |vm_name, _i|
92+
puts "- [JobID:#{host_data['request']['job']['id']}] #{vm_name['hostname']} (#{vm_name['type']}) <#{host_data['state']}>"
9393
end
9494
end
9595
when 'Pooler'
@@ -155,6 +155,9 @@ def self.pretty_print_status(verbose, service)
155155
puts "#{name.ljust(width)} #{e.red}"
156156
end
157157
end
158+
when 'ABS'
159+
puts "ABS Not OK".red unless status_response
160+
puts "ABS is OK".green if status_response
158161
else
159162
raise "Invalid service type #{service.type}"
160163
end
@@ -205,6 +208,7 @@ def self.get_service_config(config, options)
205208
end
206209

207210
# Prioritize an explicitly specified url, user, or token if the user provided one
211+
service_config['priority'] = options.priority unless options.priority.nil?
208212
service_config['url'] = options.url unless options.url.nil?
209213
service_config['token'] = options.token unless options.token.nil?
210214
service_config['user'] = options.user unless options.user.nil?

spec/vmfloaty/abs_spec.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@
3636
end
3737

3838
it 'won\'t delete a job if not all vms are listed' do
39-
hosts = ["host1"]
39+
hosts = ['host1']
4040
allocated_resources = [
4141
{
42-
"hostname" => "host1"
42+
'hostname' => 'host1',
4343
},
4444
{
45-
"hostname" => "host2"
46-
}
45+
'hostname' => 'host2',
46+
},
4747
]
4848
expect(ABS.all_job_resources_accounted_for(allocated_resources, hosts)).to eq(false)
4949

50-
hosts = ["host1", "host2"]
50+
hosts = ['host1', 'host2']
5151
allocated_resources = [
5252
{
53-
"hostname" => "host1"
53+
'hostname' => 'host1',
5454
},
5555
{
56-
"hostname" => "host2"
57-
}
56+
'hostname' => 'host2',
57+
},
5858
]
5959
expect(ABS.all_job_resources_accounted_for(allocated_resources, hosts)).to eq(true)
6060
end

0 commit comments

Comments
 (0)