~soren/nova/lp658257

« back to all changes in this revision

Viewing changes to nova/network/manager.py

  • Committer: Tarmac
  • Author(s): Vishvananda Ishaya
  • Date: 2010-10-01 01:28:17 UTC
  • mfrom: (276.6.12 network-lease-fix)
  • Revision ID: hudson@openstack.org-20101001012817-9uj531c4nzzsr33r
Adds support for periodic_tasks on manager that are regularly called by the service and recovers fixed_ips that didn't get disassociated properly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
Network Hosts are responsible for allocating ips and setting up network
21
21
"""
22
22
 
 
23
import datetime
23
24
import logging
24
25
import math
25
26
 
26
27
import IPy
 
28
from twisted.internet import defer
27
29
 
28
30
from nova import db
29
31
from nova import exception
62
64
flags.DEFINE_string('network_driver', 'nova.network.linux_net',
63
65
                    'Driver to use for network creation')
64
66
flags.DEFINE_bool('update_dhcp_on_disassociate', False,
65
 
                  'Whether to update dhcp when fixed_ip is disassocated')
 
67
                  'Whether to update dhcp when fixed_ip is disassociated')
 
68
flags.DEFINE_integer('fixed_ip_disassociate_timeout', 600,
 
69
                     'Seconds after which a deallocated ip is disassociated')
66
70
 
67
71
 
68
72
class AddressAlreadyAllocated(exception.Error):
219
223
class VlanManager(NetworkManager):
220
224
    """Vlan network with dhcp"""
221
225
 
 
226
    @defer.inlineCallbacks
 
227
    def periodic_tasks(self, context=None):
 
228
        """Tasks to be run at a periodic interval"""
 
229
        yield super(VlanManager, self).periodic_tasks(context)
 
230
        now = datetime.datetime.utcnow()
 
231
        timeout = FLAGS.fixed_ip_disassociate_timeout
 
232
        time = now - datetime.timedelta(seconds=timeout)
 
233
        num = self.db.fixed_ip_disassociate_all_by_timeout(self,
 
234
                                                           self.host,
 
235
                                                           time)
 
236
        if num:
 
237
            logging.debug("Dissassociated %s stale fixed ip(s)", num)
 
238
 
222
239
    def init_host(self):
223
240
        """Do any initialization that needs to be run if this is a
224
241
           standalone service.
242
259
        """Returns a fixed ip to the pool"""
243
260
        self.db.fixed_ip_update(context, address, {'allocated': False})
244
261
        fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address)
245
 
        if not fixed_ip_ref['leased']:
246
 
            self.db.fixed_ip_disassociate(context, address)
247
 
            # NOTE(vish): dhcp server isn't updated until next setup, this
248
 
            #             means there will stale entries in the conf file
249
 
            #             the code below will update the file if necessary
250
 
            if FLAGS.update_dhcp_on_disassociate:
251
 
                network_ref = self.db.fixed_ip_get_network(context, address)
252
 
                self.driver.update_dhcp(context, network_ref['id'])
253
262
 
254
263
 
255
264
    def setup_fixed_ip(self, context, address):
266
275
        """Called by dhcp-bridge when ip is leased"""
267
276
        logging.debug("Leasing IP %s", address)
268
277
        fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address)
269
 
        if not fixed_ip_ref['allocated']:
270
 
            logging.warn("IP %s leased that was already deallocated", address)
271
 
            return
272
278
        instance_ref = fixed_ip_ref['instance']
273
279
        if not instance_ref:
274
280
            raise exception.Error("IP %s leased that isn't associated" %
279
285
        self.db.fixed_ip_update(context,
280
286
                                fixed_ip_ref['address'],
281
287
                                {'leased': True})
 
288
        if not fixed_ip_ref['allocated']:
 
289
            logging.warn("IP %s leased that was already deallocated", address)
282
290
 
283
291
    def release_fixed_ip(self, context, mac, address):
284
292
        """Called by dhcp-bridge when ip is released"""
285
293
        logging.debug("Releasing IP %s", address)
286
294
        fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address)
287
 
        if not fixed_ip_ref['leased']:
288
 
            logging.warn("IP %s released that was not leased", address)
289
 
            return
290
295
        instance_ref = fixed_ip_ref['instance']
291
296
        if not instance_ref:
292
297
            raise exception.Error("IP %s released that isn't associated" %
294
299
        if instance_ref['mac_address'] != mac:
295
300
            raise exception.Error("IP %s released from bad mac %s vs %s" %
296
301
                                  (address, instance_ref['mac_address'], mac))
297
 
        self.db.fixed_ip_update(context, address, {'leased': False})
 
302
        if not fixed_ip_ref['leased']:
 
303
            logging.warn("IP %s released that was not leased", address)
 
304
        self.db.fixed_ip_update(context,
 
305
                                fixed_ip_ref['str_id'],
 
306
                                {'leased': False})
298
307
        if not fixed_ip_ref['allocated']:
299
308
            self.db.fixed_ip_disassociate(context, address)
300
309
            # NOTE(vish): dhcp server isn't updated until next setup, this