pvl/hosts/dhcp.py
author Tero Marttila <tero.marttila@aalto.fi>
Mon, 02 Mar 2015 19:45:56 +0200
changeset 698 656178fb8607
parent 697 3c3ac207ce3f
child 700 88b0d3df1ad7
permissions -rw-r--r--
pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     1
import pvl.dhcp.config
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     2
import pvl.hosts.host
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     3
698
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
     4
def dhcp_host_subclass (host, subclass, ethernet):
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
     5
    """
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
     6
        Build a DHCP Item for declaring a subclass for a host.
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
     7
    """
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
     8
    
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
     9
    # hardware-type prefixed hardware-address
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    10
    hardware = pvl.dhcp.config.Field('1:' + ethernet)
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    11
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    12
    return pvl.dhcp.config.Block(None, [
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    13
        ('subclass', subclass, hardware),
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    14
    ])
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    15
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    16
class HostDHCPError(pvl.hosts.host.HostError):
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    17
    pass
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    18
698
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    19
def dhcp_host_options (host, ethernet, subclass=None):
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    20
    """
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    21
        Yield specific dhcp.conf host { ... } items.
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    22
    """
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    23
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    24
    yield 'option', 'host-name', host.name
697
3c3ac207ce3f pvl.hosts.dhcp: use pvl.dhcp.config.Field() to format non-string field values
Tero Marttila <tero.marttila@aalto.fi>
parents: 689
diff changeset
    25
    yield 'hardware', 'ethernet', pvl.dhcp.config.Field(ethernet)
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    26
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    27
    if host.ip:
697
3c3ac207ce3f pvl.hosts.dhcp: use pvl.dhcp.config.Field() to format non-string field values
Tero Marttila <tero.marttila@aalto.fi>
parents: 689
diff changeset
    28
        yield 'fixed-address', pvl.dhcp.config.Field(str(host.ip))
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    29
      
689
c258e3ff6d32 pvl.hosts: update boot= to support split boot.next-server= boot.filename=, which enables inheriting defaults
Tero Marttila <tero.marttila@aalto.fi>
parents: 669
diff changeset
    30
    for bootopt in ('next-server', 'filename'):
c258e3ff6d32 pvl.hosts: update boot= to support split boot.next-server= boot.filename=, which enables inheriting defaults
Tero Marttila <tero.marttila@aalto.fi>
parents: 669
diff changeset
    31
        if bootopt in host.boot:
c258e3ff6d32 pvl.hosts: update boot= to support split boot.next-server= boot.filename=, which enables inheriting defaults
Tero Marttila <tero.marttila@aalto.fi>
parents: 669
diff changeset
    32
            yield bootopt, host.boot[bootopt]
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    33
698
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    34
def dhcp_host (host,
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    35
        subclass    = None,
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    36
):
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    37
    """
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    38
        Yield pvl.dhcp.config.Block's
698
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    39
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    40
        Takes dhcp:* extensions as keyword arguments
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    41
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    42
            subclass: name      - generate a subclass name $ethernet for this host
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    43
    """
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    44
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    45
    if set(host.ethernet) == set([None]) :
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    46
        host_fmt = "{host.name}"
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    47
    elif host.ethernet :
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    48
        host_fmt = "{host.name}-{index}"
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    49
    else :
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    50
        # nothing to be seen here
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    51
        return
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    52
 
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    53
    if host.owner :
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    54
        comment = u"Owner: {host.owner}".format(host=host)
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    55
    else:
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    56
        comment = None
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    57
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    58
    for index, ethernet in host.ethernet.iteritems() :
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    59
        name = host_fmt.format(host=host, index=index)
698
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    60
        items = list(dhcp_host_options(host, ethernet))
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    61
698
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    62
        yield pvl.dhcp.config.Block(('host', name), items, comment=comment)
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    63
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    64
        if subclass:
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    65
            yield dhcp_host_subclass(host, subclass, ethernet)
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    66
    
483
19d084bb4afd pvl.hosts.dhcp: test and document hosts on multiple networks
Tero Marttila <tero.marttila@aalto.fi>
parents: 479
diff changeset
    67
def dhcp_hosts (hosts):
479
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    68
    """
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    69
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    70
        Verifies that there are no dupliate hosts.
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    71
    """
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    72
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    73
    blocks = { }
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    74
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    75
    for host in hosts:
698
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    76
        extensions = host.extensions.get('dhcp', {})
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    77
656178fb8607 pvl.hosts.dhcp: implement support for dhcp:subclass=... using hardware ethernet
Tero Marttila <tero.marttila@aalto.fi>
parents: 697
diff changeset
    78
        for block in dhcp_host(host, **extensions):
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    79
            if block.key in blocks:
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    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]))
483
19d084bb4afd pvl.hosts.dhcp: test and document hosts on multiple networks
Tero Marttila <tero.marttila@aalto.fi>
parents: 479
diff changeset
    81
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    82
            blocks[block.key] = host
483
19d084bb4afd pvl.hosts.dhcp: test and document hosts on multiple networks
Tero Marttila <tero.marttila@aalto.fi>
parents: 479
diff changeset
    83
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    84
            yield block