from pymin.seqtools import Sequence
from pymin.dispatcher import handler, HandlerError, Handler
from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
- TransactionalHandler
+ TransactionalHandler, call
__ALL__ = ('IpHandler', 'Error','DeviceError', 'DeviceNotFoundError',
'RouteError', 'RouteNotFoundError', 'RouteAlreadyExistsError',
message - A descriptive error message.
"""
-
- def __init__(self, message):
- r"Initialize the Error object. See class documentation for more info."
- self.message = message
-
- def __str__(self):
- return self.message
+ pass
class DeviceError(Error):
def __init__(self, device):
- self.message = 'Device error : "%s"' % device
+ self.message = u'Device error : "%s"' % device
class DeviceNotFoundError(DeviceError):
def __init__(self, device):
- self.message = 'Device not found : "%s"' % device
+ self.message = u'Device not found : "%s"' % device
class AddressError(Error):
def __init__(self, addr):
- self.message = 'Address error : "%s"' % addr
+ self.message = u'Address error : "%s"' % addr
class AddressNotFoundError(AddressError):
def __init__(self, address):
- self.message = 'Address not found : "%s"' % address
+ self.message = u'Address not found : "%s"' % address
class AddressAlreadyExistsError(AddressError):
def __init__(self, address):
- self.message = 'Address already exists : "%s"' % address
+ self.message = u'Address already exists : "%s"' % address
class RouteError(Error):
def __init__(self, route):
- self.message = 'Route error : "%s"' % route
+ self.message = u'Route error : "%s"' % route
class RouteNotFoundError(RouteError):
def __init__(self, route):
- self.message = 'Route not found : "%s"' % route
+ self.message = u'Route not found : "%s"' % route
class RouteAlreadyExistsError(RouteError):
def __init__(self, route):
- self.message = 'Route already exists : "%s"' % route
+ self.message = u'Route already exists : "%s"' % route
class Route(Sequence):
class RouteHandler(Handler):
+ handler_help = u"Manage IP routes"
+
def __init__(self, devices):
self.devices = devices
class AddressHandler(Handler):
+ handler_help = u"Manage IP addresses"
+
def __init__(self, devices):
self.devices = devices
class DeviceHandler(Handler):
+ handler_help = u"Manage network devices"
+
def __init__(self, devices):
# FIXME remove templates to execute commands
from mako.template import Template
@handler(u'Bring the device up')
def up(self, name):
if name in self.devices:
- print self.device_template.render(dev=name, action='up')
+ call(self.device_template.render(dev=name, action='up'), shell=True)
else:
raise DeviceNotFoundError(name)
@handler(u'Bring the device down')
def down(self, name):
if name in self.devices:
- print self.device_template.render(dev=name, action='down')
+ call(self.device_template.render(dev=name, action='down'), shell=True)
else:
raise DeviceNotFoundError(name)
class IpHandler(Restorable, ConfigWriter, TransactionalHandler):
+ handler_help = u"Manage IP devices, addresses and routes"
+
_persistent_attrs = 'devices'
_restorable_defaults = dict(devices=get_devices())
def _write_config(self):
r"_write_config() -> None :: Execute all commands."
for device in self.devices.values():
- print self._render_config('route_flush', dict(dev=device.name))
- print self._render_config('ip_flush', dict(dev=device.name))
+ call(self._render_config('route_flush', dict(dev=device.name)), shell=True)
+ call(self._render_config('ip_flush', dict(dev=device.name)), shell=True)
for address in device.addrs.values():
- print self._render_config('ip_add', dict(
+ call(self._render_config('ip_add', dict(
dev = device.name,
addr = address.ip,
prefix = address.prefix,
broadcast = address.broadcast,
)
- )
+ ), shell=True)
for route in device.routes:
- print self._render_config('route_add', dict(
+ call(self._render_config('route_add', dict(
dev = device.name,
net_addr = route.net_addr,
prefix = route.prefix,
gateway = route.gateway,
)
- )
+ ), shell=True)
if __name__ == '__main__':