1 # vim: set encoding=utf-8 et sw=4 sts=4 :
4 import logging ; log = logging.getLogger('pymin.services.ip')
6 from pymin.dispatcher import handler
7 from pymin.service.util import SubHandler, call, ContainerNotFoundError, \
10 __all__ = ('DeviceHandler',)
13 class DeviceHandler(SubHandler):
15 handler_help = u"Manage network devices"
17 def __init__(self, parent):
18 log.debug(u'DeviceHandler(%r)', parent)
19 # FIXME remove templates to execute commands
20 from mako.template import Template
22 template_dir = path.join(path.dirname(__file__), 'templates')
23 dev_fn = path.join(template_dir, 'device')
24 self.device_template = Template(filename=dev_fn)
26 @handler(u'Bring the device up')
28 log.debug(u'DeviceHandler.up(%r)', name)
29 if name in self.parent.devices:
30 call(self.device_template.render(dev=name, action='up'), shell=True)
31 #bring up all the route asocitaed to the device
32 for route in self.parent.devices[name].routes:
34 log.debug(u'IpHandler.up: adding %r', route)
35 call(self.parent._render_config('route_add', dict(
37 net_addr = route.net_addr,
38 prefix = route.prefix,
39 gateway = route.gateway,
42 except ExecutionError, e:
43 log.debug(u'IpHandler.up: error adding %r -> %r', route, e)
44 self.parent._bring_up_no_dev_routes()
45 self.parent._restart_services()
47 log.debug(u'DeviceHandler.up: device not found')
48 raise ContainerNotFoundError(name)
50 @handler(u'Bring the device down')
52 log.debug(u'DeviceHandler.down(%r)', name)
53 if name in self.parent.devices:
54 call(self.device_template.render(dev=name, action='down'), shell=True)
55 self.parent._bring_up_no_dev_routes()
56 self.parent._restart_services()
58 log.debug(u'DeviceHandler.up: device not found')
59 raise ContainerNotFoundError(name)
61 @handler(u'List all devices')
63 log.debug(u'DeviceHandler.list()')
64 return self.parent.devices.keys()
66 @handler(u'Get information about a device')
68 log.debug(u'DeviceHandler.show()')
69 return self.parent.devices.items()