From bcd4496c2d235cd4b4607b33fd977b42a61cad39 Mon Sep 17 00:00:00 2001 From: Markus Reisner Date: Thu, 5 Oct 2023 10:53:35 +0200 Subject: [PATCH] Refs #36745 - VM is not associated upon host registration --- app/models/compute_resource.rb | 29 +++++++------------ .../compute_resources/foreman/model/ec2.rb | 12 +++++--- .../foreman/model/libvirt.rb | 14 +++++---- .../foreman/model/openstack.rb | 12 +++++--- .../compute_resources/foreman/model/ovirt.rb | 12 +++++--- .../compute_resources/foreman/model/vmware.rb | 12 +++++--- 6 files changed, 51 insertions(+), 40 deletions(-) diff --git a/app/models/compute_resource.rb b/app/models/compute_resource.rb index 700ecdeaf50..32765ba5daa 100644 --- a/app/models/compute_resource.rb +++ b/app/models/compute_resource.rb @@ -399,18 +399,6 @@ def normalize_vm_attrs(vm_attrs) vm_attrs end - def associated_host(vm) - associate_by(vm) - end - - def associated_vm(host) - vms(:eager_loading => true).each do |vm| - if associate_by(vm, host) - return vm - end - end - end - protected def memory_gb_to_bytes(memory_size) @@ -460,13 +448,18 @@ def nested_attributes_for(type, opts) end.compact end - def associate_by(name, attributes, host = nil) + def associate_by(name, attributes) attributes = Array.wrap(attributes).map { |mac| Net::Validations.normalize_mac(mac) } if name == 'mac' - host_query = Host.authorized(:view_hosts, Host) - if host - host_query = host_query.where(:id => host.id) - end - host_query.joins(:primary_interface). + Host.authorized(:view_hosts, Host).joins(:primary_interface). + where(:nics => {:primary => true}). + where(ActiveRecord::Base.sanitize_sql("nics.#{name}") => attributes). + readonly(false). + first + end + + def associate_by_host(name, attributes, host) + attributes = Array.wrap(attributes).map { |mac| Net::Validations.normalize_mac(mac) } if name == 'mac' + Host.authorized(:view_hosts, Host).where(:id => host.id).joins(:primary_interface). where(:nics => {:primary => true}). where(ActiveRecord::Base.sanitize_sql("nics.#{name}") => attributes). readonly(false). diff --git a/app/models/compute_resources/foreman/model/ec2.rb b/app/models/compute_resources/foreman/model/ec2.rb index c67b31c077c..55b5b8f0bdd 100644 --- a/app/models/compute_resources/foreman/model/ec2.rb +++ b/app/models/compute_resources/foreman/model/ec2.rb @@ -143,6 +143,14 @@ def update_required?(old_attrs, new_attrs) false end + def associated_host(vm) + associate_by("ip", [vm.public_ip_address, vm.private_ip_address]) + end + + def associated_vm(host) + vms(:eager_loading => true).find { |vm| associate_by_host("ip", [vm.public_ip_address, vm.private_ip_address], host) } + end + def user_data_supported? true end @@ -175,10 +183,6 @@ def normalize_vm_attrs(vm_attrs) private - def associate_by(vm, host = nil) - super("ip", [vm.public_ip_address, vm.private_ip_address], host) - end - def subnet_implies_is_vpc?(args) args[:subnet_id].present? end diff --git a/app/models/compute_resources/foreman/model/libvirt.rb b/app/models/compute_resources/foreman/model/libvirt.rb index 024e177719b..295a0841e54 100644 --- a/app/models/compute_resources/foreman/model/libvirt.rb +++ b/app/models/compute_resources/foreman/model/libvirt.rb @@ -196,6 +196,14 @@ def hypervisor client.nodes.first end + def associated_host(vm) + associate_by("mac", vm.interfaces.map(&:mac)) + end + + def associated_vm(host) + vms(:eager_loading => true).find { |vm| associate_by_host("mac", vm.interfaces.map(&:mac), host) } + end + def vm_compute_attributes_for(uuid) vm_attrs = super if vm_attrs[:memory_size].nil? @@ -324,11 +332,5 @@ def validate_volume_capacity(vol) raise Foreman::Exception.new(N_("Please specify volume size. You may optionally use suffix 'G' to specify volume size in gigabytes.")) end end - - private - - def associate_by(vm, host = nil) - super("mac", vm.interfaces.map(&:mac), host) - end end end diff --git a/app/models/compute_resources/foreman/model/openstack.rb b/app/models/compute_resources/foreman/model/openstack.rb index 568e5424efe..67bdb201924 100644 --- a/app/models/compute_resources/foreman/model/openstack.rb +++ b/app/models/compute_resources/foreman/model/openstack.rb @@ -200,6 +200,14 @@ def console(uuid) vm.console.body.merge({'timestamp' => Time.now.utc}) end + def associated_host(vm) + associate_by("ip", [vm.floating_ip_address, vm.private_ip_address].compact) + end + + def associated_vm(host) + vms(:eager_loading => true).find { |vm| associate_by_host("ip", [vm.floating_ip_address, vm.private_ip_address].compact, host) } + end + def flavor_name(flavor_ref) client.flavors.get(flavor_ref).try(:name) end @@ -256,10 +264,6 @@ def normalize_vm_attrs(vm_attrs) private - def associate_by(vm, host = nil) - super("ip", [vm.floating_ip_address, vm.private_ip_address].compact, host) - end - def url_for_fog u = URI.parse(url) match_data = u.path.match(%r{(.*)\/v\d+.*}) diff --git a/app/models/compute_resources/foreman/model/ovirt.rb b/app/models/compute_resources/foreman/model/ovirt.rb index 5a3c7fe5fac..a30ad96b1bf 100644 --- a/app/models/compute_resources/foreman/model/ovirt.rb +++ b/app/models/compute_resources/foreman/model/ovirt.rb @@ -408,6 +408,14 @@ def update_required?(old_attrs, new_attrs) false end + def associated_host(vm) + associate_by("mac", vm.interfaces.map(&:mac)) + end + + def associated_vm(host) + vms(:eager_loading => true).find { |vm| associate_by_host("mac", vm.interfaces.map(&:mac), host) } + end + def self.provider_friendly_name "oVirt" end @@ -566,10 +574,6 @@ def ca_cert private - def associate_by(vm, host = nil) - super("mac", vm.interfaces.map(&:mac), host) - end - def update_available_operating_systems return false if errors.any? ovirt_operating_systems = client.operating_systems if client.respond_to?(:operating_systems) diff --git a/app/models/compute_resources/foreman/model/vmware.rb b/app/models/compute_resources/foreman/model/vmware.rb index 66ce6ca001e..9a3d88df6e1 100644 --- a/app/models/compute_resources/foreman/model/vmware.rb +++ b/app/models/compute_resources/foreman/model/vmware.rb @@ -623,6 +623,14 @@ def pubkey_hash=(key) attrs[:pubkey_hash] = key end + def associated_host(vm) + associate_by("mac", vm.interfaces.map(&:mac)) + end + + def associated_vm(host) + vms(:eager_loading => true).find { |vm| associate_by_host("mac", vm.interfaces.map(&:mac), host) } + end + def display_type attrs[:display] || 'vmrc' end @@ -725,10 +733,6 @@ def normalize_vm_attrs(vm_attrs) private - def associate_by(vm, host = nil) - super("mac", vm.interfaces.map(&:mac), host) - end - def dc client.datacenters.get(datacenter) end