|
1 import pvl.dhcp.config |
|
2 import pvl.hosts.host |
|
3 |
|
4 class HostDHCPError(pvl.hosts.host.HostError): |
|
5 pass |
|
6 |
|
7 def dhcp_host_options (host, ethernet): |
|
8 """ |
|
9 Yield specific dhcp.conf host { ... } parameters for build_block() |
|
10 """ |
|
11 |
|
12 yield 'option', 'host-name', host.name |
|
13 yield 'hardware', 'ethernet', ethernet |
|
14 |
|
15 if host.ip: |
|
16 yield 'fixed-address', str(host.ip) |
|
17 |
|
18 if not host.boot: |
|
19 next_server = filename = None |
|
20 elif ':' in host.boot : |
|
21 next_server, filename = host.boot.split(':', 1) |
|
22 elif host.boot.startswith('/') : |
|
23 next_server = None |
|
24 filename = host.boot |
|
25 elif host.boot.endswith(':') : |
|
26 next_server = host.boot |
|
27 filename = None |
|
28 else : |
|
29 raise HostError(host, "invalid boot={host.boot}".format(host=host)) |
|
30 |
|
31 if next_server: |
|
32 yield 'next-server', next_server |
|
33 |
|
34 if filename: |
|
35 yield 'filename', filename |
|
36 |
|
37 def dhcp_host (host): |
|
38 """ |
|
39 Yield (block, items, **opts) tuples for pvl.dhcp.config.build_block(). |
|
40 """ |
|
41 |
|
42 if set(host.ethernet) == set([None]) : |
|
43 host_fmt = "{host.name}" |
|
44 elif host.ethernet : |
|
45 host_fmt = "{host.name}-{index}" |
|
46 else : |
|
47 # nothing to be seen here |
|
48 return |
|
49 |
|
50 if host.owner : |
|
51 comment = u"Owner: {host.owner}".format(host=host) |
|
52 else: |
|
53 comment = None |
|
54 |
|
55 for index, ethernet in host.ethernet.iteritems() : |
|
56 name = host_fmt.format(host=host, index=index) |
|
57 |
|
58 yield ('host', name), list(dhcp_host_options(host, ethernet)), dict(comment=comment) |
|
59 |
|
60 def apply_hosts_dhcp (options, hosts): |
|
61 """ |
|
62 Generate dhcp.conf output lines for the set of hosts. |
|
63 |
|
64 Verifies that there are no dupliate hosts. |
|
65 """ |
|
66 |
|
67 blocks = { } |
|
68 |
|
69 for host in hosts: |
|
70 for block, items, opts in dhcp_host(host): |
|
71 if block in blocks: |
|
72 raise HostDHCPError(host, "dupliate host block {block}: {other}".format(block=block, other=blocks[block])) |
|
73 |
|
74 blocks[block] = block |
|
75 |
|
76 for line in pvl.dhcp.config.build_block(block, items, **opts): |
|
77 yield line |
|
78 |
|
79 yield '' |
|
80 |