Se agrega a la configuracion el comando ip y se modifican
los handlers de dns e ip para que devuelvan con help
pickle_dir = 'var/lib/pymin/pickle/firewall',
config_dir = 'var/lib/pymin/config/firewall',
),
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',
+ ),
from services.dhcp import DhcpHandler
from services.dns import DnsHandler
from services.firewall import FirewallHandler
from services.dhcp import DhcpHandler
from services.dns import DnsHandler
from services.firewall import FirewallHandler
+from services.ip import IpHandler
class Sequence: pass
try:
class Sequence: pass
try:
- from dispatcher import handler, HandlerError
+ from dispatcher import handler, HandlerError, Handler
except ImportError:
class HandlerError(RuntimeError): pass
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
def as_tuple(self):
return (self.name, self.ip)
def as_tuple(self):
return (self.name, self.ip)
+class HostHandler(Handler):
def __init__(self,zones):
self.zones = zones
def __init__(self,zones):
self.zones = zones
+ @handler(u'Adds a host to a zone')
def add(self, name, hostname, ip):
if not name in self.zones:
raise ZoneNotFoundError(name)
def add(self, name, hostname, ip):
if not name in self.zones:
raise ZoneNotFoundError(name)
self.zones[name].hosts[hostname] = Host(hostname, ip)
self.zones[name].mod = True
self.zones[name].hosts[hostname] = Host(hostname, ip)
self.zones[name].mod = True
+ @handler(u'Updates a host ip in a zone')
def update(self, name, hostname, ip):
if not name in self.zones:
raise ZoneNotFoundError(name)
def update(self, name, hostname, ip):
if not name in self.zones:
raise ZoneNotFoundError(name)
self.zones[name].hosts[hostname].ip = ip
self.zones[name].mod = True
self.zones[name].hosts[hostname].ip = ip
self.zones[name].mod = True
+ @handler(u'Deletes a host from a zone')
def delete(self, name, hostname):
if not name in self.zones:
raise ZoneNotFoundError(name)
def delete(self, name, hostname):
if not name in self.zones:
raise ZoneNotFoundError(name)
del self.zones[name].hosts[hostname]
self.zones[name].mod = True
del self.zones[name].hosts[hostname]
self.zones[name].mod = True
+ @handler(u'Lists hosts')
def list(self):
return self.zones.keys()
def list(self):
return self.zones.keys()
+ @handler(u'Get insormation about all hosts')
def show(self):
return self.zones.values()
def show(self):
return self.zones.values()
def as_tuple(self):
return (self.mx, self.prio)
def as_tuple(self):
return (self.mx, self.prio)
-class MailExchangeHandler:
+class MailExchangeHandler(Handler):
def __init__(self, zones):
self.zones = zones
def __init__(self, zones):
self.zones = zones
+ @handler(u'Adds a mail exchange to a zone')
def add(self, zonename, mx, prio):
if not zonename in self.zones:
raise ZoneNotFoundError(zonename)
def add(self, zonename, mx, prio):
if not zonename in self.zones:
raise ZoneNotFoundError(zonename)
self.zones[zonename].mxs[mx] = MailExchange(mx, prio)
self.zones[zonename].mod = True
self.zones[zonename].mxs[mx] = MailExchange(mx, prio)
self.zones[zonename].mod = True
+ @handler(u'Updates a mail exchange priority')
def update(self, zonename, mx, prio):
if not zonename in self.zones:
raise ZoneNotFoundError(zonename)
def update(self, zonename, mx, prio):
if not zonename in self.zones:
raise ZoneNotFoundError(zonename)
self.zones[zonename].mxs[mx].prio = prio
self.zones[zonename].mod = True
self.zones[zonename].mxs[mx].prio = prio
self.zones[zonename].mod = True
+ @handler(u'Deletes a mail exchange from a zone')
def delete(self, zonename, mx):
if not zonename in self.zones:
raise ZoneNotFoundError(zonename)
def delete(self, zonename, mx):
if not zonename in self.zones:
raise ZoneNotFoundError(zonename)
del self.zones[zonename].mxs[mx]
self.zones[zonename].mod = True
del self.zones[zonename].mxs[mx]
self.zones[zonename].mod = True
+ @handler(u'Lists mail exchangers')
def list(self):
return self.zones.keys()
def list(self):
return self.zones.keys()
+ @handler(u'Get information about all mail exchangers')
def show(self):
return self.zones.values()
def show(self):
return self.zones.values()
def as_tuple(self):
return (self.name)
def as_tuple(self):
return (self.name)
-class NameServerHandler:
+class NameServerHandler(Handler):
def __init__(self, zones):
self.zones = zones
def __init__(self, zones):
self.zones = zones
+ @handler(u'Adds a name server to a zone')
def add(self, zone, ns):
if not zone in self.zones:
raise ZoneNotFoundError(zone)
def add(self, zone, ns):
if not zone in self.zones:
raise ZoneNotFoundError(zone)
self.zones[zone].nss[ns] = NameServer(ns)
self.zones[zone].mod = True
self.zones[zone].nss[ns] = NameServer(ns)
self.zones[zone].mod = True
+ @handler(u'Deletes a name server from a zone')
def delete(self, zone, ns):
if not zone in self.zones:
raise ZoneNotFoundError(zone)
def delete(self, zone, ns):
if not zone in self.zones:
raise ZoneNotFoundError(zone)
del self.zones[zone].nss[ns]
self.zones[zone].mod = True
del self.zones[zone].nss[ns]
self.zones[zone].mod = True
+ @handler(u'Lists name servers')
def list(self):
return self.zones.keys()
def list(self):
return self.zones.keys()
+ @handler(u'Get information about all name servers')
def show(self):
return self.zones.values()
def show(self):
return self.zones.values()
def as_tuple(self):
return (self.name, self.hosts, self.mxs, self.nss)
def as_tuple(self):
return (self.name, self.hosts, self.mxs, self.nss)
+class ZoneHandler(Handler):
r"""ZoneHandler(zones) -> ZoneHandler instance :: Handle a list of zones.
r"""ZoneHandler(zones) -> ZoneHandler instance :: Handle a list of zones.
def __init__(self, zones):
self.zones = zones
def __init__(self, zones):
self.zones = zones
+ @handler(u'Adds a zone')
def add(self, name):
if name in self.zones:
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)
self.zones[name].dele = False
else:
raise ZoneAlreadyExistsError(name)
self.zones[name].new = True
self.zones[name].new = True
+ @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
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(u'Lists zones')
def list(self):
return self.zones.keys()
def list(self):
return self.zones.keys()
+ @handler(u'Get information about all zones')
def show(self):
return self.zones.values()
def show(self):
return self.zones.values()
+class DnsHandler(Handler):
r"""DnsHandler([pickle_dir[, config_dir]]) -> DnsHandler instance.
Handles DNS service commands for the dns program.
r"""DnsHandler([pickle_dir[, config_dir]]) -> DnsHandler instance.
Handles DNS service commands for the dns program.
self.ns = NameServerHandler(self.zones)
self.mod = False
self.ns = NameServerHandler(self.zones)
self.mod = False
+ @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:
def set(self, param, value):
r"set(param, value) -> None :: Set a DNS parameter."
if not param in self.vars:
self.vars[param] = value
self.mod = True
self.vars[param] = value
self.mod = True
+ @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]
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(u'List DNS parameters')
def list(self):
return self.vars.keys()
def list(self):
return self.vars.keys()
+ @handler(u'Get all DNS parameters, with their values.')
def show(self):
return self.vars.values()
def show(self):
return self.vars.values()
+ @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
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(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
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(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
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(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
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(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
def commit(self):
r"commit() -> None :: Commit the changes and reload the DNS service."
#esto seria para poner en una interfaz
self._write_config()
self.reload()
self._write_config()
self.reload()
+ @handler(u'Discard all the uncommited changes.')
def rollback(self):
r"rollback() -> None :: Discard the changes not yet commited."
self._load()
def rollback(self):
r"rollback() -> None :: Discard the changes not yet commited."
self._load()
# NOP for testing
class Sequence: pass
try:
# 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
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'
pickle_ext = '.pkl'
pickle_devices = 'devs'
return 0
return cmp(id(self), id(other))
return 0
return cmp(id(self), id(other))
+class RouteHandler(Handler):
def __init__(self, devices):
self.devices = devices
def __init__(self, devices):
self.devices = devices
+ @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)
def add(self, device, net_addr, prefix, gateway):
if not device in self.devices:
raise DeviceNotFoundError(device)
except ValueError:
self.devices[device].routes.append(r)
except ValueError:
self.devices[device].routes.append(r)
+ @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)
def delete(self, device, net_addr, prefix, gateway):
if not device in self.devices:
raise DeviceNotFoundError(device)
except ValueError:
raise RouteNotFoundError(net_addr + '/' + prefix + '->' + gateway)
except ValueError:
raise RouteNotFoundError(net_addr + '/' + prefix + '->' + gateway)
+ @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()
def flush(self, device):
if not device in self.devices:
raise DeviceNotFoundError(device)
self.devices[device].routes = list()
+ @handler(u'List routes')
def list(self, device):
try:
k = self.devices[device].routes.keys()
def list(self, device):
try:
k = self.devices[device].routes.keys()
+ @handler(u'Get information about all routes')
def show(self):
try:
k = self.devices[device].routes.values()
def show(self):
try:
k = self.devices[device].routes.values()
def as_tuple(self):
return (self.ip, self.prefix, self.broadcast)
def as_tuple(self):
return (self.ip, self.prefix, self.broadcast)
+class AddressHandler(Handler):
def __init__(self, devices):
self.devices = devices
def __init__(self, devices):
self.devices = devices
+ @handler(u'Adds an address to a device')
def add(self, device, ip, prefix, broadcast='+'):
if not device in self.devices:
raise DeviceNotFoundError(device)
def add(self, device, ip, prefix, broadcast='+'):
if not device in self.devices:
raise DeviceNotFoundError(device)
raise AddressAlreadyExistsError(ip)
self.devices[device].addrs[ip] = Address(ip, prefix, broadcast)
raise AddressAlreadyExistsError(ip)
self.devices[device].addrs[ip] = Address(ip, prefix, broadcast)
+ @handler(u'Deletes an address from a device')
def delete(self, device, ip):
if not device in self.devices:
raise DeviceNotFoundError(device)
def delete(self, device, ip):
if not device in self.devices:
raise DeviceNotFoundError(device)
raise AddressNotFoundError(ip)
del self.devices[device].addrs[ip]
raise AddressNotFoundError(ip)
del self.devices[device].addrs[ip]
+ @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()
def flush(self, device):
if not device in self.devices:
raise DeviceNotFoundError(device)
self.devices[device].addrs = dict()
+ @handler(u'List all addresses from a device')
def list(self, device):
try:
k = self.devices[device].addrs.keys()
def list(self, device):
try:
k = self.devices[device].addrs.keys()
+ @handler(u'Get information about addresses from a device')
def show(self, device):
try:
k = self.devices[device].addrs.values()
def show(self, device):
try:
k = self.devices[device].addrs.values()
def as_tuple(self):
return (self.name, self.mac)
def as_tuple(self):
return (self.name, self.mac)
+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)
def __init__(self, devices):
self.devices = devices
dev_fn = path.join(template_dir, device_com)
self.device_template = Template(filename=dev_fn)
+ @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)
def up(self, name):
if name in self.devices:
print self.device_template.render(dev=name, action='up')
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')
else:
raise DeviceNotFoundError(name)
def down(self, name):
if name in self.devices:
print self.device_template.render(dev=name, action='down')
else:
raise DeviceNotFoundError(name)
+ @handler(u'List all devices')
def list(self):
return self.devices.keys()
def list(self):
return self.devices.keys()
+ @handler(u'Get information about a device')
def show(self):
return self.devices.items()
def show(self):
return self.devices.items()
+class IpHandler(Handler):
def __init__(self, pickle_dir='.', config_dir='.'):
r"Initialize DhcpHandler object, see class documentation for details."
def __init__(self, pickle_dir='.', config_dir='.'):
r"Initialize DhcpHandler object, see class documentation for details."
p = Popen('ip link list', shell=True, stdout=PIPE, close_fds=True)
devs = _get_devices(p.stdout.read())
self.devices = dict()
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():
self.devices[eth] = Device(eth, mac)
self._dump()
self.addr = AddressHandler(self.devices)
self.devices[eth] = Device(eth, mac)
self._dump()
self.addr = AddressHandler(self.devices)
self.dev = DeviceHandler(self.devices)
self.commit()
self.dev = DeviceHandler(self.devices)
self.commit()
+ @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
def commit(self):
r"commit() -> None :: Commit the changes and reload the DHCP service."
#esto seria para poner en una interfaz
self._dump()
self._write_config()
self._dump()
self._write_config()
+ @handler(u'Discard all the uncommited changes.')
def rollback(self):
r"rollback() -> None :: Discard the changes not yet commited."
self._load()
def rollback(self):
r"rollback() -> None :: Discard the changes not yet commited."
self._load()
- 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__':
if __name__ == '__main__':