forked from andrey-malets/cfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhcp.py
23 lines (20 loc) · 833 Bytes
/
dhcp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import jinja2
from itertools import chain
from cmd import add_cmd
@add_cmd('dhcp', True, 0)
def gen_dhcp(state, template):
def get_entries(host):
def get_entry(name, mac):
return {'name' : name,
'hostname' : host.sname,
'domain' : host.name[len(host.sname)+1:],
'mac' : mac,
'addr' : host.addr,
'params' : host.props.get('dhcp', {})}
if len(host.macs) == 1:
yield get_entry(host.sname, host.macs[0])
else:
for i in xrange(len(host.macs)):
yield get_entry('%s-%d' % (host.sname, i+1), host.macs[i])
return jinja2.Template(template).render(networks=state.networks,
hosts=chain.from_iterable(map(get_entries, state.hosts)))