mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-27 10:28:41 -05:00
(POOLER-107) Add configuration API endpoint
This commit adds a configuration endpoint to the vmpooler API. Pool size, and pool template, can be adjusted for pools that are configured at vmpooler application start time. Pool template changes trigger a pool refresh, and the new template has delta disks created automatically by vmpooler. Additionally, the capability to create template delta disks is added to the vsphere provider, and this is implemented to ensure that templates have delta disks created at application start time. The mechanism used to find template VM objects is simplified to make the flow of logic easier to understand. As an additional benefit, performance of this lookup is improved by using FindByInventoryPath. A table of contents is added to API.md to ease navigation. Without this change API.md has no table of contents and is difficult to navigate. Add mutex object for managing pool configuration updates This commit adds a mutex object for ensuring that pool configuration changes are synchronized across multiple running threads, removing the possibility of two threads attempting to update something at once, without relying on redis data. Without this change this is managed crudely by specifying in redis that a configuration update is taking place. This redis data is left so the REPOPULATE section of _check_pool can still identify when a configuration change is in progress, and prevent a pool from repopulating at that time. Add wake up event for pool template changes This commit adds a wake up event to detect pool template changes. Additionally, GET /config has a template_ready section added to the output for each pool, which makes clear when a pool is ready to populate itself.
This commit is contained in:
parent
e781ed258b
commit
9bb4df7d8e
11 changed files with 1393 additions and 46 deletions
|
|
@ -378,6 +378,29 @@ module Vmpooler
|
|||
result
|
||||
end
|
||||
|
||||
def pool_index(pools)
|
||||
pools_hash = {}
|
||||
index = 0
|
||||
for pool in pools
|
||||
pools_hash[pool['name']] = index
|
||||
index += 1
|
||||
end
|
||||
pools_hash
|
||||
end
|
||||
|
||||
def template_ready?(pool, backend)
|
||||
prepared_template = backend.hget('vmpooler__template__prepared', pool['name'])
|
||||
return false if prepared_template.nil?
|
||||
return true if pool['template'] == prepared_template
|
||||
return false
|
||||
end
|
||||
|
||||
def is_integer?(x)
|
||||
Integer(x)
|
||||
true
|
||||
rescue
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -120,6 +120,74 @@ module Vmpooler
|
|||
result
|
||||
end
|
||||
|
||||
def update_pool_size(payload)
|
||||
result = { 'ok' => false }
|
||||
|
||||
pool_index = pool_index(pools)
|
||||
pools_updated = 0
|
||||
sync_pool_sizes
|
||||
|
||||
payload.each do |poolname, size|
|
||||
unless pools[pool_index[poolname]]['size'] == size.to_i
|
||||
pools[pool_index[poolname]]['size'] = size.to_i
|
||||
backend.hset('vmpooler__config__poolsize', poolname, size)
|
||||
pools_updated += 1
|
||||
status 201
|
||||
end
|
||||
end
|
||||
status 200 unless pools_updated > 0
|
||||
result['ok'] = true
|
||||
result
|
||||
end
|
||||
|
||||
def update_pool_template(payload)
|
||||
result = { 'ok' => false }
|
||||
|
||||
pool_index = pool_index(pools)
|
||||
pools_updated = 0
|
||||
sync_pool_templates
|
||||
|
||||
payload.each do |poolname, template|
|
||||
unless pools[pool_index[poolname]]['template'] == template
|
||||
pools[pool_index[poolname]]['template'] = template
|
||||
backend.hset('vmpooler__config__template', poolname, template)
|
||||
pools_updated += 1
|
||||
status 201
|
||||
end
|
||||
end
|
||||
status 200 unless pools_updated > 0
|
||||
result['ok'] = true
|
||||
result
|
||||
end
|
||||
|
||||
def sync_pool_templates
|
||||
pool_index = pool_index(pools)
|
||||
template_configs = backend.hgetall('vmpooler__config__template')
|
||||
unless template_configs.nil?
|
||||
template_configs.each do |poolname, template|
|
||||
if pool_index.include? poolname
|
||||
unless pools[pool_index[poolname]]['template'] == template
|
||||
pools[pool_index[poolname]]['template'] = template
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def sync_pool_sizes
|
||||
pool_index = pool_index(pools)
|
||||
poolsize_configs = backend.hgetall('vmpooler__config__poolsize')
|
||||
unless poolsize_configs.nil?
|
||||
poolsize_configs.each do |poolname, size|
|
||||
if pool_index.include? poolname
|
||||
unless pools[pool_index[poolname]]['size'] == size.to_i
|
||||
pools[pool_index[poolname]]['size'] == size.to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Provide run-time statistics
|
||||
#
|
||||
# Example:
|
||||
|
|
@ -196,6 +264,8 @@ module Vmpooler
|
|||
}
|
||||
}
|
||||
|
||||
sync_pool_sizes
|
||||
|
||||
result[:capacity] = get_capacity_metrics(pools, backend) unless views and not views.include?("capacity")
|
||||
result[:queue] = get_queue_metrics(pools, backend) unless views and not views.include?("queue")
|
||||
result[:clone] = get_task_metrics(backend, 'clone', Date.today.to_s) unless views and not views.include?("clone")
|
||||
|
|
@ -502,6 +572,30 @@ module Vmpooler
|
|||
invalid
|
||||
end
|
||||
|
||||
def invalid_template_or_size(payload)
|
||||
invalid = []
|
||||
payload.each do |pool, size|
|
||||
invalid << pool unless pool_exists?(pool)
|
||||
unless is_integer?(size)
|
||||
invalid << pool
|
||||
next
|
||||
end
|
||||
invalid << pool unless Integer(size) >= 0
|
||||
end
|
||||
invalid
|
||||
end
|
||||
|
||||
def invalid_template_or_path(payload)
|
||||
invalid = []
|
||||
payload.each do |pool, template|
|
||||
invalid << pool unless pool_exists?(pool)
|
||||
invalid << pool unless template.include? '/'
|
||||
invalid << pool if template[0] == '/'
|
||||
invalid << pool if template[-1] == '/'
|
||||
end
|
||||
invalid
|
||||
end
|
||||
|
||||
post "#{api_prefix}/vm/:template/?" do
|
||||
content_type :json
|
||||
result = { 'ok' => false }
|
||||
|
|
@ -747,6 +841,95 @@ module Vmpooler
|
|||
|
||||
JSON.pretty_generate(result)
|
||||
end
|
||||
|
||||
post "#{api_prefix}/config/poolsize/?" do
|
||||
content_type :json
|
||||
result = { 'ok' => false }
|
||||
|
||||
if config['experimental_features']
|
||||
need_token! if Vmpooler::API.settings.config[:auth]
|
||||
|
||||
payload = JSON.parse(request.body.read)
|
||||
|
||||
if payload
|
||||
invalid = invalid_template_or_size(payload)
|
||||
if invalid.empty?
|
||||
result = update_pool_size(payload)
|
||||
else
|
||||
invalid.each do |bad_template|
|
||||
metrics.increment("config.invalid.#{bad_template}")
|
||||
end
|
||||
result[:bad_templates] = invalid
|
||||
status 400
|
||||
end
|
||||
else
|
||||
metrics.increment('config.invalid.unknown')
|
||||
status 404
|
||||
end
|
||||
else
|
||||
status 405
|
||||
end
|
||||
|
||||
JSON.pretty_generate(result)
|
||||
end
|
||||
|
||||
post "#{api_prefix}/config/pooltemplate/?" do
|
||||
content_type :json
|
||||
result = { 'ok' => false }
|
||||
|
||||
if config['experimental_features']
|
||||
need_token! if Vmpooler::API.settings.config[:auth]
|
||||
|
||||
payload = JSON.parse(request.body.read)
|
||||
|
||||
if payload
|
||||
invalid = invalid_template_or_path(payload)
|
||||
if invalid.empty?
|
||||
result = update_pool_template(payload)
|
||||
else
|
||||
invalid.each do |bad_template|
|
||||
metrics.increment("config.invalid.#{bad_template}")
|
||||
end
|
||||
result[:bad_templates] = invalid
|
||||
status 400
|
||||
end
|
||||
else
|
||||
metrics.increment('config.invalid.unknown')
|
||||
status 404
|
||||
end
|
||||
else
|
||||
status 405
|
||||
end
|
||||
|
||||
JSON.pretty_generate(result)
|
||||
end
|
||||
|
||||
get "#{api_prefix}/config/?" do
|
||||
content_type :json
|
||||
result = { 'ok' => false }
|
||||
status 404
|
||||
|
||||
if pools
|
||||
sync_pool_sizes
|
||||
sync_pool_templates
|
||||
|
||||
pool_configuration = []
|
||||
pools.each do |pool|
|
||||
pool['template_ready'] = template_ready?(pool, backend)
|
||||
pool_configuration << pool
|
||||
end
|
||||
|
||||
result = {
|
||||
pool_configuration: pool_configuration,
|
||||
status: {
|
||||
ok: true
|
||||
}
|
||||
}
|
||||
|
||||
status 200
|
||||
end
|
||||
JSON.pretty_generate(result)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue