1 # vim: set encoding=utf-8 et sw=4 sts=4 :
3 from subprocess import Popen, PIPE
6 from pymin.seqtools import Sequence
7 from pymin.dispatcher import handler, HandlerError, Handler
8 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
9 TransactionalHandler, SubHandler, call, \
10 get_network_devices, ListComposedSubHandler, \
11 DictComposedSubHandler, Device, Address, ExecutionError
13 __ALL__ = ('IpHandler',)
15 # TODO: convertir HopHandler a ComposedSubHandler
17 class HopError(HandlerError):
19 def __init__(self, hop):
20 self.message = u'Hop error : "%s"' % hop
22 class HopNotFoundError(HopError):
24 def __init__(self, hop):
25 self.message = u'Hop not found : "%s"' % hop
27 class HopAlreadyExistsError(HopError):
29 def __init__(self, hop):
30 self.message = u'Hop already exists : "%s"' % hop
35 def __init__(self, gateway, device):
36 self.gateway = gateway
40 return (self.gateway, self.device)
42 def __cmp__(self, other):
43 if self.gateway == other.gateway \
44 and self.device == other.device:
46 return cmp(id(self), id(other))
48 class HopHandler(Handler):
50 def __init__(self, parent):
53 @handler('Adds a hop : add <gateway> <device>')
54 def add(self, gw, dev):
55 if not dev in self.parent.devices:
56 raise DeviceNotFoundError(device)
59 self.parent.hops.index(h)
60 raise HopAlreadyExistsError(gw + '->' + dev)
62 self.parent.hops.append(h)
64 @handler(u'Deletes a hop : delete <gateway> <device>')
65 def delete(self, gw, dev):
66 if not dev in self.parent.devices:
67 raise DeviceNotFoundError(device)
70 self.parent.hops.remove(h)
72 raise HopNotFoundError(gw + '->' + dev)
74 @handler(u'Lists hops : list <dev>')
75 def list(self, device):
77 k = self.parent.hops.keys()
82 @handler(u'Get information about all hops: show <dev>')
83 def show(self, device):
85 k = self.parent.hops.values()
90 class Route(Sequence):
91 def __init__(self, net_addr, prefix, gateway):
92 self.net_addr = net_addr
94 self.gateway = gateway
95 def update(self, net_addr=None, prefix=None, gateway=None):
96 if net_addr is not None: self.net_addr = net_addr
97 if prefix is not None: self.prefix = prefix
98 if gateway is not None: self.gateway = gateway
100 return(self.net_addr, self.prefix, self.gateway)
102 class RouteHandler(ListComposedSubHandler):
103 handler_help = u"Manage IP routes"
104 _comp_subhandler_cont = 'devices'
105 _comp_subhandler_attr = 'routes'
106 _comp_subhandler_class = Route
109 class AddressHandler(DictComposedSubHandler):
110 handler_help = u"Manage IP addresses"
111 _comp_subhandler_cont = 'devices'
112 _comp_subhandler_attr = 'addrs'
113 _comp_subhandler_class = Address
116 class DeviceHandler(SubHandler):
118 handler_help = u"Manage network devices"
120 def __init__(self, parent):
121 # FIXME remove templates to execute commands
122 from mako.template import Template
124 template_dir = path.join(path.dirname(__file__), 'templates')
125 dev_fn = path.join(template_dir, 'device')
126 self.device_template = Template(filename=dev_fn)
128 @handler(u'Bring the device up')
130 if name in self.parent.devices:
131 call(self.device_template.render(dev=name, action='up'), shell=True)
133 raise DeviceNotFoundError(name)
135 @handler(u'Bring the device down')
136 def down(self, name):
137 if name in self.parent.devices:
138 call(self.device_template.render(dev=name, action='down'), shell=True)
140 raise DeviceNotFoundError(name)
142 @handler(u'List all devices')
144 return self.parent.devices.keys()
146 @handler(u'Get information about a device')
148 return self.parent.devices.items()
150 class IpHandler(Restorable, ConfigWriter, TransactionalHandler):
152 handler_help = u"Manage IP devices, addresses, routes and hops"
154 _persistent_attrs = ('devices','hops')
156 _restorable_defaults = dict(
157 devices=get_network_devices(),
161 _config_writer_files = ('device', 'ip_add', 'ip_del', 'ip_flush',
162 'route_add', 'route_del', 'route_flush', 'hop')
163 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
165 def __init__(self, pickle_dir='.', config_dir='.'):
166 r"Initialize DhcpHandler object, see class documentation for details."
167 self._persistent_dir = pickle_dir
168 self._config_writer_cfg_dir = config_dir
169 self._config_build_templates()
172 self.addr = AddressHandler(self)
173 self.route = RouteHandler(self)
174 self.dev = DeviceHandler(self)
175 self.hop = HopHandler(self)
176 self.services = list()
178 def _write_config(self):
179 r"_write_config() -> None :: Execute all commands."
180 for device in self.devices.values():
182 self._write_config_for_device(device)
185 def _write_hops(self):
186 r"_write_hops() -> None :: Execute all hops."
189 call('ip route del default', shell=True)
190 except ExecutionError, e:
193 #get hops for active devices
196 if h.device in self.devices:
197 if self.devices[h.device].active
198 active_hops.append(h)
199 call(self._render_config('hop', dict(
203 except ExecutionError, e:
206 def _write_config_for_device(self, device):
207 r"_write_config_for_device(self, device) -> None :: Execute all commands for a device."
209 call(self._render_config('route_flush', dict(dev=device.name)), shell=True)
210 except ExecutionError, e:
213 call(self._render_config('ip_flush', dict(dev=device.name)), shell=True)
214 except ExecutionError, e:
216 for address in device.addrs.values():
217 broadcast = address.broadcast
218 if broadcast is None:
221 call(self._render_config('ip_add', dict(
224 netmask = address.netmask,
226 broadcast = broadcast,
229 except ExecutionError, e:
231 for route in device.routes:
233 call(self._render_config('route_add', dict(
235 net_addr = route.net_addr,
236 prefix = route.prefix,
237 gateway = route.gateway,
240 except ExecutionError, e:
243 def handle_timer(self):
244 self.refresh_devices()
247 def refresh_devices(self):
248 devices = get_network_devices()
249 #add not registered and active devices
251 for k,v in devices.items():
252 if k not in self.devices:
254 else if not self.devices[k].active
257 self._write_config_for_device(self.devices[k])
264 except ExecutionError:
268 except ExecutionError:
271 #mark inactive devices
272 for k in self.devices.keys():
274 self.devices[k].active = False
276 #hooks a service to the ip handler, so when
277 #a device is brought up one can restart the service
278 #that need to refresh their device list
279 def device_up_hook(self, serv):
280 if hasattr(serv, 'stop') and hasattr(serv, 'start')
281 services.append(serv)
287 if __name__ == '__main__':
290 print '----------------------'
291 ip.hop.add('201.21.32.53','eth0')
292 ip.hop.add('205.65.65.25','eth1')
295 ip.addr.add('eth0','192.168.0.23','24','192.168.255.255')
296 ip.addr.add('eth0','192.168.0.26','24')
298 ip.route.add('eth0','192.168.0.0','24','192.168.0.1')
299 ip.route.add('eth0','192.168.0.5','24','192.168.0.1')
301 ip.hop.delete('201.21.32.53','eth0')
302 ip.route.clear('eth0')