diff --git a/app/models/concerns/fog_extensions/xenserver/network.rb b/app/models/concerns/fog_extensions/xenserver/network.rb new file mode 100644 index 0000000..eee12c9 --- /dev/null +++ b/app/models/concerns/fog_extensions/xenserver/network.rb @@ -0,0 +1,11 @@ +module FogExtensions + module Xenserver + module Network + extend ActiveSupport::Concern + + def id + uuid + end + end + end +end diff --git a/app/models/concerns/fog_extensions/xenserver/storage_repository.rb b/app/models/concerns/fog_extensions/xenserver/storage_repository.rb index a29c010..71b6b0d 100644 --- a/app/models/concerns/fog_extensions/xenserver/storage_repository.rb +++ b/app/models/concerns/fog_extensions/xenserver/storage_repository.rb @@ -8,6 +8,10 @@ module StorageRepository prepend FogExtensions::Xenserver::StorageRepository end + def id + uuid + end + def initialize(new_attributes = {}) super(new_attributes) attributes[:display_name] = init_display_name @@ -29,6 +33,14 @@ def physical_utilisation_gb physical_utilisation.to_i / 1024 / 1024 / 1024 end + def capacity + physical_size_gb + end + + def freespace + free_space + end + def init_display_name srname = name unless shared diff --git a/app/models/concerns/foreman_xen/host_extensions.rb b/app/models/concerns/foreman_xen/host_extensions.rb index 49f768e..692ea6a 100644 --- a/app/models/concerns/foreman_xen/host_extensions.rb +++ b/app/models/concerns/foreman_xen/host_extensions.rb @@ -3,7 +3,10 @@ module HostExtensions extend ActiveSupport::Concern def built(installed = true) - compute_resource.cleanup_configdrive(uuid) if compute_resource && compute_resource.type == 'ForemanXen::Xenserver' + if compute_resource && compute_resource.type == 'ForemanXen::Xenserver' + compute_resource.detach_cdrom(uuid) + compute_resource.cleanup_configdrive(uuid) + end super(installed) end diff --git a/app/models/foreman_xen/xenserver.rb b/app/models/foreman_xen/xenserver.rb index df4236c..ea2017f 100644 --- a/app/models/foreman_xen/xenserver.rb +++ b/app/models/foreman_xen/xenserver.rb @@ -39,6 +39,15 @@ def iso_library_mountpoint=(path) attrs[:iso_library_mountpoint] = mountpoint end + def detach_cdrom(uuid) + begin + vm = find_vm_by_uuid(uuid) + detach_cdrom_vbd(vm) if vm + rescue => e + logger.error "unable to detach cdrom:#{e}" + end + end + def cleanup_configdrive(uuid) iso_file_name = "foreman-configdrive-#{uuid}.iso" begin @@ -110,6 +119,14 @@ def available_images custom_templates! end + def available_storage_domains(*) + storage_pools + end + + def available_networks(*) + networks + end + def available_hypervisors hypervisors.select(&:enabled) end @@ -273,8 +290,8 @@ def vm_attr_from_args(args) { name: args[:name], name_description: args[:comment], - vcpus_max: args[:vcpus_max], - vcpus_at_startup: args[:vcpus_max], + VCPUs_max: args[:vcpus_max], + VCPUs_at_startup: args[:vcpus_max], memory_static_max: args[:memory_max], memory_dynamic_max: args[:memory_max], memory_dynamic_min: args[:memory_min], @@ -374,9 +391,9 @@ def set_vm_profile_attributes(vm, attr) mem = %w[memory_static_max memory_dynamic_max memory_dynamic_min memory_static_min] mem.reverse! if vm.memory_static_max.to_i > attr[:memory_static_max].to_i - # VCPU values must satisfy: 0 < vcpus_at_startup <= vcpus_max - cpu = %w[vcpus_max vcpus_at_startup] - cpu.reverse! if vm.vcpus_at_startup > attr[:vcpus_at_startup] + # VCPU values must satisfy: 0 < VCPUs_at_startup <= VCPUs_max + cpu = %w[VCPUs_max VCPUs_at_startup] + cpu.reverse! if vm.vcpus_at_startup > attr[:VCPUs_at_startup] (mem + cpu).each { |e| vm.set_attribute e, attr[e.to_sym] } end @@ -489,6 +506,13 @@ def attach_iso(vm, iso_vdi) true end + def detach_cdrom_vbd(vm) + cd_drive = client.vbds.find { |v| v.vm == vm && v.type == 'CD' } + unless cd_drive&.empty + client.eject_vbd cd_drive.reference + end + end + def find_free_userdevice(vm) # Find next free userdevice id for vbd # vm.vbds is not current, vm.reload not working. diff --git a/lib/foreman_xen/engine.rb b/lib/foreman_xen/engine.rb index ec68bcc..6ab31ad 100644 --- a/lib/foreman_xen/engine.rb +++ b/lib/foreman_xen/engine.rb @@ -45,10 +45,12 @@ class Engine < ::Rails::Engine require 'fog/xenserver/compute/models/server' require 'fog/xenserver/compute/models/host' require 'fog/xenserver/compute/models/vdi' + require 'fog/xenserver/compute/models/network' require 'fog/xenserver/compute/models/storage_repository' require File.expand_path('../../app/models/concerns/fog_extensions/xenserver/server', __dir__) require File.expand_path('../../app/models/concerns/fog_extensions/xenserver/host', __dir__) require File.expand_path('../../app/models/concerns/fog_extensions/xenserver/vdi', __dir__) + require File.expand_path('../../app/models/concerns/fog_extensions/xenserver/network', __dir__) require File.expand_path('../../app/models/concerns/fog_extensions/xenserver/storage_repository', __dir__) require File.expand_path('../../app/models/concerns/foreman_xen/host_helper_extensions', __dir__) require File.expand_path('../../app/models/concerns/foreman_xen/host_extensions', __dir__) @@ -56,6 +58,7 @@ class Engine < ::Rails::Engine Fog::XenServer::Compute::Models::Server.include ::FogExtensions::Xenserver::Server Fog::XenServer::Compute::Models::Host.include ::FogExtensions::Xenserver::Host Fog::XenServer::Compute::Models::Vdi.include ::FogExtensions::Xenserver::Vdi + Fog::XenServer::Compute::Models::Network.include ::FogExtensions::Xenserver::Network Fog::XenServer::Compute::Models::StorageRepository.include ::FogExtensions::Xenserver::StorageRepository ::HostsHelper.include ForemanXen::HostHelperExtensions ::Host::Managed.prepend ForemanXen::HostExtensions