¡@

Home 

OpenStack Study: dhcpbridge.py

OpenStack Index

**** CubicPower OpenStack Study ****

def add_lease(mac, ip_address):

    """Set the IP that was assigned by the DHCP server."""

    api = network_rpcapi.NetworkAPI()

    api.lease_fixed_ip(context.get_admin_context(), ip_address, CONF.host)

**** CubicPower OpenStack Study ****

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

**** CubicPower OpenStack Study ****

def del_lease(mac, ip_address):

    """Called when a lease expires."""

    api = network_rpcapi.NetworkAPI()

    api.release_fixed_ip(context.get_admin_context(), ip_address,

                         CONF.host)

**** CubicPower OpenStack Study ****

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)

**** CubicPower OpenStack Study ****

def add_action_parsers(subparsers):

    parser = subparsers.add_parser('init')

    # NOTE(cfb): dnsmasq always passes mac, and ip. hostname

    #            is passed if known. We don't care about

    #            hostname, but argparse will complain if we

    #            do not accept it.

    for action in ['add', 'del', 'old']:

        parser = subparsers.add_parser(action)

        parser.add_argument('mac')

        parser.add_argument('ip')

        parser.add_argument('hostname', nargs='?', default='')

        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))

**** CubicPower OpenStack Study ****

def main():

    """Parse environment and arguments and call the appropriate action."""

    config.parse_args(sys.argv,

        default_config_files=jsonutils.loads(os.environ['CONFIG_FILE']))

    logging.setup("nova")

    global LOG

    LOG = logging.getLogger('nova.dhcpbridge')

    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."))

            return(1)

        print(init_leases(network_id))

    rpc.cleanup()