1 # vim: set encoding=utf-8 et sw=4 sts=4 :
4 import cPickle as pickle
8 from mako.template import Template
9 from mako.runtime import Context
12 def __init__(self, name, ip, mac):
17 return 'Host(name="%s", ip="%s", mac="%s")' % (
18 self.name, self.ip, self.mac)
22 def __init__(self, hosts):
25 def add(self, name, ip, mac):
26 #deberia indexar por hostname o por ip? o por mac? :)
28 self.hosts[name] = Host(name, ip, mac)
30 def update(self, name, ip=None, mac=None):
32 self.hosts[name].ip = ip
34 self.hosts[name].mac = mac
36 def delete(self, name):
40 return ','.join(self.hosts)
43 hosts = self.hosts.values()
44 return '\n'.join('%s,%s,%s' % (h.name, h.ip, h.mac) for h in hosts)
47 r"""class that handles DHCP service using dhcpd program"""
52 domain_name = 'my_domain_name',
55 net_address = '192.168.0.0',
56 net_mask = '255.255.255.0',
57 net_start = '192.168.0.100',
58 net_end = '192.168.0.200',
59 net_gateway = '192.168.0.1',
61 self.host = HostHandler(self.hosts)
63 def set(self, param, value):
64 if param in self.vars:
65 self.vars[param] = value
67 raise KeyError("Parameter " + param + " not found")
70 return ','.join(self.vars)
73 return '\n'.join(('%s,%s' % (k, v) for (k, v) in self.vars.items()))
76 #esto seria para poner en una interfaz
77 #y seria el hook para arrancar el servicio
81 #esto seria para poner en una interfaz
82 #y seria el hook para arrancar el servicio
86 #esto seria para poner en una interfaz
87 #y seria que hace el pickle deberia llamarse
89 pickle.dump(self.vars, file('pickled/vars.pkl', 'wb'), 2)
90 pickle.dump(self.hosts, file('pickled/hosts.pkl', 'wb'), 2)
91 tpl = Template(filename='templates/dhcpd.conf')
92 ctx = Context(file('generated/dhcpd.conf', 'w'),
93 hosts=self.hosts.values(), **self.vars)
94 tpl.render_context(ctx)
96 if __name__ == '__main__':
98 config = DhcpHandler()
100 config.host.add('my_name','192.168.0.102','00:12:ff:56')
102 config.host.update('my_name','192.168.0.192','00:12:ff:56')
104 config.host.add('nico','192.168.0.188','00:00:00:00')
106 config.set('domain_name','baryon.com.ar')
109 config.set('sarasa','baryon.com.ar')
115 print 'Variables:', config.list()
118 print 'Hosts:', config.host.list()
119 print config.host.show()
121 vars = pickle.load(file('pickled/vars.pkl'))
122 hosts = pickle.load(file('pickled/hosts.pkl'))
123 print 'Pickled vars:', vars
124 print 'Pickled hosts:', hosts