Class: Vmpooler::API::V2
- Inherits:
-
V1
- Object
- Sinatra::Base
- V1
- Vmpooler::API::V2
show all
- Defined in:
- lib/vmpooler/api/v2.rb
Instance Method Summary
collapse
Methods inherited from V1
#account_for_starting_vm, #backend, #checkoutlock, #component_to_test, #config, #count_selection, #delete_ondemand_request, #evaluate_template_aliases, #extract_templates_from_query_params, #generate_request_id, #get_pool_weights, #invalid_pool, #invalid_template_or_path, #invalid_template_or_size, #invalid_templates, #metrics, #need_auth!, #need_token!, #pool_exists?, #pools, #pools_at_startup, #reset_pool, #reset_pool_size, #reset_pool_template, #return_vm_to_ready_state, #sync_clone_targets, #sync_pool_sizes, #sync_pool_templates, #too_many_requested?, #update_clone_target, #update_pool_size, #update_pool_template, #update_result_hosts, #update_user_metrics
Instance Method Details
#atomically_allocate_vms(payload) ⇒ Object
The domain in the result body will be set to the one associated with the last vm added. The part of the response is only being retained for backwards compatibility as the hostnames are now fqdn’s instead of bare hostnames. This change is a result of now being able to specify a domain per pool. If no vm’s in the result had a domain sepcified then the domain key will be omitted similar to how it was previously omitted if the global option domain wasn’t specified.
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/vmpooler/api/v2.rb', line 93
def atomically_allocate_vms(payload)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do |span|
result = { 'ok' => false }
failed = false
vms = []
validate_token(backend) if Vmpooler::API.settings.config[:auth] and has_token?
payload.each do |requested, count|
count.to_i.times do |_i|
vmname, vmpool, vmtemplate = fetch_single_vm(requested)
if vmname
account_for_starting_vm(vmpool, vmname)
vms << [vmpool, vmname, vmtemplate]
metrics.increment("checkout.success.#{vmpool}")
update_user_metrics('allocate', vmname) if Vmpooler::API.settings.config[:config]['usage_stats']
else
failed = true
metrics.increment("checkout.empty.#{requested}")
break
end
end
end
if failed
vms.each do |(vmpool, vmname, _vmtemplate)|
return_vm_to_ready_state(vmpool, vmname)
end
span.add_event('error', attributes: {
'error.type' => 'Vmpooler::API::V2.atomically_allocate_vms',
'error.message' => '503 due to failing to allocate one or more vms'
})
status 503
else
vm_names = []
vms.each do |(vmpool, vmname, vmtemplate)|
vmdomain = Parsing.get_domain_for_pool(full_config, vmpool)
if vmdomain
vmfqdn = "#{vmname}.#{vmdomain}"
update_result_hosts(result, vmtemplate, vmfqdn)
vm_names.append(vmfqdn)
else
update_result_hosts(result, vmtemplate, vmname)
vm_names.append(vmname)
end
end
span.set_attribute('vmpooler.vm_names', vm_names.join(',')) unless vm_names.empty?
result['ok'] = true
end
result
end
end
|
#check_ondemand_request(request_id) ⇒ Object
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
# File 'lib/vmpooler/api/v2.rb', line 338
def check_ondemand_request(request_id)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do |span|
span.set_attribute('vmpooler.request_id', request_id)
result = { 'ok' => false }
request_hash = backend.hgetall("vmpooler__odrequest__#{request_id}")
if request_hash.empty?
e_message = "no request found for request_id '#{request_id}'"
result['message'] = e_message
span.add_event('error', attributes: {
'error.type' => 'Vmpooler::API::V2.check_ondemand_request',
'error.message' => e_message
})
return result
end
result['request_id'] = request_id
result['ready'] = false
result['ok'] = true
status 202
case request_hash['status']
when 'ready'
result['ready'] = true
Parsing.get_platform_pool_count(request_hash['requested']) do |platform_alias, pool, _count|
instances = backend.smembers("vmpooler__#{request_id}__#{platform_alias}__#{pool}")
domain = Parsing.get_domain_for_pool(full_config, pool)
instances.map! { |instance| instance.concat(".#{domain}") } if domain
if result.key?(platform_alias)
result[platform_alias][:hostname] = result[platform_alias][:hostname] + instances
else
result[platform_alias] = { 'hostname': instances }
end
end
status 200
when 'failed'
result['message'] = "The request failed to provision instances within the configured ondemand_request_ttl '#{config['ondemand_request_ttl']}'"
status 200
when 'deleted'
result['message'] = 'The request has been deleted'
status 200
else
Parsing.get_platform_pool_count(request_hash['requested']) do |platform_alias, pool, count|
instance_count = backend.scard("vmpooler__#{request_id}__#{platform_alias}__#{pool}")
instances_pending = count.to_i - instance_count.to_i
if result.key?(platform_alias) && result[platform_alias].key?(:ready)
result[platform_alias][:ready] = (result[platform_alias][:ready].to_i + instance_count).to_s
result[platform_alias][:pending] = (result[platform_alias][:pending].to_i + instances_pending).to_s
else
result[platform_alias] = {
'ready': instance_count.to_s,
'pending': instances_pending.to_s
}
end
end
end
result
end
end
|
#fetch_single_vm(template) ⇒ Tuple
Fetch a single vm from a pool
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/vmpooler/api/v2.rb', line 36
def fetch_single_vm(template)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do
template_backends = [template]
aliases = Vmpooler::API.settings.config[:alias]
if aliases
template_backends += aliases[template] if aliases[template].is_a?(Array)
template_backends << aliases[template] if aliases[template].is_a?(String)
pool_index = pool_index(pools)
weighted_pools = {}
template_backends.each do |t|
next unless pool_index.key? t
index = pool_index[t]
clone_target = pools[index]['clone_target'] || config['clone_target']
next unless config.key?('backend_weight')
weight = config['backend_weight'][clone_target]
if weight
weighted_pools[t] = weight
end
end
if weighted_pools.count == template_backends.count
pickup = Pickup.new(weighted_pools)
selection = pickup.pick
template_backends.delete(selection)
template_backends.unshift(selection)
else
first = template_backends.sample
template_backends.delete(first)
template_backends.unshift(first)
end
end
checkoutlock.synchronize do
template_backends.each do |template_backend|
vms = backend.smembers("vmpooler__ready__#{template_backend}")
next if vms.empty?
vm = vms.pop
smoved = backend.smove("vmpooler__ready__#{template_backend}", "vmpooler__running__#{template_backend}", vm)
if smoved
return [vm, template_backend, template]
end
end
[nil, nil, nil]
end
end
end
|
#full_config ⇒ Object
11
12
13
|
# File 'lib/vmpooler/api/v2.rb', line 11
def full_config
Vmpooler::API.settings.config
end
|
#generate_ondemand_request(payload) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/vmpooler/api/v2.rb', line 149
def generate_ondemand_request(payload)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do |span|
result = { 'ok': false }
requested_instances = payload.reject { |k, _v| k == 'request_id' }
if too_many_requested?(requested_instances)
e_message = "requested amount of instances exceeds the maximum #{config['max_ondemand_instances_per_request']}"
result['message'] = e_message
status 403
span.add_event('error', attributes: {
'error.type' => 'Vmpooler::API::V2.generate_ondemand_request',
'error.message' => "403 due to #{e_message}"
})
return result
end
score = Time.now.to_i
request_id = payload['request_id']
request_id ||= generate_request_id
result['request_id'] = request_id
span.set_attribute('vmpooler.request_id', request_id)
if backend.exists?("vmpooler__odrequest__#{request_id}")
e_message = "request_id '#{request_id}' has already been created"
result['message'] = e_message
status 409
span.add_event('error', attributes: {
'error.type' => 'Vmpooler::API::V2.generate_ondemand_request',
'error.message' => "409 due to #{e_message}"
})
metrics.increment('ondemandrequest_generate.duplicaterequests')
return result
end
status 201
platforms_with_aliases = []
requested_instances.each do |poolname, count|
selection = evaluate_template_aliases(poolname, count)
selection.map { |selected_pool, selected_pool_count| platforms_with_aliases << "#{poolname}:#{selected_pool}:#{selected_pool_count}" }
end
platforms_string = platforms_with_aliases.join(',')
return result unless backend.zadd('vmpooler__provisioning__request', score, request_id)
backend.hset("vmpooler__odrequest__#{request_id}", 'requested', platforms_string)
if Vmpooler::API.settings.config[:auth] and has_token?
token_token = request.env['HTTP_X_AUTH_TOKEN']
token_user = backend.hget("vmpooler__token__#{token_token}", 'user')
backend.hset("vmpooler__odrequest__#{request_id}", 'token:token', token_token)
backend.hset("vmpooler__odrequest__#{request_id}", 'token:user', token_user)
span.set_attribute('enduser.id', token_user)
end
result[:ok] = true
metrics.increment('ondemandrequest_generate.success')
result
end
end
|
#get_template_aliases(template) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/vmpooler/api/v2.rb', line 15
def get_template_aliases(template)
tracer.in_span("Vmpooler::API::V2.#{__method__}") do
result = []
aliases = Vmpooler::API.settings.config[:alias]
if aliases
result += aliases[template] if aliases[template].is_a?(Array)
template_backends << aliases[template] if aliases[template].is_a?(String)
end
result
end
end
|