mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-27 02:18:41 -05:00
(QENG-3919) spike for implementation of all-or-nothing checkout
This commit is contained in:
parent
b59a1f8886
commit
e323a7fb58
1 changed files with 68 additions and 48 deletions
|
|
@ -45,10 +45,15 @@ module Vmpooler
|
||||||
newhash
|
newhash
|
||||||
end
|
end
|
||||||
|
|
||||||
def checkout_vm(template, result)
|
def fetch_single_vm(template)
|
||||||
vm = backend.spop('vmpooler__ready__' + template)
|
backend.spop('vmpooler__ready__' + template)
|
||||||
|
end
|
||||||
|
|
||||||
unless vm.nil?
|
def return_single_vm(template, vm)
|
||||||
|
backend.spush('vmpooler__ready__' + template, vm)
|
||||||
|
end
|
||||||
|
|
||||||
|
def account_for_starting_vm(template, vm)
|
||||||
backend.sadd('vmpooler__running__' + template, vm)
|
backend.sadd('vmpooler__running__' + template, vm)
|
||||||
backend.hset('vmpooler__active__' + template, vm, Time.now)
|
backend.hset('vmpooler__active__' + template, vm, Time.now)
|
||||||
backend.hset('vmpooler__vm__' + vm, 'checkout', Time.now)
|
backend.hset('vmpooler__vm__' + vm, 'checkout', Time.now)
|
||||||
|
|
@ -65,15 +70,14 @@ module Vmpooler
|
||||||
backend.hset('vmpooler__vm__' + vm, 'lifetime', config['vm_lifetime_auth'].to_i)
|
backend.hset('vmpooler__vm__' + vm, 'lifetime', config['vm_lifetime_auth'].to_i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
result[template] ||= {}
|
|
||||||
|
|
||||||
if result[template]['hostname']
|
|
||||||
result[template]['hostname'] = [result[template]['hostname']] unless result[template]['hostname'].is_a?(Array)
|
|
||||||
result[template]['hostname'].push(vm)
|
|
||||||
else
|
|
||||||
result[template]['hostname'] = vm
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def checkout_vm(template, result)
|
||||||
|
vm = fetch_single_vm(template)
|
||||||
|
|
||||||
|
unless vm.nil?
|
||||||
|
account_for_starting_vm(template, vm)
|
||||||
|
update_result_hosts(result, template, vm)
|
||||||
else
|
else
|
||||||
status 503
|
status 503
|
||||||
result['ok'] = false
|
result['ok'] = false
|
||||||
|
|
@ -335,40 +339,56 @@ module Vmpooler
|
||||||
|
|
||||||
post "#{api_prefix}/vm/?" do
|
post "#{api_prefix}/vm/?" do
|
||||||
content_type :json
|
content_type :json
|
||||||
|
|
||||||
result = { 'ok' => false }
|
result = { 'ok' => false }
|
||||||
|
|
||||||
jdata = alias_deref(JSON.parse(request.body.read))
|
jdata = alias_deref(JSON.parse(request.body.read))
|
||||||
|
|
||||||
if not jdata.nil? and not jdata.empty?
|
if not jdata.nil? and not jdata.empty?
|
||||||
available = 1
|
failed = false
|
||||||
|
vms = []
|
||||||
|
|
||||||
|
jdata.each do |template, count|
|
||||||
|
val.to_i.times do |_i|
|
||||||
|
vm = fetch_single_vm(template)
|
||||||
|
if !vm
|
||||||
|
failed = true
|
||||||
|
break
|
||||||
|
else
|
||||||
|
vms << [ template, vm ]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if failed
|
||||||
|
vms.each do |(template, vm)|
|
||||||
|
return_single_vm(template, vm)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
vms.each do |(template, vm)|
|
||||||
|
account_for_starting_vm(template, vm)
|
||||||
|
update_result_hosts(results, template, vm)
|
||||||
|
end
|
||||||
|
|
||||||
|
result['ok'] = true
|
||||||
|
result['domain'] = config['domain'] if config['domain']
|
||||||
|
end
|
||||||
else
|
else
|
||||||
status 404
|
status 404
|
||||||
end
|
end
|
||||||
|
|
||||||
jdata.each do |key, val|
|
|
||||||
if backend.scard('vmpooler__ready__' + key).to_i < val.to_i
|
|
||||||
available = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if (available == 1)
|
|
||||||
result['ok'] = true
|
|
||||||
|
|
||||||
jdata.each do |key, val|
|
|
||||||
val.to_i.times do |_i|
|
|
||||||
result = checkout_vm(key, result)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if result['ok'] && config['domain']
|
|
||||||
result['domain'] = config['domain']
|
|
||||||
end
|
|
||||||
|
|
||||||
JSON.pretty_generate(result)
|
JSON.pretty_generate(result)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_result_hosts(result, template, vm)
|
||||||
|
result[template] ||= {}
|
||||||
|
if result[template]['hostname']
|
||||||
|
result[template]['hostname'] = Array(result[template]['hostname'])
|
||||||
|
result[template]['hostname'].push(vm)
|
||||||
|
else
|
||||||
|
result[template]['hostname'] = vm
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
post "#{api_prefix}/vm/:template/?" do
|
post "#{api_prefix}/vm/:template/?" do
|
||||||
content_type :json
|
content_type :json
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue