From c8993fb7829ad17ea0936aa7cea2fceb8bcc8109 Mon Sep 17 00:00:00 2001 From: Nicolas Emiliani Date: Thu, 11 Oct 2007 13:54:46 -0300 Subject: [PATCH] Se agrega el manejo de ppp. --- config.py | 3 + pymin/services/__init__.py | 2 +- pymin/services/ppp/__init__.py | 124 +++++++++++++++++++++++ pymin/services/ppp/templates/nameX | 3 + pymin/services/ppp/templates/options.X | 1 + pymin/services/ppp/templates/pap-secrets | 3 + 6 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 pymin/services/ppp/__init__.py create mode 100644 pymin/services/ppp/templates/nameX create mode 100644 pymin/services/ppp/templates/options.X create mode 100644 pymin/services/ppp/templates/pap-secrets diff --git a/config.py b/config.py index 9f2d257..bf2a0e6 100644 --- a/config.py +++ b/config.py @@ -28,6 +28,9 @@ class Root(Handler): vrrp = VrrpHandler( pickle_dir = join(pickle_path, 'vrrp'), config_dir = join(config_path, 'vrrp')) + ppp = PppHandler( + pickle_dir = join(pickle_path, 'ppp'), + config_dir = join(config_path, 'ppp')) bind_addr = \ ( diff --git a/pymin/services/__init__.py b/pymin/services/__init__.py index 1d1ab1c..018633d 100644 --- a/pymin/services/__init__.py +++ b/pymin/services/__init__.py @@ -6,4 +6,4 @@ from pymin.services.firewall import FirewallHandler from pymin.services.ip import IpHandler from pymin.services.proxy import ProxyHandler from pymin.services.vrrp import VrrpHandler - +from pymin.services.ppp import PppHandler diff --git a/pymin/services/ppp/__init__.py b/pymin/services/ppp/__init__.py new file mode 100644 index 0000000..c579600 --- /dev/null +++ b/pymin/services/ppp/__init__.py @@ -0,0 +1,124 @@ +# vim: set encoding=utf-8 et sw=4 sts=4 : + +from os import path + +from pymin.seqtools import Sequence +from pymin.dispatcher import Handler, handler, HandlerError +from pymin.services.util import Restorable, ConfigWriter \ + ,TransactionalHandler, DictSubHandler, call + +__ALL__ = ('PppHandler') + +class Error(HandlerError): + r""" + Error(command) -> Error instance :: Base ConnectionHandler exception class. + + All exceptions raised by the ConnectionHandler inherits from this one, so you can + easily catch any ConnectionHandler exception. + + message - A descriptive error message. + """ + pass + +class ConnectionError(Error, KeyError): + r""" + ConnectionError(hostname) -> ConnectionError instance + + This is the base exception for all connection related errors. + """ + + def __init__(self, connection): + r"Initialize the object. See class documentation for more info." + self.message = u'Connection error: "%s"' % connection + +class ConnectionNotFoundError(ConnectionError): + def __init__(self, connection): + r"Initialize the object. See class documentation for more info." + self.message = u'Connection not found error: "%s"' % connection + +class Connection(Sequence): + + def __init__(self, name, dev, username=None, password=None): + self.name = name + self.dev = dev + self.username = username + self.password = password + + def as_tuple(self): + return (self.name, self.dev, self.username, self.password) + + def update(self, dev=None, username=None, password=None): + if dev is not None: + self.dev = dev + if username is not None: + self.username = username + if password is not None: + self.password = password + + +class ConnectionHandler(DictSubHandler): + + handler_help = u"Manages connections for the ppp service" + + _dict_subhandler_attr = 'conns' + _dict_subhandler_class = Connection + +class PppHandler(Restorable, ConfigWriter, TransactionalHandler): + + handler_help = u"Manage ppp service" + + _persistent_attrs = ('conns') + + _restorable_defaults = dict( + conns = dict(), + ) + + _config_writer_files = ('options.X','pap-secrets','nameX') + _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates') + + def __init__(self, pickle_dir='.', config_dir='.'): + r"Initialize Ppphandler object, see class documentation for details." + self._persistent_dir = pickle_dir + self._config_writer_cfg_dir = config_dir + self._config_build_templates() + self._restore() + self.conn = ConnectionHandler(self) + + @handler('Starts the service') + def start(self, name): + if name in self.conns: + #call(('pon', name)) + print ('pon', name) + else: + raise ConnectionNotFoundError(name) + + @handler('Stops the service') + def stop(self, name): + if name in self.conns: + #call(('poff', name)) + print ('poff', name) + else: + raise ConnectionNotFoundError(name) + + @handler('Reloads the service') + def reload(self): + for conn in self.conns.values(): + self.stop(conn.name) + self.start(conn.name) + + def _write_config(self): + r"_write_config() -> None :: Generate all the configuration files." + vars = dict(conns=self.conns) + self._write_single_config('pap-secrets','pap-secrets',vars) + for conn in self.conns.values(): + vars = dict(conn=conn) + self._write_single_config('nameX',conn.name, vars) + self._write_single_config('options.X','options.' + conn.name, vars) + + +if __name__ == '__main__': + p = PppHandler() + p.conn.add('test2','tty2','luca','luca') + p.commit() + print p.conn.list() + print p.conn.show() \ No newline at end of file diff --git a/pymin/services/ppp/templates/nameX b/pymin/services/ppp/templates/nameX new file mode 100644 index 0000000..17d3882 --- /dev/null +++ b/pymin/services/ppp/templates/nameX @@ -0,0 +1,3 @@ +name ${conn.username} +file /etc/ppp/options.${conn.name} +ipparam ${conn.name} \ No newline at end of file diff --git a/pymin/services/ppp/templates/options.X b/pymin/services/ppp/templates/options.X new file mode 100644 index 0000000..11cacbc --- /dev/null +++ b/pymin/services/ppp/templates/options.X @@ -0,0 +1 @@ +lock ${conn.dev} \ No newline at end of file diff --git a/pymin/services/ppp/templates/pap-secrets b/pymin/services/ppp/templates/pap-secrets new file mode 100644 index 0000000..fb5b37a --- /dev/null +++ b/pymin/services/ppp/templates/pap-secrets @@ -0,0 +1,3 @@ +% for conn in conns.values(): +${conn.username} * ${conn.password} +% endfor \ No newline at end of file -- 2.43.0