Skip to content

Commit 01b5457

Browse files
committed
Echo to user the service being used
1 parent 7e27542 commit 01b5457

File tree

6 files changed

+65
-65
lines changed

6 files changed

+65
-65
lines changed

lib/vmfloaty/abs.rb

+47-47
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,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 '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,39 @@ 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

178+
puts "Posting to ABS #{req_obj.to_json}" if verbose
179+
180180
# os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
181181
# raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
182182
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
184184

185-
i = 0
186185
retries = 360
187186

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

190189
(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)
192191
return translated(res_body) if res_body
193192

194193
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)
215214
vmpooler_formatted_body
216215
end
217216

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

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

224223
unless res.body.empty?
225224
res_body = JSON.parse(res.body)
@@ -228,26 +227,27 @@ def self.check_queue(conn, job_id, reqObj)
228227
[queue_info['queue_place'], nil]
229228
end
230229

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

235234
def self.status(verbose, url)
236235
conn = Http.get_conn(verbose, url)
237236

238-
res = conn.get '/status'
237+
res = conn.get 'status'
239238
JSON.parse(res.body)
240239
end
241240

242241
def self.summary(verbose, url)
243242
conn = Http.get_conn(verbose, url)
244243

245-
res = conn.get '/summary'
244+
res = conn.get 'summary'
246245
JSON.parse(res.body)
247246
end
248247

249248
def self.query(verbose, url, hostname)
250-
return @@active_hostnames if @@active_hostnames
249+
return @active_hostnames if @active_hostnames
250+
251251
puts "For vmpooler/snapshot information, use '--service vmpooler' (even for vms checked out with ABS)"
252252
conn = Http.get_conn(verbose, url)
253253

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)
3232
# NOTE:
3333
# Developers can use `Utils.generate_os_hash` to
3434
# generate the os_type param.

lib/vmfloaty/utils.rb

+2-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'

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

spec/vmfloaty/nonstandard_pooler_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
@token_status_body_active = <<~BODY
6767
{
6868
"ok": true,
69-
"user": 'first.last',
69+
"user": "first.last",
7070
"created": "2017-09-18 01:25:41 +0000",
7171
"last_accessed": "2017-09-21 19:46:25 +0000",
7272
"reserved_hosts": ["sol10-9", "sol10-11"]
@@ -75,7 +75,7 @@
7575
@token_status_body_empty = <<~BODY
7676
{
7777
"ok": true,
78-
"user": 'first.last',
78+
"user": "first.last",
7979
"created": "2017-09-18 01:25:41 +0000",
8080
"last_accessed": "2017-09-21 19:46:25 +0000",
8181
"reserved_hosts": []
@@ -88,7 +88,7 @@
8888
.with(false, @nspooler_url, 'token-value')
8989
.and_return(JSON.parse(@token_status_body_active))
9090

91-
list = NonstandardPooler.list_active(false, @nspooler_url, 'token-value')
91+
list = NonstandardPooler.list_active(false, @nspooler_url, 'token-value', 'user')
9292
expect(list).to eql ['sol10-9', 'sol10-11']
9393
end
9494
end
@@ -246,7 +246,7 @@
246246
"sol10-11": {
247247
"fqdn": "sol10-11.delivery.puppetlabs.net",
248248
"os_triple": "solaris-10-sparc",
249-
"reserved_by_user": 'first.last',
249+
"reserved_by_user": "first.last",
250250
"reserved_for_reason": "testing",
251251
"hours_left_on_reservation": 29.12
252252
}

spec/vmfloaty/pooler_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
vm_hash = {}
5555
vm_hash['debian-7-i386'] = 1
56-
expect { Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url) }.to raise_error(AuthError)
56+
expect { Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url, 'user') }.to raise_error(AuthError)
5757
end
5858

5959
it 'retrieves a single vm with a token' do
@@ -63,7 +63,7 @@
6363

6464
vm_hash = {}
6565
vm_hash['debian-7-i386'] = 1
66-
vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url)
66+
vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url, 'user')
6767
expect(vm_req).to be_an_instance_of Hash
6868
expect(vm_req['ok']).to equal true
6969
expect(vm_req['debian-7-i386']['hostname']).to eq 'fq6qlpjlsskycq6'
@@ -77,7 +77,7 @@
7777
vm_hash = {}
7878
vm_hash['debian-7-i386'] = 2
7979
vm_hash['centos-7-x86_64'] = 1
80-
vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url)
80+
vm_req = Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url, 'user')
8181
expect(vm_req).to be_an_instance_of Hash
8282
expect(vm_req['ok']).to equal true
8383
expect(vm_req['debian-7-i386']['hostname']).to be_an_instance_of Array

0 commit comments

Comments
 (0)