Skip to content

Commit

Permalink
Refs #36745 - VM is not associated upon host registration
Browse files Browse the repository at this point in the history
  • Loading branch information
goarsna committed Oct 5, 2023
1 parent 9e62b6b commit bcd4496
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 40 deletions.
29 changes: 11 additions & 18 deletions app/models/compute_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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).
Expand Down
12 changes: 8 additions & 4 deletions app/models/compute_resources/foreman/model/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions app/models/compute_resources/foreman/model/libvirt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
12 changes: 8 additions & 4 deletions app/models/compute_resources/foreman/model/openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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+.*})
Expand Down
12 changes: 8 additions & 4 deletions app/models/compute_resources/foreman/model/ovirt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions app/models/compute_resources/foreman/model/vmware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bcd4496

Please sign in to comment.