pvl/hosts/dhcp.py
changeset 669 83e9bff09a0b
parent 491 cfcb47a3dc3e
child 689 c258e3ff6d32
equal deleted inserted replaced
668:794f943c835d 669:83e9bff09a0b
     4 class HostDHCPError(pvl.hosts.host.HostError):
     4 class HostDHCPError(pvl.hosts.host.HostError):
     5     pass
     5     pass
     6 
     6 
     7 def dhcp_host_options (host, ethernet):
     7 def dhcp_host_options (host, ethernet):
     8     """
     8     """
     9         Yield specific dhcp.conf host { ... } parameters for build_block()
     9         Yield specific dhcp.conf host { ... } items.
    10     """
    10     """
    11 
    11 
    12     yield 'option', 'host-name', host.name
    12     yield 'option', 'host-name', host.name
    13     yield 'hardware', 'ethernet', ethernet
    13     yield 'hardware', 'ethernet', ethernet
    14 
    14 
    34     if filename:
    34     if filename:
    35         yield 'filename', filename
    35         yield 'filename', filename
    36 
    36 
    37 def dhcp_host (host):
    37 def dhcp_host (host):
    38     """
    38     """
    39         Yield (block, items, **opts) tuples for pvl.dhcp.config.build_block().
    39         Yield pvl.dhcp.config.Block's
    40     """
    40     """
    41 
    41 
    42     if set(host.ethernet) == set([None]) :
    42     if set(host.ethernet) == set([None]) :
    43         host_fmt = "{host.name}"
    43         host_fmt = "{host.name}"
    44     elif host.ethernet :
    44     elif host.ethernet :
    53         comment = None
    53         comment = None
    54 
    54 
    55     for index, ethernet in host.ethernet.iteritems() :
    55     for index, ethernet in host.ethernet.iteritems() :
    56         name = host_fmt.format(host=host, index=index)
    56         name = host_fmt.format(host=host, index=index)
    57 
    57 
    58         yield ('host', name), list(dhcp_host_options(host, ethernet)), dict(comment=comment)
    58         yield pvl.dhcp.config.Block(('host', name), list(dhcp_host_options(host, ethernet)), comment=comment)
    59     
    59     
    60 def dhcp_hosts (hosts):
    60 def dhcp_hosts (hosts):
    61     """
    61     """
    62 
    62 
    63         Verifies that there are no dupliate hosts.
    63         Verifies that there are no dupliate hosts.
    64     """
    64     """
    65 
    65 
    66     blocks = { }
    66     blocks = { }
    67 
    67 
    68     for host in hosts:
    68     for host in hosts:
    69         for block, items, opts in dhcp_host(host):
    69         for block in dhcp_host(host):
    70             if block in blocks:
    70             if block.key in blocks:
    71                 raise HostDHCPError(host, "hosts on multiple networks must use unique ethernet.XXX=... naming: {other}".format(block=block, other=blocks[block]))
    71                 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]))
    72 
    72 
    73             blocks[block] = host
    73             blocks[block.key] = host
    74 
    74 
    75             yield block, items, opts
    75             yield block
    76 
       
    77 def apply_hosts_dhcp (hosts):
       
    78     """
       
    79         Generate dhcp.conf output lines for the set of hosts.
       
    80     """
       
    81     
       
    82     for block, items, opts in dhcp_hosts(hosts):
       
    83         for line in pvl.dhcp.config.build_block(block, items, **opts):
       
    84             yield line
       
    85         
       
    86         yield ''
       
    87