~justin-fathomdb/nova/bug744150

116.9.7 by Vishvananda Ishaya
whitespace fixes and header changes
1
#!/usr/bin/env python
2
# vim: tabstop=4 shiftwidth=4 softtabstop=4
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
3
116.9.7 by Vishvananda Ishaya
whitespace fixes and header changes
4
# Copyright 2010 United States Government as represented by the
5
# Administrator of the National Aeronautics and Space Administration.
6
# All Rights Reserved.
7
#
8
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
9
#    not use this file except in compliance with the License. You may obtain
10
#    a copy of the License at
11
#
12
#         http://www.apache.org/licenses/LICENSE-2.0
13
#
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
14
#    Unless required by applicable law or agreed to in writing, software
116.9.7 by Vishvananda Ishaya
whitespace fixes and header changes
15
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
#    License for the specific language governing permissions and limitations
18
#    under the License.
19
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
20
"""
21
Handle lease database updates from DHCP servers.
22
"""
23
140.6.1 by Vishvananda Ishaya
Fixes to dhcp lease code to use a flagfile
24
import logging
25
import os
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
26
import sys
140.6.1 by Vishvananda Ishaya
Fixes to dhcp lease code to use a flagfile
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
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
31
sys.path.append(os.path.abspath(os.path.join(__file__, "../../")))
32
140.6.2 by Vishvananda Ishaya
merged in trunk and fixed import merge errors
33
from nova import flags
139.2.2 by Jesse Andrews
reorder imports spacing
34
from nova import rpc
140.6.1 by Vishvananda Ishaya
Fixes to dhcp lease code to use a flagfile
35
from nova import utils
198.4.1 by Vishvananda Ishaya
Huge network refactor, Round I
36
from nova.network import linux_net
37
from nova.network import model
38
from nova.network import service
140.6.2 by Vishvananda Ishaya
merged in trunk and fixed import merge errors
39
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
40
FLAGS = flags.FLAGS
41
42
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
43
def add_lease(_mac, ip, _hostname, _interface):
44
    """Set the IP that was assigned by the DHCP server."""
116.9.2 by Joshua McKenty
Got dhcpleasor working, with test ENV for testing, and rpc.cast for real world.
45
    if FLAGS.fake_rabbit:
198.4.1 by Vishvananda Ishaya
Huge network refactor, Round I
46
        service.VlanNetworkService().lease_ip(ip)
116.9.2 by Joshua McKenty
Got dhcpleasor working, with test ENV for testing, and rpc.cast for real world.
47
    else:
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
48
        rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.node_name),
198.4.1 by Vishvananda Ishaya
Huge network refactor, Round I
49
                 {"method": "lease_ip",
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
50
                  "args": {"fixed_ip": ip}})
51
52
53
def old_lease(_mac, _ip, _hostname, _interface):
54
    """Do nothing, just an old lease update."""
116.9.2 by Joshua McKenty
Got dhcpleasor working, with test ENV for testing, and rpc.cast for real world.
55
    logging.debug("Adopted old lease or got a change of mac/hostname")
116.9.7 by Vishvananda Ishaya
whitespace fixes and header changes
56
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
57
58
def del_lease(_mac, ip, _hostname, _interface):
59
    """Remove the leased IP from the databases."""
116.9.2 by Joshua McKenty
Got dhcpleasor working, with test ENV for testing, and rpc.cast for real world.
60
    if FLAGS.fake_rabbit:
198.4.1 by Vishvananda Ishaya
Huge network refactor, Round I
61
        service.VlanNetworkService().release_ip(ip)
116.9.2 by Joshua McKenty
Got dhcpleasor working, with test ENV for testing, and rpc.cast for real world.
62
    else:
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
63
        rpc.cast("%s.%s" % (FLAGS.network_topic, FLAGS.node_name),
198.4.1 by Vishvananda Ishaya
Huge network refactor, Round I
64
                 {"method": "release_ip",
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
65
                  "args": {"fixed_ip": ip}})
66
116.9.7 by Vishvananda Ishaya
whitespace fixes and header changes
67
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
68
def init_leases(interface):
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
69
    """Get the list of hosts for an interface."""
198.4.1 by Vishvananda Ishaya
Huge network refactor, Round I
70
    net = model.get_network_by_interface(interface)
116.9.2 by Joshua McKenty
Got dhcpleasor working, with test ENV for testing, and rpc.cast for real world.
71
    res = ""
72
    for host_name in net.hosts:
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
73
        res += "%s\n" % linux_net.hostDHCP(net, host_name,
74
                                           net.hosts[host_name])
116.9.2 by Joshua McKenty
Got dhcpleasor working, with test ENV for testing, and rpc.cast for real world.
75
    return res
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
76
77
140.6.1 by Vishvananda Ishaya
Fixes to dhcp lease code to use a flagfile
78
def main():
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
79
    """Parse environment and arguments and call the approproate action."""
140.6.1 by Vishvananda Ishaya
Fixes to dhcp lease code to use a flagfile
80
    flagfile = os.environ.get('FLAGFILE', FLAGS.dhcpbridge_flagfile)
81
    utils.default_flagfile(flagfile)
82
    argv = FLAGS(sys.argv)
83
    interface = os.environ.get('DNSMASQ_INTERFACE', 'br0')
84
    if int(os.environ.get('TESTING', '0')):
116.9.2 by Joshua McKenty
Got dhcpleasor working, with test ENV for testing, and rpc.cast for real world.
85
        FLAGS.fake_rabbit = True
86
        FLAGS.redis_db = 8
87
        FLAGS.network_size = 32
145.1.2 by Ewan Mellor
In preparation for XenAPI support, refactor the interface between
88
        FLAGS.connection_type = 'fake'
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
89
        FLAGS.fake_network = True
90
        FLAGS.auth_driver = 'nova.auth.ldapdriver.FakeLdapDriver'
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
91
    action = argv[1]
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
92
    if action in ['add', 'del', 'old']:
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
93
        mac = argv[2]
94
        ip = argv[3]
95
        hostname = argv[4]
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
96
        logging.debug("Called %s for mac %s with ip %s and "
97
                      "hostname %s on interface %s",
98
                      action, mac, ip, hostname, interface)
99
        globals()[action + '_lease'](mac, ip, hostname, interface)
116.9.1 by Joshua McKenty
Capture signals from dnsmasq and use them to update network state.
100
    else:
101
        print init_leases(interface)
102
103
if __name__ == "__main__":
206.3.3 by Eric Day
Cleaned up pep8/pylint for bin/* files. I did not fix rsapi since this is already cleaned up in another branch.
104
    main()