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

add available_storage_domains & available_networks #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/models/concerns/fog_extensions/xenserver/network.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module FogExtensions
module Xenserver
module Network
extend ActiveSupport::Concern

def id
uuid
end
end
end
end
12 changes: 12 additions & 0 deletions app/models/concerns/fog_extensions/xenserver/storage_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion app/models/concerns/foreman_xen/host_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 29 additions & 5 deletions app/models/foreman_xen/xenserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/foreman_xen/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ 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__)

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
Expand Down