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
96 def update(self, net_addr=None, prefix=None, gateway=None):
97 if net_addr is not None: self.net_addr = net_addr
98 if prefix is not None: self.prefix = prefix
99 if gateway is not None: self.gateway = gateway
102 return(self.net_addr, self.prefix, self.gateway)
104 def __cmp__(self, other):
105 if self.net_addr == other.net_addr \
106 and self.prefix == other.prefix \
107 and self.gateway == other.gateway:
109 return cmp(id(self), id(other))
111 class RouteHandler(ListComposedSubHandler):
112 handler_help = u"Manage IP routes"
113 _comp_subhandler_cont = 'devices'
114 _comp_subhandler_attr = 'routes'
115 _comp_subhandler_class = Route
117 @handler(u'Adds a route to : ip route add <net_addr> <prefix> <gateway> [device]')
118 def add(self, net_addr, prefix, gateway, dev=None):
120 ListComposedSubHandler.add(self, dev, net_addr, prefix, gateway)
122 r = Route(net_addr, prefix, gateway)
123 if not r in self.parent.no_device_routes:
124 self.parent.no_device_routes.append(r)
126 @handler("Deletes a route : ip route delete <route_number_in_show> [dev]")
127 def delete(self, index, dev=None):
129 ListComposedSubHandler.delete(self, dev, index)
132 del self.parent.no_device_routes[i]
134 @handler("Shows routes : ip route show [dev]")
135 def show(self, dev=None):
137 return ListComposedSubHandler.show(self, dev)
139 return self.parent.no_device_routes
141 class AddressHandler(DictComposedSubHandler):
142 handler_help = u"Manage IP addresses"
143 _comp_subhandler_cont = 'devices'
144 _comp_subhandler_attr = 'addrs'
145 _comp_subhandler_class = Address
148 class DeviceHandler(SubHandler):
150 handler_help = u"Manage network devices"
152 def __init__(self, parent):
153 # FIXME remove templates to execute commands
154 from mako.template import Template
156 template_dir = path.join(path.dirname(__file__), 'templates')
157 dev_fn = path.join(template_dir, 'device')
158 self.device_template = Template(filename=dev_fn)
160 @handler(u'Bring the device up')
162 if name in self.parent.devices:
163 call(self.device_template.render(dev=name, action='up'), shell=True)
164 #bring up all the route asocitaed to the device
165 for route in self.parent.devices[name].routes:
167 call(self.parent._render_config('route_add', dict(
169 net_addr = route.net_addr,
170 prefix = route.prefix,
171 gateway = route.gateway,
174 except ExecutionError, e:
176 self.parent._bring_up_no_dev_routes()
177 self.parent._restart_services()
179 raise DeviceNotFoundError(name)
181 @handler(u'Bring the device down')
182 def down(self, name):
183 if name in self.parent.devices:
184 call(self.device_template.render(dev=name, action='down'), shell=True)
185 self.parent._bring_up_no_dev_routes()
186 self.parent._restart_services()
188 raise DeviceNotFoundError(name)
190 @handler(u'List all devices')
192 return self.parent.devices.keys()
194 @handler(u'Get information about a device')
196 return self.parent.devices.items()
198 class IpHandler(Restorable, ConfigWriter, TransactionalHandler):
200 handler_help = u"Manage IP devices, addresses, routes and hops"
202 _persistent_attrs = ('devices','hops','no_device_routes')
204 _restorable_defaults = dict(
205 devices=get_network_devices(),
207 no_device_routes = list(),
210 _config_writer_files = ('device', 'ip_add', 'ip_del', 'ip_flush',
211 'route_add', 'route_del', 'route_flush', 'hop')
212 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
214 def __init__(self, pickle_dir='.', config_dir='.'):
215 r"Initialize DhcpHandler object, see class documentation for details."
216 self._persistent_dir = pickle_dir
217 self._config_writer_cfg_dir = config_dir
218 self._config_build_templates()
221 self.addr = AddressHandler(self)
222 self.route = RouteHandler(self)
223 self.dev = DeviceHandler(self)
224 self.hop = HopHandler(self)
225 self.no_device_routes = list()
226 self.services = list()
228 def _write_config(self):
229 r"_write_config() -> None :: Execute all commands."
230 for device in self.devices.values():
232 self._write_config_for_device(device)
233 self._bring_up_no_dev_routes()
236 def _bring_up_no_dev_routes(self):
237 for route in self.no_device_routes:
239 call(self._render_config('route_add', dict(
241 net_addr = route.net_addr,
242 prefix = route.prefix,
243 gateway = route.gateway,
246 except ExecutionError, e:
249 def _write_hops(self):
250 r"_write_hops() -> None :: Execute all hops."
253 call('ip route del default', shell=True)
254 except ExecutionError, e:
257 #get hops for active devices
260 if h.device in self.devices:
261 if self.devices[h.device].active:
262 active_hops.append(h)
263 call(self._render_config('hop', dict(
267 except ExecutionError, e:
270 def _write_config_for_device(self, device):
271 r"_write_config_for_device(self, device) -> None :: Execute all commands for a device."
273 call(self._render_config('route_flush', dict(dev=device.name)), shell=True)
274 except ExecutionError, e:
277 call(self._render_config('ip_flush', dict(dev=device.name)), shell=True)
278 except ExecutionError, e:
280 for address in device.addrs.values():
281 broadcast = address.broadcast
282 if broadcast is None:
285 call(self._render_config('ip_add', dict(
288 netmask = address.netmask,
290 broadcast = broadcast,
293 except ExecutionError, e:
295 for route in device.routes:
297 call(self._render_config('route_add', dict(
299 net_addr = route.net_addr,
300 prefix = route.prefix,
301 gateway = route.gateway,
304 except ExecutionError, e:
307 def handle_timer(self):
308 self.refresh_devices()
311 def refresh_devices(self):
312 devices = get_network_devices()
313 #add not registered and active devices
315 for k,v in devices.items():
316 if k not in self.devices:
318 elif not self.devices[k].active:
321 self._write_config_for_device(self.devices[k])
324 self._bring_up_no_dev_routes()
325 self._restart_services()
327 #mark inactive devices
328 for k in self.devices.keys():
331 self.devices[k].active = False
334 self._bring_up_no_dev_routes()
336 def _restart_services(self):
337 for s in self.services:
338 if s._service_running:
341 except ExecutionError:
345 except ExecutionError:
348 #hooks a service to the ip handler, so when
349 #a device is brought up one can restart the service
350 #that need to refresh their device list
351 def device_up_hook(self, serv):
352 if hasattr(serv, 'stop') and hasattr(serv, 'start'):
353 self.services.append(serv)
359 if __name__ == '__main__':
362 print '----------------------'
363 ip.hop.add('201.21.32.53','eth0')
364 ip.hop.add('205.65.65.25','eth1')
367 ip.addr.add('eth0','192.168.0.23','24','192.168.255.255')
368 ip.addr.add('eth0','192.168.0.26','24')
370 ip.route.add('eth0','192.168.0.0','24','192.168.0.1')
371 ip.route.add('eth0','192.168.0.5','24','192.168.0.1')
373 ip.hop.delete('201.21.32.53','eth0')
374 ip.route.clear('eth0')