feat: add ip module for automated free IP discovery in agent pool

This commit is contained in:
Fredrick Amnehagen 2026-02-05 19:24:04 +01:00
parent 064144134e
commit a6f61ad2ac
3 changed files with 58 additions and 7 deletions

View file

@ -55,4 +55,25 @@ class DNSManager:
def list(self):
hosts = self.exec_lxc(f"cat {self.hosts_file}").stdout
dns = self.exec_lxc(f"cat {self.dns_file}").stdout
return {"hosts": hosts, "dns": dns}
return {"hosts": hosts, "dns": dns}
def get_free_ips(self, range_start=1, range_end=254, subnet="10.32.70"):
"""Finds free IPs in the specified range by checking both static and dynamic leases"""
# 1. Get all static IPs from dhcp-hosts.conf and dynamic-hosts.conf
static_configs = self.exec_lxc(f"cat /etc/dnsmasq.d/dhcp-hosts.conf {self.hosts_file} 2>/dev/null").stdout
import re
used_ips = set(re.findall(r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', static_configs))
# 2. Get all active dynamic leases
leases = self.exec_lxc("cat /var/lib/misc/dnsmasq.leases 2>/dev/null").stdout
used_ips.update(set(re.findall(r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', leases)))
# 3. Find first available in the agent range
free_ips = []
for i in range(range_start, range_end + 1):
candidate = f"{subnet}.{i}"
if candidate not in used_ips:
free_ips.append(candidate)
if len(free_ips) >= 10: # Just return the top 10
break
return free_ips