summaryrefslogtreecommitdiffstats
path: root/nova
diff options
context:
space:
mode:
authorSoren Hansen <soren@linux2go.dk>2011-03-14 15:45:15 +0000
committerTarmac <>2011-03-14 15:45:15 +0000
commit7fde254ec53aeb88301e5592853961b2b9c87ef4 (patch)
tree867af4ed8bb57d6dfbfae7e2fb06775f0f964605 /nova
parentab982a009f2ab608a1acbe2d5cc2d0fb6b488b53 (diff)
parent04838ee14d0ac6df0052fba465cdf7f765c66ae5 (diff)
downloadnova-7fde254ec53aeb88301e5592853961b2b9c87ef4.tar.gz
nova-7fde254ec53aeb88301e5592853961b2b9c87ef4.tar.xz
nova-7fde254ec53aeb88301e5592853961b2b9c87ef4.zip
Make nova-dhcpbridge output lease information in dnsmasq's leasesfile format.
Diffstat (limited to 'nova')
-rw-r--r--nova/network/linux_net.py32
-rw-r--r--nova/tests/test_network.py26
2 files changed, 56 insertions, 2 deletions
diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py
index e69ed2f75..7106e6164 100644
--- a/nova/network/linux_net.py
+++ b/nova/network/linux_net.py
@@ -19,6 +19,7 @@ Implements vlans, bridges, and iptables rules using linux utilities.
import inspect
import os
+import calendar
from eventlet import semaphore
@@ -56,6 +57,8 @@ flags.DEFINE_string('routing_source_ip', '$my_ip',
'Public IP of network host')
flags.DEFINE_string('input_chain', 'INPUT',
'chain to add nova_input to')
+flags.DEFINE_integer('dhcp_lease_time', 120,
+ 'Lifetime of a DHCP lease')
flags.DEFINE_string('dns_server', None,
'if set, uses specific dns server for dnsmasq')
@@ -533,8 +536,17 @@ def ensure_bridge(bridge, interface, net_attrs=None):
bridge)
+def get_dhcp_leases(context, network_id):
+ """Return a network's hosts config in dnsmasq leasefile format"""
+ hosts = []
+ for fixed_ip_ref in db.network_get_associated_fixed_ips(context,
+ network_id):
+ hosts.append(_host_lease(fixed_ip_ref))
+ return '\n'.join(hosts)
+
+
def get_dhcp_hosts(context, network_id):
- """Get a string containing a network's hosts config in dnsmasq format"""
+ """Get a string containing a network's hosts config in dhcp-host format"""
hosts = []
for fixed_ip_ref in db.network_get_associated_fixed_ips(context,
network_id):
@@ -625,8 +637,24 @@ interface %s
utils.get_my_linklocal(network_ref['bridge'])})
+def _host_lease(fixed_ip_ref):
+ """Return a host string for an address in leasefile format"""
+ instance_ref = fixed_ip_ref['instance']
+ if instance_ref['updated_at']:
+ timestamp = instance_ref['updated_at']
+ else:
+ timestamp = instance_ref['created_at']
+
+ seconds_since_epoch = calendar.timegm(timestamp.utctimetuple())
+
+ return "%d %s %s %s *" % (seconds_since_epoch + FLAGS.dhcp_lease_time,
+ instance_ref['mac_address'],
+ fixed_ip_ref['address'],
+ instance_ref['hostname'] or '*')
+
+
def _host_dhcp(fixed_ip_ref):
- """Return a host string for an address"""
+ """Return a host string for an address in dhcp-host format"""
instance_ref = fixed_ip_ref['instance']
return "%s,%s.%s,%s" % (instance_ref['mac_address'],
instance_ref['hostname'],
diff --git a/nova/tests/test_network.py b/nova/tests/test_network.py
index 53e35ce7e..1e634b388 100644
--- a/nova/tests/test_network.py
+++ b/nova/tests/test_network.py
@@ -20,6 +20,7 @@ Unit Tests for network code
"""
import IPy
import os
+import time
from nova import context
from nova import db
@@ -463,6 +464,31 @@ class NetworkTestCase(test.TestCase):
network['id'])
self.assertEqual(ip_count, num_available_ips)
+ def test_dhcp_lease_output(self):
+ admin_ctxt = context.get_admin_context()
+ address = self._create_address(0, self.instance_id)
+ lease_ip(address)
+ network_ref = db.network_get_by_instance(admin_ctxt, self.instance_id)
+ leases = linux_net.get_dhcp_leases(context.get_admin_context(),
+ network_ref['id'])
+ for line in leases.split('\n'):
+ seconds, mac, ip, hostname, client_id = line.split(' ')
+ self.assertTrue(int(seconds) > time.time(), 'Lease expires in '
+ 'the past')
+ octets = mac.split(':')
+ self.assertEqual(len(octets), 6, "Wrong number of octets "
+ "in %s" % (max,))
+ for octet in octets:
+ self.assertEqual(len(octet), 2, "Oddly sized octet: %s"
+ % (octet,))
+ # This will throw an exception if the octet is invalid
+ int(octet, 16)
+
+ # And this will raise an exception in case of an invalid IP
+ IPy.IP(ip)
+
+ release_ip(address)
+
def is_allocated_in_project(address, project_id):
"""Returns true if address is in specified project"""