Project

General

Profile

« Previous | Next » 

Revision 909cac51

Added by Ben Bettridge about 7 years ago

Cache Templates, Networks & Storage Pools

closes GH-52

View differences:

.rubocop_todo.yml
- 'app/helpers/xen_compute_helper.rb'
- 'lib/foreman_xen/vnc_tunnel.rb'
# Offense count: 15
# Offense count: 16
Metrics/AbcSize:
Max: 65
......
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 347
Max: 368
# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 12
# Offense count: 19
# Offense count: 21
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 52
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 117
# Offense count: 1
# Configuration parameters: CountKeywordArgs.
Metrics/ParameterLists:
Max: 6
# Offense count: 4
Metrics/PerceivedComplexity:
Max: 13
app/assets/javascripts/compute_resources/xenserver/cache_refresh.js
function refreshCache(item, on_success) {
tfm.tools.showSpinner();
attribute_name = $(item).data('attribute')
data = {
type: attribute_name,
compute_resource_id: $(item).data('compute-resource-id')
}
$.ajax({
type:'post',
url: $(item).data('url'),
data: data,
complete: function(){
tfm.tools.hideSpinner();
},
error: function(){
notify(__("Error refreshing cache for " + attribute_name), 'error', true);
},
success: on_success
})
}
app/assets/javascripts/compute_resources/xenserver/populate_fields.js
function xenPopulateNetworks(network_list){
$('#host_compute_attributes_VIFs_print').children().remove();
for (var i = 0; i < network_list.length; i++) {
network = network_list[i];
$('#host_compute_attributes_VIFs_print').append('<option id=' + network['name'] + '>' + network['name'] + '</option>');
}
}
function xenPopulateStoragePools(results){
$('#host_compute_attributes_VBDs_sr_uuid').children().remove();
for (var i = 0; i < results.length; i++) {
result = results[i];
$('#host_compute_attributes_VBDs_sr_uuid').append('<option id=' + result['uuid'] + '>' + result['name'] + '</option>');
}
}
function xenPopulateCustomTemplates(custom_templates){
xenPopulateTemplates(custom_templates, '#host_compute_attributes_custom_template_name');
}
function xenPopulateBuiltinTemplates(builtin_templates){
xenPopulateTemplates(builtin_templates, '#host_compute_attributes_builtin_template_name');
}
function xenPopulateTemplates(results, selector){
$(selector).children().remove();
$(selector).append('<option>No template</option>');
for (var i = 0; i < results.length; i++) {
result = results[i];
$(selector).append('<option id=' + result['name'] + '>' + result['name'] + '</option>');
}
}
app/controllers/foreman_xen/cache_controller.rb
module ForemanXen
class CacheController < ::ApplicationController
before_action :load_compute_resource
# POST = foreman_xen/cache/refresh
def refresh
type = params[:type]
unless cache_attribute_whitelist.include?(type)
process_error(:error_msg => "Error refreshing cache. #{type} is not a white listed attribute")
end
unless @compute_resource.respond_to?("#{type}!")
process_error(:error_msg => "Error refreshing cache. Method '#{type}!' not found for compute resource" +
@compute_resource.name)
end
respond_to do |format|
format.json { render :json => @compute_resource.public_send("#{type}!") }
end
end
private
# List of methods to permit
def cache_attribute_whitelist
%w(networks hypervisors templates custom_templates builtin_templates storage_pools)
end
def load_compute_resource
@compute_resource = ComputeResource.find_by(id: params['compute_resource_id'])
end
end
end
app/helpers/xen_compute_helper.rb
attribute_map[:power_on] = compute_attributes['start']
attribute_map
end
def xen_builtin_template_map(compute_resource)
compute_resource.builtin_templates.map { |t| [t.name, t.name] }
end
def xen_custom_template_map(compute_resource)
compute_resource.custom_templates.map { |t| [t.name, t.name] }
end
def xen_storage_pool_map(compute_resource)
compute_resource.storage_pools.map { |item| [item[:display_name], item[:uuid]] }
end
def xen_hypervisor_map(compute_resource)
compute_resource.available_hypervisors!.map do |t|
[t.name + ' - ' + (
t.metrics.memory_free.to_f / t.metrics.memory_total.to_f * 100
).round(2).to_s + '% free mem', t.name]
end
end
def selectable_f_with_cache_invalidation(f, attr, array,
select_options = {}, html_options = {}, input_group_options = {})
unless html_options.key?('input_group_btn')
html_options[:input_group_btn] = link_to_function(
icon_text('refresh'),
"refreshCache(this, #{input_group_options[:callback]})",
:class => 'btn btn-primary',
:title => _(input_group_options[:title]),
:data => {
:url => input_group_options[:url],
:compute_resource_id => input_group_options[:computer_resource_id],
:attribute => input_group_options[:attribute]
}
)
end
selectable_f(f, attr, array, select_options, html_options)
end
end
app/models/foreman_xen/xenserver.rb
end
def available_hypervisors
tmps = begin
client.hosts
rescue
[]
read_from_cache('available_hypervisors', 'available_hypervisors!')
end
def available_hypervisors!
store_in_cache('available_hypervisors') do
hosts = client.hosts
hosts.sort_by(&:name)
end
tmps.sort_by(&:name)
end
def new_nic(attr = {})
......
end
def storage_pools
results = []
storages = begin
client.storage_repositories.select { |sr| sr.type != 'udev' && sr.type != 'iso' }
rescue
[]
end
hosts = client.hosts
storages.each do |sr|
subresults = {}
found = 0
hosts.each do |host|
next unless sr.reference == host.suspend_image_sr
found = 1
subresults[:name] = sr.name
subresults[:display_name] = sr.name + '(' + host.hostname + ')'
subresults[:uuid] = sr.uuid
break
read_from_cache('storage_pools', 'storage_pools!')
end
def storage_pools!
store_in_cache('storage_pools') do
results = []
storages = client.storage_repositories.select { |sr| sr.type != 'udev' && sr.type != 'iso' }
storages.each do |sr|
subresults = {}
found = false
available_hypervisors.each do |host|
next unless sr.reference == host.suspend_image_sr
found = true
subresults[:name] = sr.name
subresults[:display_name] = sr.name + '(' + host.hostname + ')'
subresults[:uuid] = sr.uuid
break
end
unless found
subresults[:name] = sr.name
subresults[:display_name] = sr.name
subresults[:uuid] = sr.uuid
end
results.push(subresults)
end
if found.zero?
subresults[:name] = sr.name
subresults[:display_name] = sr.name
subresults[:uuid] = sr.uuid
end
results.push(subresults)
results.sort_by! { |item| item[:display_name] }
end
results.sort_by! { |item| item[:display_name] }
results
end
def interfaces
......
end
def networks
networks = begin
client.networks
rescue
[]
read_from_cache('networks', 'networks!')
end
def networks!
store_in_cache('networks') do
client.networks.sort_by(&:name)
end
networks.sort_by(&:name)
end
def templates
client.servers.templates
rescue
[]
read_from_cache('templates', 'templates!')
end
def templates!
store_in_cache('templates') do
client.servers.templates.sort_by(&:name)
end
end
def custom_templates
get_templates(client.servers.custom_templates)
read_from_cache('custom_templates', 'custom_templates!')
end
def custom_templates!
store_in_cache('custom_templates') do
get_templates(client.servers.custom_templates)
end
end
def builtin_templates
get_templates(client.servers.builtin_templates)
read_from_cache('builtin_templates', 'builtin_templates!')
end
def builtin_templates!
store_in_cache('builtin_templates') do
get_templates(client.servers.builtin_templates)
end
end
def associated_host(vm)
......
end
def get_templates(templates)
tmps = begin
templates.select { |t| !t.is_a_snapshot }
rescue
[]
end
tmps = templates.select { |t| !t.is_a_snapshot }
tmps.sort_by(&:name)
end
......
return client.hosts.first unless args[:hypervisor_host] != ''
client.hosts.find { |host| host.name == args[:hypervisor_host] }
end
def read_from_cache(key, fallback)
value = Rails.cache.fetch(cache_key + key) { public_send(fallback) }
value
end
def store_in_cache(key)
value = yield
Rails.cache.write(cache_key + key, value)
value
end
def cache_key
"computeresource_#{id}/"
end
end
end
app/views/compute_resources_vms/form/_hypervisors.html.erb
<div id='templates' class=''>
<div class="form-group">
<%= selectable_f f, :hypervisor_host, [[_("Automatic allocation"), ""]] + compute_resource.available_hypervisors.map { |t| [t.name + " - " + (t.metrics.memory_free.to_f / t.metrics.memory_total.to_f * 100).round(2).to_s + "% free mem", t.name] }, {}, :class => 'form-control span2', :disabled => (controller_name != 'hosts'), :label => 'Hypervisor' %>
<%= selectable_f f, :hypervisor_host,
[[_("Automatic allocation"), ""]] + xen_hypervisor_map(compute_resource),
{},
{ :class => 'form-control span2',
:disabled => (controller_name != 'hosts'),
:label => 'Hypervisor'
}
%>
</div>
</div>
app/views/compute_resources_vms/form/_network.html.erb
<div class="fields">
<%
nat = compute_resource.networks
-%>
<div id='nat' class=''>
<%= selectable_f f, :print, nat.map(&:name),
{ :include_blank => nat.any? ? false : _("No networks"),
:selected => attribute_map[:network_selected] },
{ :class => "span2", :label => _("Network") } %>
<%= selectable_f_with_cache_invalidation f, :print,
compute_resource.networks.map(&:name),
{ :include_blank => compute_resource.networks.any? ? false : _('No networks'),
:selected => attribute_map[:network_selected]
},
{ :class => 'span2',
:label => _('Network')
},
{
:callback => 'xenPopulateNetworks',
:title => 'Refresh available networks',
:url => '/foreman_xen/cache/refresh',
:computer_resource_id => compute_resource.id,
:attribute => 'networks'
}
%>
</div>
</div>
app/views/compute_resources_vms/form/_templates.html.erb
<div class="fields">
<div id='templates' class=''>
<div class="form-group">
<%= selectable_f f, :custom_template_name, [[_("No template"), ""]] + compute_resource.custom_templates.map { |t| [t.name, t.name] }, { :selected => attribute_map[:template_selected_custom] }, :class => 'form-control span2', :label => 'Custom Template' %>
<%= selectable_f_with_cache_invalidation f,
:custom_template_name,
[[_("No template"), ""]] + xen_custom_template_map(compute_resource),
{ :selected => attribute_map[:template_selected_custom] },
{ :class => 'form-control span2',
:label => 'Custom Template'
},
{
:callback => 'xenPopulateCustomTemplates',
:title => 'Refresh available custom templates',
:url => '/foreman_xen/cache/refresh',
:computer_resource_id => compute_resource.id,
:attribute => 'custom_templates'
}
%>
</div>
<div class="form-group ">
<%= selectable_f f, :builtin_template_name, [[_("No template"), ""]] + compute_resource.builtin_templates.map { |t| [t.name, t.name] }, { :selected => attribute_map[:template_selected_builtin] }, :class => 'form-control span2', :label => 'Builtin Template' %>
<%= selectable_f_with_cache_invalidation f,
:builtin_template_name,
[[_("No template"), ""]] + xen_builtin_template_map(compute_resource),
{ :selected => attribute_map[:template_selected_builtin] },
{ :class => 'form-control span2',
:label => 'Builtin Template'
},
{
:callback => 'xenPopulateBuiltinTemplates',
:title => 'Refresh available builtin templates',
:url => '/foreman_xen/cache/refresh',
:computer_resource_id => compute_resource.id,
:attribute => 'builtin_templates'
}
%>
</div>
</div>
</div>
</div>
app/views/compute_resources_vms/form/_volume.html.erb
<div class="fields">
<%= selectable_f f, :sr_uuid, compute_resource.storage_pools.map { |item| [item[:display_name], item[:uuid]] }, { :selected => attribute_map[:volume_selected] }, :class => "span2", :label => _("Storage Repository") %>
<%= selectable_f_with_cache_invalidation f, :sr_uuid,
xen_storage_pool_map(compute_resource),
{ :selected => attribute_map[:volume_selected] },
{ :class => "span2",
:label => _("Storage Repository"),
},
{
:callback => 'xenPopulateStoragePools',
:title => 'Refresh available storage repositories',
:url => '/foreman_xen/cache/refresh',
:computer_resource_id => compute_resource.id,
:attribute => 'storage_pools'
}
%>
<%= text_f f, :physical_size, :class => "input-mini", :label => _("Size (GB)"), :value => attribute_map[:volume_size] %>
</div>
app/views/compute_resources_vms/form/xenserver/_base.html.erb
}
})
</script>
<%= compute_specific_js(compute_resource, 'cache_refresh') %>
<%= compute_specific_js(compute_resource, 'populate_fields') %>
config/routes.rb
match 'snapshots/:id/delete/:ref', :to => 'snapshots#destroy', :via => 'get'
match 'snapshots/:id/create', :to => 'snapshots#create', :via => 'post'
match 'cache/refresh', :to => 'cache#refresh', :via => 'post'
end
end
lib/foreman_xen/engine.rb
end
end
assets_to_precompile =
Dir.chdir(root) do
Dir['app/assets/javascripts/**/*', 'app/assets/stylesheets/**/*'].map do |f|
f.split(File::SEPARATOR, 4).last
end
end
initializer 'foreman_xen.assets.precompile' do |app|
app.config.assets.precompile += assets_to_precompile
end
initializer 'foreman_xen.configure_assets', group: :assets do
SETTINGS[:foreman_xen] = { assets: { precompile: assets_to_precompile } }
end
config.to_prepare do
begin
# extend fog xen server and image models.

Also available in: Unified diff