Fixes #460 - Verify VMID existence before creating VM#461
Fixes #460 - Verify VMID existence before creating VM#461jakduch wants to merge 2 commits intotheforeman:masterfrom
Conversation
When creating a new VM, assign_vmid now checks whether the assigned VMID already belongs to an existing VM or container on any cluster node. If the VMID is taken, it requests a new one from Proxmox and retries up to 5 times. This prevents silent overwriting of unmanaged VMs when Proxmox's /cluster/nextid returns a VMID that is already in use due to race conditions or stale nextid state.
f207cd1 to
a98ddb7
Compare
|
Thanks for the contribution @jakduch . I tested it and if I create a proxmox host with vmid that already exists (say vmid of some container or another unmanaged proxmox vm), it creates foreman host but is unmanaged. Because it checks if vm exists in foreman and for existing vm it return true but still tries to create a vm ( using |
a98ddb7 to
5ee711b
Compare
|
Hi, thanks for testing! Yes, this is the intended behavior. The PR specifically protects the The scenario you describe (existing Proxmox VM being adopted as an unmanaged Foreman host via |
Move rescue Fog::Errors::NotFound inside the any? block so that a NotFound from one node does not short-circuit the search across remaining nodes. Update node mock factories to stub servers.get and containers.get returning nil, matching the vmid_exists? lookup during create_vm.
5ee711b to
8b70de5
Compare
Problem
When creating a new VM through Foreman, the
assign_vmidmethod blindly trusts the VMID returned by Proxmox's/cluster/nextidendpoint without verifying that no VM with that ID already exists in the cluster.This can lead to silent overwriting of existing VM configurations when:
/etc/pve/nextidfile is stale after manual VM creation/deletionThe existing
id_valid?check only validates the VMID range (100–999999999), not whether the VMID is actually free.Solution
Added a
vmid_exists?method that checks all cluster nodes for existing VMs and containers with the given VMID. Theassign_vmidmethod now:next_idor user input)This ensures Foreman never creates or clones a VM with a VMID that is already in use, regardless of whether the existing VM is managed by Foreman or not.
Changes
assign_vmid: Added existence check loop with retry logicvmid_exists?: New method that scans all cluster nodes for VMs and containers with the given VMIDTesting
next_id