fix: Apply IPAM IP address to VM cloud-init configuration

- Fix field names in frontend: ipam_ip -> ip_address, ipam_gateway -> gateway
- Pass ipam_network_id to deploy API for proper network mask lookup
- Extract network mask from IPAM network CIDR (e.g., /24 from 10.10.10.0/24)
- Auto-fetch gateway from IPAM network if not provided
- Add detailed logging for VM IP configuration
- Remove hardcoded /24 mask, use actual network mask from IPAM

This fixes the issue where VMs were getting DHCP instead of the
static IP allocated from IPAM.
This commit is contained in:
2025-12-15 20:30:17 +05:00
parent 379afd0353
commit cf456faa16
2 changed files with 35 additions and 7 deletions

View File

@@ -754,15 +754,43 @@ async def deploy_vm_from_template(
if error:
logger.warning(f"IPAM registration failed for manually specified IP: {error}")
# Get network mask (CIDR) and gateway from IPAM network if IP is specified
network_mask = "24" # Default mask
gateway = deploy_data.gateway
# Try to get proper network mask and gateway from IPAM
if allocated_ip and (deploy_data.ipam_network_id or ipam_allocation):
try:
from ..models import IPAMNetwork
network_id = deploy_data.ipam_network_id
if ipam_allocation and hasattr(ipam_allocation, 'network_id'):
network_id = ipam_allocation.network_id
if network_id:
network = db.query(IPAMNetwork).filter(IPAMNetwork.id == network_id).first()
if network:
# Extract mask from CIDR (e.g., "10.10.10.0/24" -> "24")
if network.network and '/' in network.network:
network_mask = network.network.split('/')[1]
logger.info(f"Using network mask /{network_mask} from IPAM network {network.name}")
# Get gateway if not already specified
if not gateway and network.gateway:
gateway = network.gateway
logger.info(f"Using gateway {gateway} from IPAM network {network.name}")
except Exception as e:
logger.warning(f"Could not get network info from IPAM: {e}")
# Configure VM
ip_config = None
if allocated_ip:
if deploy_data.gateway:
ip_config = f"ip={allocated_ip}/24,gw={deploy_data.gateway}"
logger.info(f"Configuring VM {new_vmid} with IP {allocated_ip}/{network_mask}, gateway: {gateway}")
if gateway:
ip_config = f"ip={allocated_ip}/{network_mask},gw={gateway}"
else:
ip_config = f"ip={allocated_ip}/24"
elif allocated_ip is None:
ip_config = f"ip={allocated_ip}/{network_mask}"
else:
ip_config = "ip=dhcp"
logger.info(f"Configuring VM {new_vmid} with DHCP")
# Combine user's SSH key with deploy request SSH keys
ssh_keys = deploy_data.ssh_keys or ""

View File

@@ -2614,9 +2614,9 @@ async function executeCreateVM(event) {
start_after_create: document.getElementById('vmStartAfterCreate').checked,
onboot: document.getElementById('vmOnboot').checked,
enable_ha: document.getElementById('vmEnableHA').checked,
ipam_ip: allocatedIP,
ipam_gateway: allocatedGateway,
ipam_allocation_id: allocationId
ip_address: allocatedIP,
gateway: allocatedGateway,
ipam_network_id: ipamNetworkId ? parseInt(ipamNetworkId) : null
};
try {