From: Nicolas Emiliani Date: Tue, 2 Oct 2007 16:11:13 +0000 (-0300) Subject: Se agrega el comando ip a la pymind y se corrige @handler X-Git-Url: https://git.llucax.com/software/pymin.git/commitdiff_plain/70672dbd46126ba2b5a60a034c0a8a13385d4510?ds=sidebyside;hp=--cc Se agrega el comando ip a la pymind y se corrige @handler Se agrega a la configuracion el comando ip y se modifican los handlers de dns e ip para que devuelvan con help --- 70672dbd46126ba2b5a60a034c0a8a13385d4510 diff --git a/config.py b/config.py index f135b23..f155fcb 100644 --- a/config.py +++ b/config.py @@ -22,6 +22,10 @@ routes = dict \ pickle_dir = 'var/lib/pymin/pickle/firewall', config_dir = 'var/lib/pymin/config/firewall', ), + ip = IpHandler( + pickle_dir = 'var/lib/pymin/pickle/ip', + config_dir = 'var/lib/pymin/config/ip', + ), ) bind_addr = \ diff --git a/services/__init__.py b/services/__init__.py index e1a505d..bd7c3c1 100644 --- a/services/__init__.py +++ b/services/__init__.py @@ -3,4 +3,5 @@ from services.dhcp import DhcpHandler from services.dns import DnsHandler from services.firewall import FirewallHandler +from services.ip import IpHandler diff --git a/services/dns/__init__.py b/services/dns/__init__.py index 6bc45e8..8f08d35 100644 --- a/services/dns/__init__.py +++ b/services/dns/__init__.py @@ -18,10 +18,14 @@ except ImportError: class Sequence: pass try: - from dispatcher import handler, HandlerError + from dispatcher import handler, HandlerError, Handler except ImportError: class HandlerError(RuntimeError): pass - def handler(f): return f # NOP for testing + class Handler: pass + def handler(help): + def wrapper(f): + return f + return wrapper @@ -232,11 +236,11 @@ class Host(Sequence): def as_tuple(self): return (self.name, self.ip) -class HostHandler: +class HostHandler(Handler): def __init__(self,zones): self.zones = zones - @handler + @handler(u'Adds a host to a zone') def add(self, name, hostname, ip): if not name in self.zones: raise ZoneNotFoundError(name) @@ -245,7 +249,7 @@ class HostHandler: self.zones[name].hosts[hostname] = Host(hostname, ip) self.zones[name].mod = True - @handler + @handler(u'Updates a host ip in a zone') def update(self, name, hostname, ip): if not name in self.zones: raise ZoneNotFoundError(name) @@ -254,7 +258,7 @@ class HostHandler: self.zones[name].hosts[hostname].ip = ip self.zones[name].mod = True - @handler + @handler(u'Deletes a host from a zone') def delete(self, name, hostname): if not name in self.zones: raise ZoneNotFoundError(name) @@ -263,11 +267,11 @@ class HostHandler: del self.zones[name].hosts[hostname] self.zones[name].mod = True - @handler + @handler(u'Lists hosts') def list(self): return self.zones.keys() - @handler + @handler(u'Get insormation about all hosts') def show(self): return self.zones.values() @@ -281,12 +285,12 @@ class MailExchange(Sequence): def as_tuple(self): return (self.mx, self.prio) -class MailExchangeHandler: +class MailExchangeHandler(Handler): def __init__(self, zones): self.zones = zones - @handler + @handler(u'Adds a mail exchange to a zone') def add(self, zonename, mx, prio): if not zonename in self.zones: raise ZoneNotFoundError(zonename) @@ -295,7 +299,7 @@ class MailExchangeHandler: self.zones[zonename].mxs[mx] = MailExchange(mx, prio) self.zones[zonename].mod = True - @handler + @handler(u'Updates a mail exchange priority') def update(self, zonename, mx, prio): if not zonename in self.zones: raise ZoneNotFoundError(zonename) @@ -304,7 +308,7 @@ class MailExchangeHandler: self.zones[zonename].mxs[mx].prio = prio self.zones[zonename].mod = True - @handler + @handler(u'Deletes a mail exchange from a zone') def delete(self, zonename, mx): if not zonename in self.zones: raise ZoneNotFoundError(zonename) @@ -313,11 +317,11 @@ class MailExchangeHandler: del self.zones[zonename].mxs[mx] self.zones[zonename].mod = True - @handler + @handler(u'Lists mail exchangers') def list(self): return self.zones.keys() - @handler + @handler(u'Get information about all mail exchangers') def show(self): return self.zones.values() @@ -330,12 +334,12 @@ class NameServer(Sequence): def as_tuple(self): return (self.name) -class NameServerHandler: +class NameServerHandler(Handler): def __init__(self, zones): self.zones = zones - @handler + @handler(u'Adds a name server to a zone') def add(self, zone, ns): if not zone in self.zones: raise ZoneNotFoundError(zone) @@ -344,7 +348,7 @@ class NameServerHandler: self.zones[zone].nss[ns] = NameServer(ns) self.zones[zone].mod = True - @handler + @handler(u'Deletes a name server from a zone') def delete(self, zone, ns): if not zone in self.zones: raise ZoneNotFoundError(zone) @@ -353,11 +357,11 @@ class NameServerHandler: del self.zones[zone].nss[ns] self.zones[zone].mod = True - @handler + @handler(u'Lists name servers') def list(self): return self.zones.keys() - @handler + @handler(u'Get information about all name servers') def show(self): return self.zones.values() @@ -375,7 +379,7 @@ class Zone(Sequence): def as_tuple(self): return (self.name, self.hosts, self.mxs, self.nss) -class ZoneHandler: +class ZoneHandler(Handler): r"""ZoneHandler(zones) -> ZoneHandler instance :: Handle a list of zones. @@ -387,10 +391,10 @@ class ZoneHandler: def __init__(self, zones): self.zones = zones - @handler + @handler(u'Adds a zone') def add(self, name): if name in self.zones: - if self.zones[name].dele = True: + if self.zones[name].dele == True: self.zones[name].dele = False else: raise ZoneAlreadyExistsError(name) @@ -399,22 +403,22 @@ class ZoneHandler: self.zones[name].new = True - @handler + @handler(u'Deletes a zone') def delete(self, name): r"delete(name) -> None :: Delete a zone from the zone list." if not name in self.zones: raise ZoneNotFoundError(name) self.zones[name].dele = True - @handler + @handler(u'Lists zones') def list(self): return self.zones.keys() - @handler + @handler(u'Get information about all zones') def show(self): return self.zones.values() -class DnsHandler: +class DnsHandler(Handler): r"""DnsHandler([pickle_dir[, config_dir]]) -> DnsHandler instance. Handles DNS service commands for the dns program. @@ -451,7 +455,7 @@ class DnsHandler: self.ns = NameServerHandler(self.zones) self.mod = False - @handler + @handler(u'Set a DNS parameter') def set(self, param, value): r"set(param, value) -> None :: Set a DNS parameter." if not param in self.vars: @@ -459,50 +463,50 @@ class DnsHandler: self.vars[param] = value self.mod = True - @handler + @handler(u'Get a DNS parameter') def get(self, param): r"get(param) -> None :: Get a DNS parameter." if not param in self.vars: raise ParameterNotFoundError(param) return self.vars[param] - @handler + @handler(u'List DNS parameters') def list(self): return self.vars.keys() - @handler + @handler(u'Get all DNS parameters, with their values.') def show(self): return self.vars.values() - @handler + @handler(u'Start the service.') def start(self): r"start() -> None :: Start the DNS service." #esto seria para poner en una interfaz #y seria el hook para arrancar el servicio pass - @handler + @handler(u'Stop the service.') def stop(self): r"stop() -> None :: Stop the DNS service." #esto seria para poner en una interfaz #y seria el hook para arrancar el servicio pass - @handler + @handler(u'Restart the service.') def restart(self): r"restart() -> None :: Restart the DNS service." #esto seria para poner en una interfaz #y seria el hook para arrancar el servicio pass - @handler + @handler(u'Reload the service config (without restarting, if possible)') def reload(self): r"reload() -> None :: Reload the configuration of the DNS service." #esto seria para poner en una interfaz #y seria el hook para arrancar el servicio pass - @handler + @handler(u'Commit the changes (reloading the service, if necessary).') def commit(self): r"commit() -> None :: Commit the changes and reload the DNS service." #esto seria para poner en una interfaz @@ -512,7 +516,7 @@ class DnsHandler: self._write_config() self.reload() - @handler + @handler(u'Discard all the uncommited changes.') def rollback(self): r"rollback() -> None :: Discard the changes not yet commited." self._load() diff --git a/services/ip/__init__.py b/services/ip/__init__.py index 9152ba4..bc8b275 100644 --- a/services/ip/__init__.py +++ b/services/ip/__init__.py @@ -15,13 +15,18 @@ except ImportError: # NOP for testing class Sequence: pass try: - from dispatcher import handler, HandlerError + from dispatcher import handler, HandlerError, Handler except ImportError: # NOP for testing class HandlerError(RuntimeError): pass - def handler(f): return f + class Handler: pass + def handler(help): + def wrapper(f): + return f + return wrapper -__ALL__ = ('IpHandler',) +__ALL__ = ('IpHandler','Error','DeviceError','DeviceNotFoundError','RouteError','RouteNotFoundError', + 'RouteAlreadyExistsError','AddressError','AddressNotFoundError','AddressAlreadyExistsError') pickle_ext = '.pkl' pickle_devices = 'devs' @@ -112,12 +117,12 @@ class Route(Sequence): return 0 return cmp(id(self), id(other)) -class RouteHandler: +class RouteHandler(Handler): def __init__(self, devices): self.devices = devices - @handler + @handler(u'Adds a route to a device') def add(self, device, net_addr, prefix, gateway): if not device in self.devices: raise DeviceNotFoundError(device) @@ -128,7 +133,7 @@ class RouteHandler: except ValueError: self.devices[device].routes.append(r) - @handler + @handler(u'Deletes a route from a device') def delete(self, device, net_addr, prefix, gateway): if not device in self.devices: raise DeviceNotFoundError(device) @@ -138,14 +143,14 @@ class RouteHandler: except ValueError: raise RouteNotFoundError(net_addr + '/' + prefix + '->' + gateway) - @handler + @handler(u'Flushes routes from a device') def flush(self, device): if not device in self.devices: raise DeviceNotFoundError(device) self.devices[device].routes = list() - @handler + @handler(u'List routes') def list(self, device): try: k = self.devices[device].routes.keys() @@ -153,7 +158,7 @@ class RouteHandler: k = list() return k - @handler + @handler(u'Get information about all routes') def show(self): try: k = self.devices[device].routes.values() @@ -171,12 +176,12 @@ class Address(Sequence): def as_tuple(self): return (self.ip, self.prefix, self.broadcast) -class AddressHandler: +class AddressHandler(Handler): def __init__(self, devices): self.devices = devices - @handler + @handler(u'Adds an address to a device') def add(self, device, ip, prefix, broadcast='+'): if not device in self.devices: raise DeviceNotFoundError(device) @@ -184,7 +189,7 @@ class AddressHandler: raise AddressAlreadyExistsError(ip) self.devices[device].addrs[ip] = Address(ip, prefix, broadcast) - @handler + @handler(u'Deletes an address from a device') def delete(self, device, ip): if not device in self.devices: raise DeviceNotFoundError(device) @@ -192,13 +197,13 @@ class AddressHandler: raise AddressNotFoundError(ip) del self.devices[device].addrs[ip] - @handler + @handler(u'Flushes addresses from a device') def flush(self, device): if not device in self.devices: raise DeviceNotFoundError(device) self.devices[device].addrs = dict() - @handler + @handler(u'List all addresses from a device') def list(self, device): try: k = self.devices[device].addrs.keys() @@ -206,7 +211,7 @@ class AddressHandler: k = list() return k - @handler + @handler(u'Get information about addresses from a device') def show(self, device): try: k = self.devices[device].addrs.values() @@ -225,36 +230,36 @@ class Device(Sequence): def as_tuple(self): return (self.name, self.mac) -class DeviceHandler: +class DeviceHandler(Handler): def __init__(self, devices): self.devices = devices dev_fn = path.join(template_dir, device_com) self.device_template = Template(filename=dev_fn) - @handler + @handler(u'Bring the device up') def up(self, name): if name in self.devices: print self.device_template.render(dev=name, action='up') else: raise DeviceNotFoundError(name) - @handler + @handler(u'Bring the device down') def down(self, name): if name in self.devices: print self.device_template.render(dev=name, action='down') else: raise DeviceNotFoundError(name) - @handler + @handler(u'List all devices') def list(self): return self.devices.keys() - @handler + @handler(u'Get information about a device') def show(self): return self.devices.items() -class IpHandler: +class IpHandler(Handler): def __init__(self, pickle_dir='.', config_dir='.'): r"Initialize DhcpHandler object, see class documentation for details." @@ -282,7 +287,7 @@ class IpHandler: p = Popen('ip link list', shell=True, stdout=PIPE, close_fds=True) devs = _get_devices(p.stdout.read()) self.devices = dict() - for eth, mac in devs.values(): + for eth, mac in devs: self.devices[eth] = Device(eth, mac) self._dump() self.addr = AddressHandler(self.devices) @@ -290,7 +295,7 @@ class IpHandler: self.dev = DeviceHandler(self.devices) self.commit() - @handler + @handler(u'Commit the changes (reloading the service, if necessary).') def commit(self): r"commit() -> None :: Commit the changes and reload the DHCP service." #esto seria para poner en una interfaz @@ -299,7 +304,7 @@ class IpHandler: self._dump() self._write_config() - @handler + @handler(u'Discard all the uncommited changes.') def rollback(self): r"rollback() -> None :: Discard the changes not yet commited." self._load() @@ -352,16 +357,16 @@ class IpHandler: gateway=route.gateway ) - def _get_devices(string): - l = list() - i = string.find('eth') - while i != -1: - eth = string[i:i+4] - m = string.find('link/ether', i+4) - mac = string[ m+11 : m+11+17] - l.append((eth,mac)) - i = string.find('eth', m+11+17) - return l +def _get_devices(string): + l = list() + i = string.find('eth') + while i != -1: + eth = string[i:i+4] + m = string.find('link/ether', i+4) + mac = string[ m+11 : m+11+17] + l.append((eth,mac)) + i = string.find('eth', m+11+17) + return l if __name__ == '__main__':