Merge "populate dnsmasq lease db with valid leases"

This commit is contained in:
Jenkins 2013-02-02 14:09:28 +00:00 committed by Gerrit Code Review
commit 7a378c212b
3 changed files with 60 additions and 10 deletions

View File

@ -2212,7 +2212,9 @@ def network_get_associated_fixed_ips(context, network_id, host=None):
models.VirtualInterface.address, models.VirtualInterface.address,
models.Instance.hostname, models.Instance.hostname,
models.Instance.updated_at, models.Instance.updated_at,
models.Instance.created_at).\ models.Instance.created_at,
models.FixedIp.allocated,
models.FixedIp.leased).\
filter(models.FixedIp.deleted == 0).\ filter(models.FixedIp.deleted == 0).\
filter(models.FixedIp.network_id == network_id).\ filter(models.FixedIp.network_id == network_id).\
filter(models.FixedIp.allocated == True).\ filter(models.FixedIp.allocated == True).\
@ -2234,6 +2236,8 @@ def network_get_associated_fixed_ips(context, network_id, host=None):
cleaned['instance_hostname'] = datum[5] cleaned['instance_hostname'] = datum[5]
cleaned['instance_updated'] = datum[6] cleaned['instance_updated'] = datum[6]
cleaned['instance_created'] = datum[7] cleaned['instance_created'] = datum[7]
cleaned['allocated'] = datum[8]
cleaned['leased'] = datum[9]
data.append(cleaned) data.append(cleaned)
return data return data

View File

@ -31,6 +31,7 @@ from nova.openstack.common import fileutils
from nova.openstack.common import importutils from nova.openstack.common import importutils
from nova.openstack.common import lockutils from nova.openstack.common import lockutils
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
from nova.openstack.common import timeutils
from nova import paths from nova import paths
from nova import utils from nova import utils
@ -755,7 +756,11 @@ def get_dhcp_leases(context, network_ref):
for data in db.network_get_associated_fixed_ips(context, for data in db.network_get_associated_fixed_ips(context,
network_ref['id'], network_ref['id'],
host=host): host=host):
hosts.append(_host_lease(data)) # NOTE(cfb): Don't return a lease entry if the IP isn't
# already leased
if data['allocated'] and data['leased']:
hosts.append(_host_lease(data))
return '\n'.join(hosts) return '\n'.join(hosts)
@ -1008,13 +1013,8 @@ interface %s
def _host_lease(data): def _host_lease(data):
"""Return a host string for an address in leasefile format.""" """Return a host string for an address in leasefile format."""
if data['instance_updated']: timestamp = timeutils.utcnow()
timestamp = data['instance_updated']
else:
timestamp = data['instance_created']
seconds_since_epoch = calendar.timegm(timestamp.utctimetuple()) seconds_since_epoch = calendar.timegm(timestamp.utctimetuple())
return '%d %s %s %s *' % (seconds_since_epoch + CONF.dhcp_lease_time, return '%d %s %s %s *' % (seconds_since_epoch + CONF.dhcp_lease_time,
data['vif_address'], data['vif_address'],
data['address'], data['address'],

View File

@ -15,6 +15,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import calendar
import os import os
import mox import mox
@ -25,6 +26,7 @@ from nova.network import driver
from nova.network import linux_net from nova.network import linux_net
from nova.openstack.common import fileutils from nova.openstack.common import fileutils
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
from nova.openstack.common import timeutils
from nova import test from nova import test
from nova import utils from nova import utils
@ -107,6 +109,7 @@ fixed_ips = [{'id': 0,
'address': '192.168.0.100', 'address': '192.168.0.100',
'instance_id': 0, 'instance_id': 0,
'allocated': True, 'allocated': True,
'leased': True,
'virtual_interface_id': 0, 'virtual_interface_id': 0,
'instance_uuid': '00000000-0000-0000-0000-0000000000000000', 'instance_uuid': '00000000-0000-0000-0000-0000000000000000',
'floating_ips': []}, 'floating_ips': []},
@ -115,6 +118,7 @@ fixed_ips = [{'id': 0,
'address': '192.168.1.100', 'address': '192.168.1.100',
'instance_id': 0, 'instance_id': 0,
'allocated': True, 'allocated': True,
'leased': True,
'virtual_interface_id': 1, 'virtual_interface_id': 1,
'instance_uuid': '00000000-0000-0000-0000-0000000000000000', 'instance_uuid': '00000000-0000-0000-0000-0000000000000000',
'floating_ips': []}, 'floating_ips': []},
@ -123,6 +127,7 @@ fixed_ips = [{'id': 0,
'address': '192.168.0.101', 'address': '192.168.0.101',
'instance_id': 1, 'instance_id': 1,
'allocated': True, 'allocated': True,
'leased': True,
'virtual_interface_id': 2, 'virtual_interface_id': 2,
'instance_uuid': '00000000-0000-0000-0000-0000000000000001', 'instance_uuid': '00000000-0000-0000-0000-0000000000000001',
'floating_ips': []}, 'floating_ips': []},
@ -131,6 +136,7 @@ fixed_ips = [{'id': 0,
'address': '192.168.1.101', 'address': '192.168.1.101',
'instance_id': 1, 'instance_id': 1,
'allocated': True, 'allocated': True,
'leased': True,
'virtual_interface_id': 3, 'virtual_interface_id': 3,
'instance_uuid': '00000000-0000-0000-0000-0000000000000001', 'instance_uuid': '00000000-0000-0000-0000-0000000000000001',
'floating_ips': []}, 'floating_ips': []},
@ -139,6 +145,7 @@ fixed_ips = [{'id': 0,
'address': '192.168.0.102', 'address': '192.168.0.102',
'instance_id': 0, 'instance_id': 0,
'allocated': True, 'allocated': True,
'leased': False,
'virtual_interface_id': 4, 'virtual_interface_id': 4,
'instance_uuid': '00000000-0000-0000-0000-0000000000000000', 'instance_uuid': '00000000-0000-0000-0000-0000000000000000',
'floating_ips': []}, 'floating_ips': []},
@ -147,6 +154,7 @@ fixed_ips = [{'id': 0,
'address': '192.168.1.102', 'address': '192.168.1.102',
'instance_id': 1, 'instance_id': 1,
'allocated': True, 'allocated': True,
'leased': False,
'virtual_interface_id': 5, 'virtual_interface_id': 5,
'instance_uuid': '00000000-0000-0000-0000-0000000000000001', 'instance_uuid': '00000000-0000-0000-0000-0000000000000001',
'floating_ips': []}] 'floating_ips': []}]
@ -184,7 +192,7 @@ vifs = [{'id': 0,
'instance_uuid': '00000000-0000-0000-0000-0000000000000001'}] 'instance_uuid': '00000000-0000-0000-0000-0000000000000001'}]
def get_associated(context, network_id, host=None): def get_associated(context, network_id, host=None, address=None):
result = [] result = []
for datum in fixed_ips: for datum in fixed_ips:
if (datum['network_id'] == network_id and datum['allocated'] if (datum['network_id'] == network_id and datum['allocated']
@ -193,6 +201,8 @@ def get_associated(context, network_id, host=None):
instance = instances[datum['instance_uuid']] instance = instances[datum['instance_uuid']]
if host and host != instance['host']: if host and host != instance['host']:
continue continue
if address and address != datum['address']:
continue
cleaned = {} cleaned = {}
cleaned['address'] = datum['address'] cleaned['address'] = datum['address']
cleaned['instance_uuid'] = datum['instance_uuid'] cleaned['instance_uuid'] = datum['instance_uuid']
@ -203,6 +213,8 @@ def get_associated(context, network_id, host=None):
cleaned['instance_hostname'] = instance['hostname'] cleaned['instance_hostname'] = instance['hostname']
cleaned['instance_updated'] = instance['updated_at'] cleaned['instance_updated'] = instance['updated_at']
cleaned['instance_created'] = instance['created_at'] cleaned['instance_created'] = instance['created_at']
cleaned['allocated'] = datum['allocated']
cleaned['leased'] = datum['leased']
result.append(cleaned) result.append(cleaned)
return result return result
@ -299,7 +311,6 @@ class LinuxNetworkTestCase(test.TestCase):
"192.168.1.102,net:NW-5" "192.168.1.102,net:NW-5"
) )
actual_hosts = self.driver.get_dhcp_hosts(self.context, networks[1]) actual_hosts = self.driver.get_dhcp_hosts(self.context, networks[1])
self.assertEquals(actual_hosts, expected) self.assertEquals(actual_hosts, expected)
def test_get_dns_hosts_for_nw00(self): def test_get_dns_hosts_for_nw00(self):
@ -333,6 +344,41 @@ class LinuxNetworkTestCase(test.TestCase):
self.assertEquals(actual_opts, expected_opts) self.assertEquals(actual_opts, expected_opts)
def test_get_dhcp_leases_for_nw00(self):
timestamp = timeutils.utcnow()
seconds_since_epoch = calendar.timegm(timestamp.utctimetuple())
leases = self.driver.get_dhcp_leases(self.context, networks[0])
leases = leases.split('\n')
for lease in leases:
lease = lease.split(' ')
data = get_associated(self.context, 0, address=lease[2])[0]
self.assertTrue(data['allocated'])
self.assertTrue(data['leased'])
self.assertTrue(lease[0] > seconds_since_epoch)
self.assertTrue(lease[1] == data['vif_address'])
self.assertTrue(lease[2] == data['address'])
self.assertTrue(lease[3] == data['instance_hostname'])
self.assertTrue(lease[4] == '*')
def test_get_dhcp_leases_for_nw01(self):
self.flags(host='fake_instance01')
timestamp = timeutils.utcnow()
seconds_since_epoch = calendar.timegm(timestamp.utctimetuple())
leases = self.driver.get_dhcp_leases(self.context, networks[1])
leases = leases.split('\n')
for lease in leases:
lease = lease.split(' ')
data = get_associated(self.context, 1, address=lease[2])[0]
self.assertTrue(data['allocated'])
self.assertTrue(data['leased'])
self.assertTrue(lease[0] > seconds_since_epoch)
self.assertTrue(lease[1] == data['vif_address'])
self.assertTrue(lease[2] == data['address'])
self.assertTrue(lease[3] == data['instance_hostname'])
self.assertTrue(lease[4] == '*')
def test_dhcp_opts_not_default_gateway_network(self): def test_dhcp_opts_not_default_gateway_network(self):
expected = "NW-0,3" expected = "NW-0,3"
data = get_associated(self.context, 0)[0] data = get_associated(self.context, 0)[0]