1 #!/usr/bin/env python |
1 #!/usr/bin/env python |
2 |
2 |
|
3 import logging; log = logging.getLogger('pvl.hosts-dhcp') |
|
4 import optparse |
3 import pvl.args |
5 import pvl.args |
4 import pvl.hosts |
6 import pvl.hosts |
|
7 import pvl.hosts.dhcp |
5 |
8 |
6 import configobj |
9 def main (argv): |
7 import logging; log = logging.getLogger('pvl.hosts-dhcp') |
|
8 import optparse |
|
9 |
|
10 def build_host (host_name, *items) : |
|
11 yield "host {host} {{".format(host=host_name) |
|
12 for item in items : |
|
13 if isinstance(item, dict) : |
|
14 for setting, value in item.iteritems() : |
|
15 if value : |
|
16 yield "\t{setting:30} {value};".format(setting=setting, value=value) |
|
17 else : |
|
18 raise ValueError("Unknown item: %r", item) |
|
19 yield "}" |
|
20 |
|
21 def dhcp_quote (value) : |
|
22 if value is None : |
|
23 return None |
|
24 else : |
|
25 return '"{value}"'.format(value=value) |
|
26 |
|
27 def process_host (options, host) : |
|
28 if host.boot : |
|
29 if ':' in host.boot : |
|
30 next_server, filename = host.boot.split(':', 1) |
|
31 elif host.boot.startswith('/') : |
|
32 next_server = None |
|
33 filename = host.boot |
|
34 elif host.boot.endswith(':') : |
|
35 next_server = host.boot |
|
36 filename = None |
|
37 else : |
|
38 log.error("%s: invalid boot: %s", host, host.boot) |
|
39 else : |
|
40 next_server = filename = None |
|
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 there |
|
48 return |
|
49 |
|
50 if host.owner : |
|
51 yield u"# Owner: {host.owner}".format(host=host) |
|
52 |
|
53 for index, ethernet in host.ethernet.iteritems() : |
|
54 for line in build_host(host_fmt.format(host=host, index=index), |
|
55 { 'option host-name': dhcp_quote(host.name) }, |
|
56 { 'hardware ethernet': ethernet }, |
|
57 { 'fixed-address': host.ip }, |
|
58 { 'next-server': next_server }, |
|
59 { 'filename': dhcp_quote(filename) }, |
|
60 ) : |
|
61 yield line |
|
62 |
|
63 yield "" |
|
64 |
|
65 def process_hosts (options, hosts) : |
|
66 for host in hosts : |
|
67 for line in process_host(options, host) : |
|
68 yield line |
|
69 |
|
70 def apply_conf (options, lines) : |
|
71 for line in lines : |
|
72 print line |
|
73 |
|
74 def main (argv) : |
|
75 """ |
10 """ |
76 Generate DHCP host configs from host definitions. |
11 Generate DHCP host configs from host definitions. |
77 """ |
12 """ |
78 |
13 |
79 parser = optparse.OptionParser(main.__doc__) |
14 parser = optparse.OptionParser(main.__doc__) |
80 parser.add_option_group(pvl.args.parser(parser)) |
15 parser.add_option_group(pvl.args.parser(parser)) |
81 parser.add_option_group(pvl.hosts.optparser(parser)) |
16 parser.add_option_group(pvl.hosts.config.optparser(parser)) |
82 |
|
83 options, args = parser.parse_args(argv[1:]) |
|
84 pvl.args.apply(options) |
|
85 |
17 |
86 # input |
18 # input |
|
19 options, args = pvl.args.parse(parser, argv) |
|
20 |
87 hosts = pvl.hosts.apply(options, args) |
21 hosts = pvl.hosts.apply(options, args) |
88 |
22 |
89 # process |
23 # process |
90 apply_conf(options, process_hosts(options, hosts)) |
24 for line in pvl.hosts.dhcp.apply_hosts_dhcp(options, hosts): |
|
25 print line |
91 |
26 |
92 if __name__ == '__main__': |
27 if __name__ == '__main__': |
93 pvl.args.main(main) |
28 pvl.args.main(main) |