host.py
author Tero Marttila <terom@fixme.fi>
Sun, 12 Jul 2009 00:43:36 +0300
changeset 6 57e8168ba8c4
parent 5 86b05c0ab5cd
permissions -rw-r--r--
use FQDN for zone hosts
"""
    Information about one physica host
"""

import dhcp
import bind_conf as bindc

class Interface (object) :
    """
        A physical interface for a host
    """

    def __init__ (self, mac_addr, name=None) :
        """
            @param name the short name of the interface (e.g. 'lan' or 'wlan'), or None for no suffix
            @param mac the physical-layer addr.MAC address
        """

        self.addr = mac_addr
        self.name = name

class Host (object) :
    """
        A host has a single address/name, an owner, and multiple interfaces
    """

    def __init__ (self, hostname, address, interfaces) :
        """
            @param hostname the short hostname, without the domain name component
            @param address the addr.IP address
            @param interfaces a list of zero or more Interface objects
        """

        self.hostname = hostname
        self.address = address
        self.interfaces = interfaces
    
    def build_dhcp_hosts (self) :
        """
            Build and yield a series of dhcp_conf.Host objects for this host.

            If the host does not have any interfaces defined, this doesn't yield anything
        """
        
        # XXX: do we want to ensure that the host names are unique?
        
        for iface in self.interfaces :
            # the DHCP hostname
            name = "%s%s" % (self.hostname, ('-%s' % (iface.name)) if iface.name else '')
            
            # build it
            yield dhcp.Host(name, iface.addr, self.address)

    def build_bind_domain_records (self, origin) :
        """
            Build and yield one or more forward records (A/AAAA) for the host, with the given domain as the origin
        """

        yield bindc.A(self.hostname, self.address)