Ensure pool sizes and templates are synced

This commit adds a step to sync pool sizes and templates when interacting with the configuration API. Without this change a pool size change is not reflected by the API loaded configuration until the change is made again via the API, and this registers as a new change.
This commit is contained in:
kirby@puppetlabs.com 2018-05-24 14:12:44 -07:00
parent 3b97afe076
commit 17956294ca
2 changed files with 51 additions and 24 deletions

View file

@ -125,6 +125,7 @@ module Vmpooler
pool_index = pool_index(pools) pool_index = pool_index(pools)
pools_updated = 0 pools_updated = 0
sync_pool_sizes
payload.each do |poolname, size| payload.each do |poolname, size|
unless pools[pool_index[poolname]]['size'] == size.to_i unless pools[pool_index[poolname]]['size'] == size.to_i
@ -144,6 +145,7 @@ module Vmpooler
pool_index = pool_index(pools) pool_index = pool_index(pools)
pools_updated = 0 pools_updated = 0
sync_pool_templates
payload.each do |poolname, template| payload.each do |poolname, template|
unless pools[pool_index[poolname]]['template'] == template unless pools[pool_index[poolname]]['template'] == template
@ -158,6 +160,34 @@ module Vmpooler
result result
end 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 # Provide run-time statistics
# #
# Example: # Example:
@ -234,6 +264,8 @@ module Vmpooler
} }
} }
sync_pool_sizes
result[:capacity] = get_capacity_metrics(pools, backend) unless views and not views.include?("capacity") 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[: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") result[:clone] = get_task_metrics(backend, 'clone', Date.today.to_s) unless views and not views.include?("clone")
@ -864,6 +896,9 @@ module Vmpooler
status 404 status 404
if pools if pools
sync_pool_sizes
sync_pool_templates
result = { result = {
pool_configuration: pools, pool_configuration: pools,
status: { status: {

View file

@ -18,24 +18,25 @@ describe Vmpooler::API::V1 do
Vmpooler::API Vmpooler::API
end end
let(:config) {
{
config: {
'site_name' => 'test pooler',
'vm_lifetime_auth' => 2,
},
pools: [
{'name' => 'pool1', 'size' => 5, 'template' => 'templates/pool1'},
{'name' => 'pool2', 'size' => 10}
],
statsd: { 'prefix' => 'stats_prefix'},
alias: { 'poolone' => 'pool1' },
pool_names: [ 'pool1', 'pool2', 'poolone' ]
}
}
describe '/config/pooltemplate' do describe '/config/pooltemplate' do
let(:prefix) { '/api/v1' } let(:prefix) { '/api/v1' }
let(:metrics) { Vmpooler::DummyStatsd.new } let(:metrics) { Vmpooler::DummyStatsd.new }
let(:config) {
{
config: {
'site_name' => 'test pooler',
'vm_lifetime_auth' => 2,
},
pools: [
{'name' => 'pool1', 'size' => 5, 'template' => 'templates/pool1'},
{'name' => 'pool2', 'size' => 10}
],
statsd: { 'prefix' => 'stats_prefix'},
alias: { 'poolone' => 'pool1' },
pool_names: [ 'pool1', 'pool2', 'poolone' ]
}
}
let(:current_time) { Time.now } let(:current_time) { Time.now }
@ -170,15 +171,6 @@ describe Vmpooler::API::V1 do
describe 'GET /config' do describe 'GET /config' do
let(:prefix) { '/api/v1' } let(:prefix) { '/api/v1' }
let(:config) {
{
config: {},
pools: [
{'name' => 'pool1', 'size' => 5, 'template' => 'templates/pool1'},
{'name' => 'pool2', 'size' => 10}
],
}
}
it 'returns pool configuration when set' do it 'returns pool configuration when set' do
get "#{prefix}/config" get "#{prefix}/config"