Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .rubocop_local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Style/GlobalVars:
AllowedVariables:
- $proxmox_log
26 changes: 26 additions & 0 deletions app/models/manageiq/providers/proxmox/inventory/collector.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
class ManageIQ::Providers::Proxmox::Inventory::Collector < ManageIQ::Providers::Inventory::Collector
private

def collect_vm_details(vm)
base = "/nodes/#{vm["node"]}/qemu/#{vm["vmid"]}"
config = connection.request(:get, "#{base}/config")
status = connection.request(:get, "#{base}/status/current")

details = {
"config" => config,
"status" => status,
"name" => config&.dig("name"),
"template" => config&.dig("template")
}

if status&.dig("status") == "running" && config&.dig("agent")&.to_s&.start_with?("1")
begin
details["agent_info"] = connection.request(:get, "#{base}/agent/get-osinfo")&.dig("result")
details["networks"] = connection.request(:get, "#{base}/agent/network-get-interfaces")&.dig("result")
details["hostname"] = connection.request(:get, "#{base}/agent/get-host-name")&.dig("result", "host-name")
rescue RuntimeError => err
$proxmox_log.warn("Proxmox agent not responding for VM #{vm["vmid"]}: #{err.message}")
end
end

vm.merge(details)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def nodes
end

def vms
@vms ||= cluster_resources_by_type["qemu"]
@vms ||= cluster_resources_by_type["qemu"]&.map { |vm| collect_vm_details(vm) } || []
end

def storages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ def vms
node, vmid = vm_ref.split("/")
next unless node && vmid

status_data = connection.request(:get, "/nodes/#{node}/qemu/#{vmid}/status/current")
status_data&.merge("node" => node, "id" => "qemu/#{vmid}")
end.compact
collect_vm_details("node" => node, "vmid" => vmid, "id" => "qemu/#{vmid}")
end
end

def storages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,142 @@ def vms

collector.vms.each do |vm|
ems_ref = vm["id"].gsub("qemu/", "")
host = persister.hosts.lazy_find(vm["node"]) if vm["node"]
template = vm["template"] == 1
raw_power_state = template ? "never" : vm["status"]
config = vm["config"] || {}
status = vm["status"] || {}

vm_obj = persister.vms_and_templates.build(
:type => "#{persister.manager.class}::#{template ? "Template" : "Vm"}",
:ems_ref => ems_ref,
:uid_ems => ems_ref,
:name => vm["name"],
:template => template,
:raw_power_state => raw_power_state,
:host => host,
:raw_power_state => template ? "never" : (status["qmpstatus"] || vm["status"]),
:host => persister.hosts.lazy_find(vm["node"]),
:ems_cluster => cluster,
:location => "#{vm["node"]}/#{vm["vmid"]}",
:vendor => "proxmox"
:vendor => "proxmox",
:description => config["description"],
:tools_status => parse_tools_status(config, vm["agent_info"])
)

hardware = parse_hardware(vm_obj, config, status)
parse_disks(hardware, config)
parse_networks(hardware, vm)
parse_operating_system(vm_obj, vm["agent_info"], config)
end
end

def parse_tools_status(config, agent_info)
return "toolsNotInstalled" unless config["agent"].to_s.start_with?("1")

agent_info.present? ? "toolsOk" : "toolsNotRunning"
end

def parse_hardware(vm_obj, config, status)
persister.hardwares.build(
:vm_or_template => vm_obj,
:cpu_total_cores => status["cpus"] || (config["cores"].to_i * config.fetch("sockets", 1).to_i),
:memory_mb => (status["maxmem"] || (config["memory"].to_i * 1024 * 1024)) / 1024 / 1024,
:disk_capacity => status["maxdisk"]
)
end

def parse_disks(hardware, config)
disk_keys = config.keys.grep(/^(scsi|ide|sata|virtio|nvme)\d+$/)

disk_keys.each do |disk_id|
disk_str = config[disk_id]
next if disk_str.include?("media=cdrom")

size_bytes = parse_disk_size(disk_str)
storage_name = disk_str.split(":").first

persister.disks.build(
:hardware => hardware,
:device_name => disk_id,
:device_type => "disk",
:controller_type => disk_id.gsub(/\d+$/, ""),
:size => size_bytes,
:location => disk_id,
:filename => disk_str.split(",").first,
:storage => persister.storages.lazy_find(storage_name)
)
end
end

def parse_disk_size(disk_str)
return nil unless disk_str

size_match = disk_str.match(/size=(\d+)([TGMK]?)/)
return nil unless size_match

size = size_match[1].to_i
unit = size_match[2]

case unit
when "T" then size * 1024 * 1024 * 1024 * 1024
when "G" then size * 1024 * 1024 * 1024
when "M" then size * 1024 * 1024
when "K" then size * 1024
else size
end
end

def parse_networks(hardware, vm)
agent_networks = vm["networks"]
return unless agent_networks

agent_networks.each do |iface|
next if iface["name"] == "lo"

mac = iface["hardware-address"]&.downcase
ipv4 = iface["ip-addresses"]&.find { |ip| ip["ip-address-type"] == "ipv4" }
ipv6 = iface["ip-addresses"]&.find { |ip| ip["ip-address-type"] == "ipv6" }

network = persister.networks.build(
:hardware => hardware,
:description => iface["name"],
:hostname => vm["hostname"],
:ipaddress => ipv4&.dig("ip-address"),
:ipv6address => ipv6&.dig("ip-address")
)

persister.guest_devices.build(
:hardware => hardware,
:uid_ems => "#{vm["vmid"]}-#{iface["name"]}",
:device_name => iface["name"],
:device_type => "ethernet",
:controller_type => "ethernet",
:address => mac,
:network => network
)
end
end

def parse_mac(net_config)
net_config&.match(/([0-9a-fA-F:]{17})/)&.[](1)&.downcase
end

def parse_operating_system(vm_obj, agent_info, config)
product_name = if agent_info.present?
agent_info["pretty-name"] || agent_info["name"]
else
map_ostype(config["ostype"])
end

persister.operating_systems.build(
:vm_or_template => vm_obj,
:product_name => product_name || "Unknown"
)
end

def map_ostype(ostype)
case ostype.to_s
when /^w/ then "Windows"
when "l26" then "Linux"
when "l24" then "Linux (2.4 kernel)"
else ostype
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ def initialize_inventory_collections
add_collection(infra, :host_hardwares)
add_collection(infra, :storages)
add_collection(infra, :host_storages)
add_collection(infra, :disks, :parent_inventory_collections => %i[vms_and_templates])
add_collection(infra, :guest_devices, :parent_inventory_collections => %i[vms_and_templates])
add_collection(infra, :hardwares, :parent_inventory_collections => %i[vms_and_templates])
add_collection(infra, :networks, :parent_inventory_collections => %i[vms_and_templates])
add_collection(infra, :operating_systems, :parent_inventory_collections => %i[vms_and_templates])
add_collection(infra, :vms_and_templates, {}, {:without_sti => true}) do |builder|
builder.vm_template_shared
Expand Down
122 changes: 94 additions & 28 deletions spec/models/manageiq/providers/proxmox/infra_manager/refresher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
before { with_vcr { described_class.refresh([ems]) } }

context "with a Host target" do
let(:target) { ems.hosts.find_by(:ems_ref => "pve") }
let(:target) { ems.hosts.find_by(:ems_ref => "vmpvetest") }
before { with_vcr("targeted-refresh/host") { described_class.refresh(targets) } }

it "doesn't impact unrelated inventory" do
Expand All @@ -45,7 +45,7 @@
end

it "updates the host's power state" do
expect(target.reload.power_state).to eq("off")
expect(target.reload.power_state).to eq("on")
end
end

Expand All @@ -69,74 +69,140 @@
end

def assert_counts
expect(Vm.count).to eq(1)
expect(MiqTemplate.count).to eq(1)
expect(Host.count).to eq(1)
expect(Storage.count).to eq(2)
expect(Vm.count).to eq(6)
expect(MiqTemplate.count).to eq(14)
expect(Host.count).to eq(2)
expect(Storage.count).to eq(4)
expect(EmsCluster.count).to eq(1)
end

def assert_ems_counts
expect(ems.vms.count).to eq(1)
expect(ems.miq_templates.count).to eq(1)
expect(ems.hosts.count).to eq(1)
expect(ems.storages.count).to eq(2)
expect(ems.vms.count).to eq(6)
expect(ems.miq_templates.count).to eq(14)
expect(ems.hosts.count).to eq(2)
expect(ems.storages.count).to eq(4)
expect(ems.ems_clusters.count).to eq(1)
end

def assert_specific_cluster
cluster = ems.ems_clusters.find_by(:ems_ref => "cluster")
expect(cluster).to have_attributes(
:name => "Cluster",
:name => "pvetest",
:uid_ems => "cluster",
:ems_ref => "cluster"
)
end

def assert_specific_host
host = ems.hosts.find_by(:ems_ref => "pve")
host = ems.hosts.find_by(:ems_ref => "vmpvetest")
expect(host).to have_attributes(
:name => "pve",
:name => "vmpvetest",
:vmm_vendor => "proxmox",
:vmm_version => nil,
:vmm_product => "Proxmox VE",
:vmm_buildnumber => nil,
:power_state => "on",
:uid_ems => "pve",
:ems_ref => "pve",
:uid_ems => "vmpvetest",
:ems_ref => "vmpvetest",
:ems_cluster => ems.ems_clusters.find_by(:ems_ref => "cluster")
)
expect(host.hardware).to have_attributes(
:cpu_total_cores => 2,
:memory_mb => 1_967
:cpu_total_cores => 8,
:memory_mb => 32_096
)
end

def assert_specific_vm
vm = ems.vms.find_by(:ems_ref => "100")
expect(vm).to have_attributes(
:vendor => "proxmox",
:name => "vm-test",
:location => "pve/100",
:host => ems.hosts.find_by(:ems_ref => "pve"),
:name => "vmpvevm1",
:location => "vmpvetest/100",
:host => ems.hosts.find_by(:ems_ref => "vmpvetest"),
:ems_cluster => ems.ems_clusters.find_by(:ems_ref => "cluster"),
:uid_ems => "100",
:ems_ref => "100",
:power_state => "off",
:raw_power_state => "stopped"
:power_state => "on",
:raw_power_state => "running",
:tools_status => "toolsOk"
)

assert_specific_vm_hardware(vm)
assert_specific_vm_disks(vm)
assert_specific_vm_networks(vm)
assert_specific_vm_os(vm)
end

def assert_specific_vm_hardware(vm)
expect(vm.hardware).to have_attributes(
:cpu_total_cores => 4,
:memory_mb => 2048,
:disk_capacity => 10_737_418_240
)
end

def assert_specific_vm_disks(vm)
expect(vm.hardware.disks.count).to be >= 3

scsi0 = vm.hardware.disks.find_by(:device_name => "scsi0")
expect(scsi0).to have_attributes(
:device_type => "disk",
:controller_type => "scsi",
:size => 10_737_418_240,
:location => "scsi0",
:filename => "local-lvm:vm-100-disk-1"
)

scsi1 = vm.hardware.disks.find_by(:device_name => "scsi1")
expect(scsi1).to have_attributes(
:device_type => "disk",
:controller_type => "scsi",
:size => 2_147_483_648,
:location => "scsi1"
)

scsi2 = vm.hardware.disks.find_by(:device_name => "scsi2")
expect(scsi2).to have_attributes(
:device_type => "disk",
:controller_type => "scsi",
:size => 3_221_225_472,
:location => "scsi2"
)
end

def assert_specific_vm_networks(vm)
expect(vm.hardware.networks.count).to be >= 1

ens18 = vm.hardware.networks.find_by(:description => "ens18")
expect(ens18).to have_attributes(
:hostname => "vmpvevm1",
:ipaddress => "192.0.2.253"
)

guest_device = vm.hardware.guest_devices.find_by(:device_name => "ens18")
expect(guest_device).to have_attributes(
:device_type => "ethernet",
:controller_type => "ethernet",
:address => "bc:24:11:e2:9f:14"
)
end

def assert_specific_vm_os(vm)
expect(vm.operating_system).to have_attributes(
:product_name => "Debian GNU/Linux 13 (trixie)"
)
end

def assert_specific_template
template = ems.miq_templates.find_by(:ems_ref => "101")
template = ems.miq_templates.find_by(:ems_ref => "900")
expect(template).to have_attributes(
:vendor => "proxmox",
:name => "vm-template",
:location => "pve/101",
:host => ems.hosts.find_by(:ems_ref => "pve"),
:name => "debian-13-2025-11-17T19-50-19Z-00000000",
:location => "vmpvetest/900",
:host => ems.hosts.find_by(:ems_ref => "vmpvetest"),
:ems_cluster => ems.ems_clusters.find_by(:ems_ref => "cluster"),
:uid_ems => "101",
:ems_ref => "101",
:uid_ems => "900",
:ems_ref => "900",
:power_state => "never",
:raw_power_state => "never"
)
Expand Down
Loading