]> git.llucax.com Git - software/pymin.git/blob - services/ip/dev.py
Add common Int related validators (refs #20)
[software/pymin.git] / services / ip / dev.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from os import path
4 import logging ; log = logging.getLogger('pymin.services.ip')
5
6 from pymin.dispatcher import handler
7 from pymin.service.util import SubHandler, call, ContainerNotFoundError, \
8                                ExecutionError
9
10 __all__ = ('DeviceHandler',)
11
12
13 class DeviceHandler(SubHandler):
14
15     handler_help = u"Manage network devices"
16
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
21         self.parent = parent
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)
25
26     @handler(u'Bring the device up')
27     def up(self, name):
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:
33                 try:
34                     log.debug(u'IpHandler.up: adding %r', route)
35                     call(self.parent._render_config('route_add', dict(
36                             dev = name,
37                             net_addr = route.net_addr,
38                             prefix = route.prefix,
39                             gateway = route.gateway,
40                         )
41                     ), shell=True)
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()
46         else:
47             log.debug(u'DeviceHandler.up: device not found')
48             raise ContainerNotFoundError(name)
49
50     @handler(u'Bring the device down')
51     def down(self, name):
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()
57         else:
58             log.debug(u'DeviceHandler.up: device not found')
59             raise ContainerNotFoundError(name)
60
61     @handler(u'List all devices')
62     def list(self):
63         log.debug(u'DeviceHandler.list()')
64         return self.parent.devices.keys()
65
66     @handler(u'Get information about a device')
67     def show(self):
68         log.debug(u'DeviceHandler.show()')
69         return self.parent.devices.items()
70