~ewanmellor/nova/xenapi

« back to all changes in this revision

Viewing changes to bin/nova-dhcpbridge

  • Committer: Ewan Mellor
  • Date: 2010-09-28 18:12:13 UTC
  • mfrom: (145.13.135 trunk)
  • Revision ID: ewan.mellor@citrix.com-20100928181213-rnit65ejj20hjvjo
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import os
26
26
import sys
27
27
 
28
 
#TODO(joshua): there is concern that the user dnsmasq runs under will not
29
 
#              have nova in the path. This should be verified and if it is
30
 
#              not true the ugly line below can be removed
31
 
sys.path.append(os.path.abspath(os.path.join(__file__, "../../")))
 
28
# If ../nova/__init__.py exists, add ../ to Python search path, so that
 
29
# it will override what happens to be installed in /usr/(local/)lib/python...
 
30
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
 
31
                                   os.pardir,
 
32
                                   os.pardir))
 
33
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
 
34
    sys.path.insert(0, possible_topdir)
32
35
 
 
36
from nova import db
33
37
from nova import flags
34
38
from nova import rpc
35
39
from nova import utils
36
40
from nova.network import linux_net
37
 
from nova.network import model
38
 
from nova.network import service
39
41
 
40
42
FLAGS = flags.FLAGS
41
 
 
42
 
 
43
 
def add_lease(_mac, ip_address, _hostname, _interface):
 
43
flags.DECLARE('auth_driver', 'nova.auth.manager')
 
44
flags.DECLARE('redis_db', 'nova.datastore')
 
45
flags.DECLARE('network_size', 'nova.network.manager')
 
46
flags.DECLARE('num_networks', 'nova.network.manager')
 
47
flags.DECLARE('update_dhcp_on_disassociate', 'nova.network.manager')
 
48
 
 
49
 
 
50
def add_lease(mac, ip_address, _hostname, _interface):
44
51
    """Set the IP that was assigned by the DHCP server."""
45
52
    if FLAGS.fake_rabbit:
46
 
        service.VlanNetworkService().lease_ip(ip_address)
 
53
        logging.debug("leasing ip")
 
54
        network_manager = utils.import_object(FLAGS.network_manager)
 
55
        network_manager.lease_fixed_ip(None, mac, ip_address)
47
56
    else:
48
 
        rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.node_name),
49
 
                 {"method": "lease_ip",
50
 
                  "args": {"fixed_ip": ip_address}})
 
57
        rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.host),
 
58
                 {"method": "lease_fixed_ip",
 
59
                  "args": {"context": None,
 
60
                           "mac": mac,
 
61
                           "address": ip_address}})
51
62
 
52
63
 
53
64
def old_lease(_mac, _ip_address, _hostname, _interface):
55
66
    logging.debug("Adopted old lease or got a change of mac/hostname")
56
67
 
57
68
 
58
 
def del_lease(_mac, ip_address, _hostname, _interface):
 
69
def del_lease(mac, ip_address, _hostname, _interface):
59
70
    """Called when a lease expires."""
60
71
    if FLAGS.fake_rabbit:
61
 
        service.VlanNetworkService().release_ip(ip_address)
 
72
        logging.debug("releasing ip")
 
73
        network_manager = utils.import_object(FLAGS.network_manager)
 
74
        network_manager.release_fixed_ip(None, mac, ip_address)
62
75
    else:
63
 
        rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.node_name),
64
 
                 {"method": "release_ip",
65
 
                  "args": {"fixed_ip": ip_address}})
 
76
        rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.host),
 
77
                 {"method": "release_fixed_ip",
 
78
                  "args": {"context": None,
 
79
                           "mac": mac,
 
80
                           "address": ip_address}})
66
81
 
67
82
 
68
83
def init_leases(interface):
69
84
    """Get the list of hosts for an interface."""
70
 
    net = model.get_network_by_interface(interface)
71
 
    res = ""
72
 
    for address in net.assigned_objs:
73
 
        res += "%s\n" % linux_net.host_dhcp(address)
74
 
    return res
 
85
    network_ref = db.network_get_by_bridge(None, interface)
 
86
    return linux_net.get_dhcp_hosts(None, network_ref['id'])
75
87
 
76
88
 
77
89
def main():
83
95
    if int(os.environ.get('TESTING', '0')):
84
96
        FLAGS.fake_rabbit = True
85
97
        FLAGS.redis_db = 8
86
 
        FLAGS.network_size = 32
 
98
        FLAGS.network_size = 16
87
99
        FLAGS.connection_type = 'fake'
88
100
        FLAGS.fake_network = True
89
101
        FLAGS.auth_driver = 'nova.auth.ldapdriver.FakeLdapDriver'
 
102
        FLAGS.num_networks = 5
 
103
        path = os.path.abspath(os.path.join(os.path.dirname(__file__),
 
104
                                            '..',
 
105
                                            '_trial_temp',
 
106
                                            'nova.sqlite'))
 
107
        FLAGS.sql_connection = 'sqlite:///%s' % path
90
108
    action = argv[1]
91
109
    if action in ['add', 'del', 'old']:
92
110
        mac = argv[2]