]> git.llucax.com Git - software/pymin.git/blobdiff - pymin/services/ppp/__init__.py
Add support for service running status and restoring at startup.
[software/pymin.git] / pymin / services / ppp / __init__.py
index 028eb912c316f95d22514192cdd963dfcd4e4f07..91070e79cc652441b6909e36cda6a06a31939aab 100644 (file)
@@ -4,23 +4,12 @@ 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
+from pymin.services.util import Restorable, ConfigWriter, ReloadHandler, \
+                                TransactionalHandler, DictSubHandler, call
 
-__ALL__ = ('PppHandler')
+__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):
+class ConnectionError(HandlerError, KeyError):
     r"""
     ConnectionError(hostname) -> ConnectionError instance
 
@@ -43,6 +32,7 @@ class Connection(Sequence):
         self.username = username
         self.password = password
         self.type = type
+        self._running = False
         if type == 'OE':
             if not 'device' in kw:
                 raise ConnectionError('Bad arguments for type=OE')
@@ -78,14 +68,14 @@ class ConnectionHandler(DictSubHandler):
 
     handler_help = u"Manages connections for the ppp service"
 
-    _dict_subhandler_attr = 'conns'
-    _dict_subhandler_class = Connection
+    _cont_subhandler_attr = 'conns'
+    _cont_subhandler_class = Connection
 
-class PppHandler(Restorable, ConfigWriter, TransactionalHandler):
+class PppHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler):
 
     handler_help = u"Manage ppp service"
 
-    _persistent_attrs = ('conns')
+    _persistent_attrs = ['conns']
 
     _restorable_defaults = dict(
         conns  = dict(),
@@ -100,30 +90,70 @@ class PppHandler(Restorable, ConfigWriter, TransactionalHandler):
         self._config_writer_cfg_dir = config_dir
         self._config_build_templates()
         self._restore()
+        for conn in self.conns.values():
+            if conn._running:
+                conn._running = False
+                self.start(conn.name)
         self.conn = ConnectionHandler(self)
 
-    @handler('Starts the service')
-    def start(self, name):
+    @handler(u'Start one or all the connections.')
+    def start(self, name=None):
+        names = [name]
+        if name is None:
+            names = self.conns.keys()
+        for name in names:
+            if name in self.conns:
+                if not self.conns[name]._running:
+                    call(('pon', name))
+                    self.conns[name]._running = True
+                    self._dump_attr('conns')
+            else:
+                raise ConnectionNotFoundError(name)
+
+    @handler(u'Stop one or all the connections.')
+    def stop(self, name=None):
+        names = [name]
+        if name is None:
+            names = self.conns.keys()
+        for name in names:
+            if name in self.conns:
+                if self.conns[name]._running:
+                    call(('poff', name))
+                    self.conns[name]._running = False
+                    self._dump_attr('conns')
+            else:
+                raise ConnectionNotFoundError(name)
+
+    @handler(u'Restart one or all the connections (even disconnected ones).')
+    def restart(self, name=None):
+        names = [name]
+        if name is None:
+            names = self.conns.keys()
+        for name in names:
+            self.stop(name)
+            self.start(name)
+
+    @handler(u'Restart only one or all the already running connections.')
+    def reload(self, name=None):
+        r"reload() -> None :: Reload the configuration of the service."
+        names = [name]
+        if name is None:
+            names = self.conns.keys()
+        for name in names:
+            if self.conns[name]._running:
+                self.stop(name)
+                self.start(name)
+
+    @handler(u'Tell if the service is running.')
+    def running(self, name=None):
+        r"reload() -> None :: Reload the configuration of the service."
+        if name is None:
+            return [c.name for c in self.conns.values() if c._running]
         if name in self.conns:
-            #call(('pon', name))
-            print ('pon', name)
+            return int(self.conns[name]._running)
         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."
         #guardo los pass que van el pap-secrets
@@ -154,4 +184,4 @@ if __name__ == '__main__':
     p.conn.add('ppptunnel_c','dominio\luca','luca',type='TUNNEL',server='192.168.0.23')
     p.commit()
     print p.conn.list()
-    print p.conn.show()
\ No newline at end of file
+    print p.conn.show()