summaryrefslogtreecommitdiffstats
path: root/bin/nova-dhcpbridge
blob: 66f6ba6a33b5b613d8a910b8554bbbedd7cfe6c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Handle lease database updates from DHCP servers.
"""

import gettext
import os
import sys

# If ../nova/__init__.py exists, add ../ to Python search path, so that
# it will override what happens to be installed in /usr/(local/)lib/python...
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
                                   os.pardir,
                                   os.pardir))
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
    sys.path.insert(0, possible_topdir)

gettext.install('nova', unicode=1)

from nova import config
from nova import context
from nova import db
from nova.network import linux_net
from nova.network import rpcapi as network_rpcapi
from nova.openstack.common import cfg
from nova.openstack.common import importutils
from nova.openstack.common import log as logging
from nova.openstack.common import rpc
from nova import utils

CONF = cfg.CONF
CONF.import_opt('host', 'nova.config')
CONF.import_opt('network_manager', 'nova.config')
LOG = logging.getLogger('nova.dhcpbridge')


def add_lease(mac, ip_address):
    """Set the IP that was assigned by the DHCP server."""
    if CONF.fake_rabbit:
        LOG.debug(_("leasing ip"))
        network_manager = importutils.import_object(CONF.network_manager)
        network_manager.lease_fixed_ip(context.get_admin_context(),
                                       ip_address)
    else:
        api = network_rpcapi.NetworkAPI()
        api.lease_fixed_ip(context.get_admin_context(), ip_address, CONF.host)


def old_lease(mac, ip_address):
    """Called when an old lease is recognized."""
    # NOTE(vish): We assume we heard about this lease the first time.
    #             If not, we will get it the next time the lease is
    #             renewed.
    pass


def del_lease(mac, ip_address):
    """Called when a lease expires."""
    if CONF.fake_rabbit:
        LOG.debug(_("releasing ip"))
        network_manager = importutils.import_object(CONF.network_manager)
        network_manager.release_fixed_ip(context.get_admin_context(),
                                         ip_address)
    else:
        api = network_rpcapi.NetworkAPI()
        api.release_fixed_ip(context.get_admin_context(), ip_address,
                             CONF.host)


def init_leases(network_id):
    """Get the list of hosts for a network."""
    ctxt = context.get_admin_context()
    network_ref = db.network_get(ctxt, network_id)
    network_manager = importutils.import_object(CONF.network_manager)
    return network_manager.get_dhcp_leases(ctxt, network_ref)


def add_action_parsers(subparsers):
    parser = subparsers.add_parser('init')

    for action in ['add', 'del', 'old']:
        parser = subparsers.add_parser(action)
        parser.add_argument('mac')
        parser.add_argument('ip')
        parser.set_defaults(func=globals()[action + '_lease'])


CONF.register_cli_opt(
    cfg.SubCommandOpt('action',
                      title='Action options',
                      help='Available dhcpbridge options',
                      handler=add_action_parsers))


def main():
    """Parse environment and arguments and call the approproate action."""
    try:
        config_file = os.environ['CONFIG_FILE']
    except KeyError:
        config_file = os.environ['FLAGFILE']

    config.parse_args(sys.argv, default_config_files=[config_file])

    logging.setup("nova")

    if int(os.environ.get('TESTING', '0')):
        from nova.tests import conf_fixture

    if CONF.action.name in ['add', 'del', 'old']:
        msg = (_("Called '%(action)s' for mac '%(mac)s' with ip '%(ip)s'") %
               {"action": CONF.action.name,
                "mac": CONF.action.mac,
                "ip": CONF.action.ip})
        LOG.debug(msg)
        CONF.action.func(CONF.action.mac, CONF.action.ip)
    else:
        try:
            network_id = int(os.environ.get('NETWORK_ID'))
        except TypeError:
            LOG.error(_("Environment variable 'NETWORK_ID' must be set."))
            sys.exit(1)

        print init_leases(network_id)

    rpc.cleanup()


if __name__ == "__main__":
    main()