Commit 828c8d16 authored by Morten Knutsen's avatar Morten Knutsen

Rewrite to generate ranges based on an *array* of ranges as input, rather than a single range.

parent 55df0886
......@@ -11,7 +11,7 @@ from nova_router import setupLogfile, send_stat, parse_ip, format_ip, etcd_conne
APP = "nova_dhcp_configurator"
DESCRIPTION = "Generate dhcp server config based on neighbor list in etcd"
INPUT_RANGE_FILE = "/etc/dhcp/dhcp_range.json"
INPUT_RANGES_FILE = "/etc/dhcp/dhcp_ranges.json"
RANGES_FILE = "/etc/dhcp/generated_ranges.conf"
HOSTS_FILE = "/etc/dhcp/generated_hosts.conf"
......@@ -24,20 +24,20 @@ class Generator(object):
logging.info("Process starting")
dhcp_range = json.load(open(INPUT_RANGE_FILE))
self.range = [parse_ip(x) for x in dhcp_range]
dhcp_ranges = json.load(open(INPUT_RANGES_FILE))
self.ranges = [[parse_ip(x) for x in dhcp_range] for dhcp_range in dhcp_ranges]
def next_free(self, i, hosts):
while format_ip(i) in hosts and i <= self.range[1]:
def next_free(self, range, i, hosts):
while format_ip(i) in hosts and i <= range[1]:
i += 1
if i > self.range[1]:
if i > range[1]:
return None
return i
def next_used(self, i, hosts):
while format_ip(i) not in hosts and i <= self.range[1]:
def next_used(self, range, i, hosts):
while format_ip(i) not in hosts and i <= range[1]:
i += 1
if i > self.range[1]:
if i > range[1]:
return None
return i
......@@ -51,16 +51,17 @@ class Generator(object):
def generate_ranges(self, hosts):
result = ""
range_start = self.next_free(self.range[0], hosts)
while range_start:
next_host = self.next_used(range_start + 1, hosts)
if next_host is None:
range_end = range_start
else:
range_end = next_host - 1
result += "range {} {};\n".format(format_ip(range_start),
format_ip(range_end))
range_start = self.next_free(range_end + 1, hosts)
for r in self.ranges:
range_start = self.next_free(r, r[0], hosts)
while range_start:
next_host = self.next_used(r, range_start + 1, hosts)
if next_host is None:
range_end = range_start
else:
range_end = next_host - 1
result += "range {} {};\n".format(format_ip(range_start),
format_ip(range_end))
range_start = self.next_free(r, range_end + 1, hosts)
return result
def generate_hosts(self, hosts):
......@@ -86,11 +87,18 @@ class Generator(object):
pass
return hosts, index
def get_dynamic_hosts(self, hosts):
dyn_hosts = []
for r in self.ranges:
for host in hosts:
if parse_ip(host) >= r[0] and parse_ip(host) <= r[1]:
dyn_hosts.append(host)
return dyn_hosts
def generate_all(self):
hosts, index = self.get_hosts()
total_range = self.range[1] - self.range[0] + 1
dynamic_hosts = [host for host in hosts if
parse_ip(host) >= self.range[0] and parse_ip(host) <= self.range[1]]
total_range = sum(map(lambda x: x[1] - x[0] + 1, self.ranges))
dynamic_hosts = self.get_dynamic_hosts(hosts)
send_stat("instance_net.addresses.ipv4.used", len(hosts))
send_stat("instance_net.addresses.ipv4.free_dynamic", total_range - len(dynamic_hosts))
if total_range - len(dynamic_hosts) < 2:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment