Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Fixes #17860,#12051,#22277,#22621 Various big rewrites
Browse files Browse the repository at this point in the history
- implement image provisioning
- improve volume support
- add cloud-init support
- UI improvements
- heavy refactoring
  • Loading branch information
Niels Baumgartner authored and mmoll committed Jun 30, 2019
1 parent f0a9868 commit 717923b
Show file tree
Hide file tree
Showing 28 changed files with 755 additions and 718 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ Please see the Foreman manual for further instructions:

* [Foreman: How to Install a Plugin](http://theforeman.org/plugins/#2.Installation)

## Image based provisioning

In order to use the cloud-init functionality users need to:

- install the `genisoimage` package
- mount a "NFS ISO Library" (as XenServer calls it) which is attached to the Xen pool to a location writable by the foreman user.
- set this mount point / path as ISO library mountpoint in the compute resource

foreman_xen then creates a network configuration file, renders the user_data template, puts them in an ISO, copies this ISO to the attached ISO-library and attaches it to the created VM, where cloud-init can use the data provided to initialize the VM.

## Compatibility

| Foreman Version | Plugin Version |
Expand Down
49 changes: 31 additions & 18 deletions app/assets/javascripts/foreman_xen/xenserver/cache_refresh.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
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')
function refreshCache(item) {
tfm.tools.showSpinner();
attribute_name = $(item).data('attribute');
sel = $(item).closest('.input-group').children('select')
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: function(results, textStatus, jqXHR){
var elements = sel.children()
if (elements.first().val() == "") { //include_empty
elements = elements.slice(1);
}
elements.remove();
for (var i = 0; i < results.length; i++) {
var result = results[i];
var id = ('uuid' in result) ? result['uuid'] : result['id'];
var name = ('display_name' in result) ? result['display_name'] : result['name'];
sel.append('<option value=' + id + '>' + name + '</option>');
}
}
$.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
})
});
}
32 changes: 0 additions & 32 deletions app/assets/javascripts/foreman_xen/xenserver/populate_fields.js

This file was deleted.

9 changes: 7 additions & 2 deletions app/controllers/foreman_xen/cache_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ def refresh
end

respond_to do |format|
format.json { render :json => @compute_resource.public_send("#{type}!") }
format.json do
filtered_data = @compute_resource.public_send("#{type}!").map do |e|
e.attributes.slice(:id, :uuid, :name, :display_name)
end
render json: filtered_data
end
end
end

private

# List of methods to permit
def cache_attribute_whitelist
%w[networks hypervisors templates custom_templates builtin_templates storage_pools]
%w[isos networks available_hypervisors hypervisors templates builtin_templates storage_pools]
end

def load_compute_resource
Expand Down
111 changes: 8 additions & 103 deletions app/helpers/xen_compute_helper.rb
Original file line number Diff line number Diff line change
@@ -1,121 +1,26 @@
module XenComputeHelper
def compute_attribute_map(params, compute_resource, new)
if controller_name == 'hosts'
attribute_map = hosts_controller_compute_attribute_map(params, compute_resource, new)
elsif controller_name == 'compute_attributes'
attribute_map = compute_resource_controller_attribute_map(params, compute_resource)
end
attribute_map
end
def compute_attributes_from_params(compute_resource)
id = params.dig('host', 'compute_profile_id') || params.dig('compute_profile_id')
return compute_resource.compute_profile_attributes_for id if id

def init_vmdata
vmdata = {
:ifs => {
'0' => {
:ip => '',
:gateway => '',
:netmask => ''
}
},
:nameserver1 => '',
:nameserver2 => '',
:environment => ''
}
{}
end

private

def hosts_controller_compute_attribute_map(params, compute_resource, new)
attribute_map = empty_attribute_map
if new.try(:new_record?)
compute_attributes = compute_resource.compute_profile_attributes_for(params['host']['compute_profile_id'])
attribute_map = filter_compute_attributes(attribute_map, compute_attributes)
elsif new
attribute_map[:cpu_count] = new.vcpus_max || nil
attribute_map[:memory_min] = new.memory_static_min || nil
attribute_map[:memory_max] = new.memory_static_max || nil
if new.vbds
vdi = new.vbds.first.vdi
if vdi
attribute_map[:volume_selected] = vdi.sr.uuid || nil
attribute_map[:volume_size] = vdi.virtual_size ? (vdi.virtual_size.to_i / 1_073_741_824).to_s : nil
end
end
attribute_map[:network_selected] = new.vifs.first.network.name || nil if new.vifs
end
attribute_map
end

def compute_resource_controller_attribute_map(params, compute_resource)
attribute_map = empty_attribute_map
if params && params['compute_profile_id']
compute_attributes = compute_resource.compute_profile_attributes_for(params['compute_profile_id'])
elsif params && params['host'] && params['host']['compute_profile_id']
compute_attributes = compute_resource.compute_profile_attributes_for(params['host']['compute_profile_id'])
end
attribute_map = filter_compute_attributes(attribute_map, compute_attributes) if compute_attributes
attribute_map
end

def empty_attribute_map
{ :volume_size => nil,
:volume_selected => nil,
:network_selected => nil,
:template_selected_custom => nil,
:template_selected_builtin => nil,
:cpu_count => nil,
:memory_min => nil,
:memory_max => nil,
:power_on => nil }
end

def filter_compute_attributes(attribute_map, compute_attributes)
if compute_attributes['VBDs']
attribute_map[:volume_size] = compute_attributes['VBDs']['physical_size']
attribute_map[:volume_selected] = compute_attributes['VBDs']['sr_uuid']
end
attribute_map[:network_selected] = compute_attributes['VIFs']['print'] if compute_attributes['VIFs']
attribute_map[:template_selected_custom] = compute_attributes['custom_template_name']
attribute_map[:template_selected_builtin] = compute_attributes['builtin_template_name']
attribute_map[:cpu_count] = compute_attributes['vcpus_max']
attribute_map[:memory_min] = compute_attributes['memory_min']
attribute_map[:memory_max] = compute_attributes['memory_max']
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]})",
'refreshCache(this)',
: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]
:compute_resource_id => input_group_options[:compute_resource_id],
:attribute => input_group_options[:attribute],
:select_attr => attr
}
)
end
Expand Down
23 changes: 23 additions & 0 deletions app/models/concerns/fog_extensions/xenserver/host.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module FogExtensions
module Xenserver
module Host
extend ActiveSupport::Concern

included do
attribute :display_name
prepend FogExtensions::Xenserver::Host
end

def initialize(new_attributes = {})
super(new_attributes)
attributes[:display_name] = "#{name} - #{mem_free_gb} GB free memory"
end

def mem_free_gb
return metrics.memory_free.to_i / 1024 / 1024 / 1024 if metrics

0
end
end
end
end
24 changes: 21 additions & 3 deletions app/models/concerns/fog_extensions/xenserver/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ module Server

include ActionView::Helpers::NumberHelper

attr_accessor :start
attr_accessor :memory_min, :memory_max, :custom_template_name, :builtin_template_name, :hypervisor_host
attr_accessor :start, :image_id, :hypervisor_host, :iso, :target_sr
attr_accessor :memory_min, :memory_max, :builtin_template
attr_writer :volumes, :interfaces

def id
uuid
end

def to_s
name
Expand All @@ -16,6 +21,13 @@ def nics_attributes=(attrs); end

def volumes_attributes=(attrs); end

def volumes
@volumes ||= []
disks = vbds.compact.select(&:disk?)
disks.sort! { |x, y| x.userdevice <=> y.userdevice }
(disks.map(&:vdi) + @volumes).uniq
end

def memory
memory_static_max.to_i
end
Expand Down Expand Up @@ -45,12 +57,18 @@ def vm_description
end

def interfaces
vifs
(vifs + @interfaces).uniq
end

def select_nic(fog_nics, nic)
fog_nics[0]
end

def user_data
return !other_config['default_template'] if is_a_template

false
end
end
end
end
45 changes: 45 additions & 0 deletions app/models/concerns/fog_extensions/xenserver/storage_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module FogExtensions
module Xenserver
module StorageRepository
extend ActiveSupport::Concern

included do
attribute :display_name
prepend FogExtensions::Xenserver::StorageRepository
end

def initialize(new_attributes = {})
super(new_attributes)
attributes[:display_name] = init_display_name
end

def free_space
physical_size.to_i - physical_utilisation.to_i
end

def free_space_gb
free_space.to_i / 1024 / 1024 / 1024
end

def physical_size_gb
physical_size.to_i / 1024 / 1024 / 1024
end

def physical_utilisation_gb
physical_utilisation.to_i / 1024 / 1024 / 1024
end

def init_display_name
srname = name
unless shared
pbd = pbds.first
srname = "#{name} - #{pbd.host.name}" unless pbd.nil?
end
format('%{n} (%{f}: %{f_gb} GB - %{u}: %{u_gb} GB - %{t}: %{t_gb} GB)',
n: srname, f: _('free'), f_gb: free_space_gb,
u: _('used'), u_gb: physical_utilisation_gb,
t: _('total'), t_gb: physical_size_gb)
end
end
end
end
11 changes: 11 additions & 0 deletions app/models/concerns/fog_extensions/xenserver/vdi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module FogExtensions
module Xenserver
module Vdi
extend ActiveSupport::Concern

def id
uuid
end
end
end
end
Loading

0 comments on commit 717923b

Please sign in to comment.