Files
aca07a42fabb7f506cf132b995b4ad0139987b02
nova /bin /nova-dhcpbridge

127 lines
4.3 KiB
Plaintext
Raw Normal View History

2010年07月07日 12:25:22 -07:00
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2010年07月07日 12:25:22 -07:00
# 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
2010年07月07日 12:25:22 -07:00
# 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.
"""
2010年07月15日 23:11:33 +00:00
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)
2010年08月24日 01:14:10 -07:00
from nova import db
from nova import flags
2010年07月15日 23:11:33 +00:00
from nova import rpc
from nova import utils
2010年08月30日 09:03:43 -07:00
from nova.network import linux_net
2010年07月18日 13:25:43 -05:00
FLAGS = flags.FLAGS
2010年09月07日 19:48:38 -07:00
flags.DECLARE('auth_driver', 'nova.auth.manager')
flags.DECLARE('network_size', 'nova.network.manager')
flags.DECLARE('num_networks', 'nova.network.manager')
2010年09月11日 04:06:22 -07:00
flags.DECLARE('update_dhcp_on_disassociate', 'nova.network.manager')
LOG = logging.getLogger('nova.dhcpbridge')
2011年08月04日 16:02:28 -04:00
def add_lease(mac, ip_address, _interface):
"""Set the IP that was assigned by the DHCP server."""
if FLAGS.fake_rabbit:
LOG.debug(_("leasing ip"))
2010年08月29日 18:53:47 -07:00
network_manager = utils.import_object(FLAGS.network_manager)
ip_address)
"%s.%s" % (FLAGS.network_topic, FLAGS.host),
"args": {"address": ip_address}})
2011年08月04日 16:02:28 -04:00
def old_lease(mac, ip_address, interface):
2010年10月18日 12:46:32 -07:00
"""Update just as add lease."""
2011年08月04日 16:02:28 -04:00
LOG.debug(_("Adopted old lease or got a change of mac"))
add_lease(mac, ip_address, interface)
2010年07月07日 12:25:22 -07:00
2011年08月04日 16:02:28 -04:00
def del_lease(mac, ip_address, _interface):
2010年08月09日 23:44:13 -07:00
"""Called when a lease expires."""
if FLAGS.fake_rabbit:
LOG.debug(_("releasing ip"))
2010年08月29日 18:53:47 -07:00
network_manager = utils.import_object(FLAGS.network_manager)
ip_address)
"%s.%s" % (FLAGS.network_topic, FLAGS.host),
"args": {"address": ip_address}})
2010年07月07日 12:25:22 -07:00
def init_leases(network_id):
"""Get the list of hosts for a network."""
network_ref = db.network_get(ctxt, network_id)
return linux_net.get_dhcp_leases(ctxt, network_ref)
2010年07月15日 23:11:33 +00:00
def main():
"""Parse environment and arguments and call the approproate action."""
2010年07月15日 23:11:33 +00:00
flagfile = os.environ.get('FLAGFILE', FLAGS.dhcpbridge_flagfile)
utils.default_flagfile(flagfile)
argv = FLAGS(sys.argv)
2011年02月21日 13:46:41 -08:00
logging.setup()
2011年03月29日 15:32:44 -05:00
# check ENV first so we don't break any older deploys
network_id = int(os.environ.get('NETWORK_ID'))
2010年07月15日 23:11:33 +00:00
if int(os.environ.get('TESTING', '0')):
from nova.tests import fake_flags
#if FLAGS.fake_rabbit:
# LOG.debug(_("leasing ip"))
# network_manager = utils.import_object(FLAGS.network_manager)
## reload(fake_flags)
# from nova.tests import fake_flags
action = argv[1]
mac = argv[2]
ip = argv[3]
2011年08月04日 16:02:28 -04:00
msg = _("Called %(action)s for mac %(mac)s with ip %(ip)s"
" on interface %(interface)s") % locals()
2011年08月04日 16:02:28 -04:00
globals()[action + '_lease'](mac, ip, interface)
print init_leases(network_id)
if __name__ == "__main__":