From 81fe66bb19d16c387705e144c9941096cc497cf0 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Mon, 7 Mar 2011 15:12:26 +0100 Subject: Make "dhcpbridge init" output correctly formatted leases information. --- bin/nova-dhcpbridge | 2 +- nova/network/linux_net.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/bin/nova-dhcpbridge b/bin/nova-dhcpbridge index 3dd9de367..7ef51feba 100755 --- a/bin/nova-dhcpbridge +++ b/bin/nova-dhcpbridge @@ -94,7 +94,7 @@ def init_leases(interface): """Get the list of hosts for an interface.""" ctxt = context.get_admin_context() network_ref = db.network_get_by_bridge(ctxt, interface) - return linux_net.get_dhcp_hosts(ctxt, network_ref['id']) + return linux_net.get_dhcp_leases(ctxt, network_ref['id']) def main(): diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 535ce87bc..0bcc36081 100644 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -18,6 +18,7 @@ Implements vlans, bridges, and iptables rules using linux utilities. """ import os +import time from nova import db from nova import exception @@ -56,6 +57,8 @@ flags.DEFINE_bool('use_nova_chains', False, 'use the nova_ routing chains instead of default') 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') @@ -273,8 +276,17 @@ def ensure_bridge(bridge, interface, net_attrs=None): _confirm_rule("FORWARD", "-j nova-local") +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): @@ -365,8 +377,19 @@ 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'] + timestamp = time.mktime(instance_ref['updated_at'].timetuple()) + + return "%d %s %s %s" % (timestamp + FLAGS.dhcp_lease_time, + instance_ref['mac_address'], + instance_ref['hostname'], + fixed_ip_ref['address']) + + 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'], @@ -420,7 +443,8 @@ def _dnsmasq_cmd(net): ' --pid-file=%s' % _dhcp_file(net['bridge'], 'pid'), ' --listen-address=%s' % net['gateway'], ' --except-interface=lo', - ' --dhcp-range=%s,static,120s' % net['dhcp_start'], + ' --dhcp-range=%s,static,%ds' % (net['dhcp_start'], + FLAGS.dhcp_lease_time), ' --dhcp-hostsfile=%s' % _dhcp_file(net['bridge'], 'conf'), ' --dhcp-script=%s' % FLAGS.dhcpbridge, ' --leasefile-ro'] -- cgit From c8fc7ed48be84e3b39ab88c8c103fbe52b6718e1 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Mon, 14 Mar 2011 14:06:10 +0100 Subject: Add a unit test --- nova/network/linux_net.py | 17 +++++++++++------ nova/tests/test_network.py | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 0bcc36081..f55662a7a 100644 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -18,7 +18,7 @@ Implements vlans, bridges, and iptables rules using linux utilities. """ import os -import time +import calendar from nova import db from nova import exception @@ -380,12 +380,17 @@ interface %s def _host_lease(fixed_ip_ref): """Return a host string for an address in leasefile format""" instance_ref = fixed_ip_ref['instance'] - timestamp = time.mktime(instance_ref['updated_at'].timetuple()) + 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" % (timestamp + FLAGS.dhcp_lease_time, - instance_ref['mac_address'], - instance_ref['hostname'], - fixed_ip_ref['address']) + 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): diff --git a/nova/tests/test_network.py b/nova/tests/test_network.py index ce1c77210..b7a76be85 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 @@ -29,6 +30,7 @@ from nova import log as logging from nova import test from nova import utils from nova.auth import manager +from nova.network import linux_net FLAGS = flags.FLAGS LOG = logging.getLogger('nova.tests.network') @@ -321,6 +323,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""" -- cgit