author | Tero Marttila <terom@paivola.fi> |
Mon, 09 Mar 2015 18:00:18 +0200 | |
changeset 733 | 45bedeba92e5 |
parent 731 | 5e2c1b78047d |
child 739 | 5149c39f3dfc |
permissions | -rw-r--r-- |
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 |
700
88b0d3df1ad7
pvl.hosts.dhcp: subclass class name is a String
Tero Marttila <tero.marttila@aalto.fi>
parents:
698
diff
changeset
|
10 |
hardware = '1:' + ethernet |
698
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, [ |
700
88b0d3df1ad7
pvl.hosts.dhcp: subclass class name is a String
Tero Marttila <tero.marttila@aalto.fi>
parents:
698
diff
changeset
|
13 |
('subclass', pvl.dhcp.config.String(subclass), pvl.dhcp.config.Field(hardware)), |
698
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 |
|
733
45bedeba92e5
pvl.hosts: rename Host.ip -> Host.ip4; support instanced ip.foo = ... for foo.host A .... sub-labels
Tero Marttila <terom@paivola.fi>
parents:
731
diff
changeset
|
27 |
if host.ip4: |
45bedeba92e5
pvl.hosts: rename Host.ip -> Host.ip4; support instanced ip.foo = ... for foo.host A .... sub-labels
Tero Marttila <terom@paivola.fi>
parents:
731
diff
changeset
|
28 |
yield 'fixed-address', pvl.dhcp.config.Field(str(host.ip4)) |
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 |
|
731
5e2c1b78047d
pvl.hosts.dhcp: fix mixed ethernet instance/None naming to be per-dhcp-host
Tero Marttila <terom@paivola.fi>
parents:
708
diff
changeset
|
45 |
if not 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
|
46 |
# 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
|
47 |
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
|
48 |
|
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 |
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
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
|
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 |
for index, ethernet in host.ethernet.iteritems() : |
731
5e2c1b78047d
pvl.hosts.dhcp: fix mixed ethernet instance/None naming to be per-dhcp-host
Tero Marttila <terom@paivola.fi>
parents:
708
diff
changeset
|
55 |
if index: |
5e2c1b78047d
pvl.hosts.dhcp: fix mixed ethernet instance/None naming to be per-dhcp-host
Tero Marttila <terom@paivola.fi>
parents:
708
diff
changeset
|
56 |
name = '{host.name}-{index}'.format(host=host, index=index) |
5e2c1b78047d
pvl.hosts.dhcp: fix mixed ethernet instance/None naming to be per-dhcp-host
Tero Marttila <terom@paivola.fi>
parents:
708
diff
changeset
|
57 |
else: |
5e2c1b78047d
pvl.hosts.dhcp: fix mixed ethernet instance/None naming to be per-dhcp-host
Tero Marttila <terom@paivola.fi>
parents:
708
diff
changeset
|
58 |
name = '{host.name}'.format(host=host) |
5e2c1b78047d
pvl.hosts.dhcp: fix mixed ethernet instance/None naming to be per-dhcp-host
Tero Marttila <terom@paivola.fi>
parents:
708
diff
changeset
|
59 |
|
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): |
708
fd6f0f044f42
pvl.hosts.dhcp: only check host blocks for uniqueness; ignore subclasses
Tero Marttila <tero.marttila@aalto.fi>
parents:
700
diff
changeset
|
79 |
if not block.key: |
fd6f0f044f42
pvl.hosts.dhcp: only check host blocks for uniqueness; ignore subclasses
Tero Marttila <tero.marttila@aalto.fi>
parents:
700
diff
changeset
|
80 |
# TODO: check for unique Item-Blocks |
fd6f0f044f42
pvl.hosts.dhcp: only check host blocks for uniqueness; ignore subclasses
Tero Marttila <tero.marttila@aalto.fi>
parents:
700
diff
changeset
|
81 |
pass |
fd6f0f044f42
pvl.hosts.dhcp: only check host blocks for uniqueness; ignore subclasses
Tero Marttila <tero.marttila@aalto.fi>
parents:
700
diff
changeset
|
82 |
elif block.key in blocks: |
669
83e9bff09a0b
pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents:
491
diff
changeset
|
83 |
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])) |
708
fd6f0f044f42
pvl.hosts.dhcp: only check host blocks for uniqueness; ignore subclasses
Tero Marttila <tero.marttila@aalto.fi>
parents:
700
diff
changeset
|
84 |
else: |
fd6f0f044f42
pvl.hosts.dhcp: only check host blocks for uniqueness; ignore subclasses
Tero Marttila <tero.marttila@aalto.fi>
parents:
700
diff
changeset
|
85 |
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
|
86 |
|
669
83e9bff09a0b
pvl.hosts.dhcp: update for pvl.dhcp.config
Tero Marttila <terom@paivola.fi>
parents:
491
diff
changeset
|
87 |
yield block |