pvl/hosts/dhcp.py
changeset 698 656178fb8607
parent 697 3c3ac207ce3f
child 700 88b0d3df1ad7
equal deleted inserted replaced
697:3c3ac207ce3f 698:656178fb8607
     1 import pvl.dhcp.config
     1 import pvl.dhcp.config
     2 import pvl.hosts.host
     2 import pvl.hosts.host
       
     3 
       
     4 def dhcp_host_subclass (host, subclass, ethernet):
       
     5     """
       
     6         Build a DHCP Item for declaring a subclass for a host.
       
     7     """
       
     8     
       
     9     # hardware-type prefixed hardware-address
       
    10     hardware = pvl.dhcp.config.Field('1:' + ethernet)
       
    11 
       
    12     return pvl.dhcp.config.Block(None, [
       
    13         ('subclass', subclass, hardware),
       
    14     ])
     3 
    15 
     4 class HostDHCPError(pvl.hosts.host.HostError):
    16 class HostDHCPError(pvl.hosts.host.HostError):
     5     pass
    17     pass
     6 
    18 
     7 def dhcp_host_options (host, ethernet):
    19 def dhcp_host_options (host, ethernet, subclass=None):
     8     """
    20     """
     9         Yield specific dhcp.conf host { ... } items.
    21         Yield specific dhcp.conf host { ... } items.
    10     """
    22     """
    11 
    23 
    12     yield 'option', 'host-name', host.name
    24     yield 'option', 'host-name', host.name
    17       
    29       
    18     for bootopt in ('next-server', 'filename'):
    30     for bootopt in ('next-server', 'filename'):
    19         if bootopt in host.boot:
    31         if bootopt in host.boot:
    20             yield bootopt, host.boot[bootopt]
    32             yield bootopt, host.boot[bootopt]
    21 
    33 
    22 def dhcp_host (host):
    34 def dhcp_host (host,
       
    35         subclass    = None,
       
    36 ):
    23     """
    37     """
    24         Yield pvl.dhcp.config.Block's
    38         Yield pvl.dhcp.config.Block's
       
    39 
       
    40         Takes dhcp:* extensions as keyword arguments
       
    41 
       
    42             subclass: name      - generate a subclass name $ethernet for this host
    25     """
    43     """
    26 
    44 
    27     if set(host.ethernet) == set([None]) :
    45     if set(host.ethernet) == set([None]) :
    28         host_fmt = "{host.name}"
    46         host_fmt = "{host.name}"
    29     elif host.ethernet :
    47     elif host.ethernet :
    37     else:
    55     else:
    38         comment = None
    56         comment = None
    39 
    57 
    40     for index, ethernet in host.ethernet.iteritems() :
    58     for index, ethernet in host.ethernet.iteritems() :
    41         name = host_fmt.format(host=host, index=index)
    59         name = host_fmt.format(host=host, index=index)
       
    60         items = list(dhcp_host_options(host, ethernet))
    42 
    61 
    43         yield pvl.dhcp.config.Block(('host', name), list(dhcp_host_options(host, ethernet)), comment=comment)
    62         yield pvl.dhcp.config.Block(('host', name), items, comment=comment)
       
    63 
       
    64         if subclass:
       
    65             yield dhcp_host_subclass(host, subclass, ethernet)
    44     
    66     
    45 def dhcp_hosts (hosts):
    67 def dhcp_hosts (hosts):
    46     """
    68     """
    47 
    69 
    48         Verifies that there are no dupliate hosts.
    70         Verifies that there are no dupliate hosts.
    49     """
    71     """
    50 
    72 
    51     blocks = { }
    73     blocks = { }
    52 
    74 
    53     for host in hosts:
    75     for host in hosts:
    54         for block in dhcp_host(host):
    76         extensions = host.extensions.get('dhcp', {})
       
    77 
       
    78         for block in dhcp_host(host, **extensions):
    55             if block.key in blocks:
    79             if block.key in blocks:
    56                 raise HostDHCPError(host, "dhcp {block} conflict with {other}; hosts on multiple networks must use unique ethernet.XXX=... naming".format(block=block, other=blocks[block.key]))
    80                 raise HostDHCPError(host, "dhcp {block} conflict with {other}; hosts on multiple networks must use unique ethernet.XXX=... naming".format(block=block, other=blocks[block.key]))
    57 
    81 
    58             blocks[block.key] = host
    82             blocks[block.key] = host
    59 
    83