pvl/hosts/dhcp.py
author Tero Marttila <tero.marttila@aalto.fi>
Mon, 02 Mar 2015 17:58:24 +0200
changeset 689 c258e3ff6d32
parent 669 83e9bff09a0b
child 697 3c3ac207ce3f
permissions -rw-r--r--
pvl.hosts: update boot= to support split boot.next-server= boot.filename=, which enables inheriting defaults
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
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     4
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
     5
    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
     6
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
     7
def dhcp_host_options (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
     8
    """
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
     9
        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
    10
    """
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    11
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    12
    yield 'option', 'host-name', 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
    13
    yield 'hardware', 'ethernet', 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
    14
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    15
    if host.ip:
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
        yield 'fixed-address', str(host.ip)
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
      
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
    18
    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
    19
        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
    20
            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
    21
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
def dhcp_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
    23
    """
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    24
        Yield pvl.dhcp.config.Block's
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
    25
    """
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 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
    28
        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
    29
    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
    30
        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
    31
    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
    32
        # 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
    33
        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
    34
 
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    35
    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
    36
        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
    37
    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
    38
        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
    39
1e68e3a30b51 pvl.hosts.dhcp: split out of script, refactor using pvl.dhcp.config, and test
Tero Marttila <tero.marttila@aalto.fi>
parents:
diff changeset
    40
    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
    41
        name = host_fmt.format(host=host, index=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
    42
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    43
        yield pvl.dhcp.config.Block(('host', name), list(dhcp_host_options(host, ethernet)), comment=comment)
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
    44
    
483
19d084bb4afd pvl.hosts.dhcp: test and document hosts on multiple networks
Tero Marttila <tero.marttila@aalto.fi>
parents: 479
diff changeset
    45
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
    46
    """
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
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
        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
    49
    """
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
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
    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
    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
    for host in hosts:
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    54
        for block in dhcp_host(host):
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    55
            if block.key in blocks:
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    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]))
483
19d084bb4afd pvl.hosts.dhcp: test and document hosts on multiple networks
Tero Marttila <tero.marttila@aalto.fi>
parents: 479
diff changeset
    57
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    58
            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
    59
669
83e9bff09a0b pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents: 491
diff changeset
    60
            yield block