]> git.llucax.com Git - software/pymin.git/blobdiff - pymin/services/ppp/__init__.py
Convert Rule to Item for validation in firewall service handler.
[software/pymin.git] / pymin / services / ppp / __init__.py
index 2115d65de3ec08a0b08b95d6917a0ed4dd246b07..6ee9f5a97218ae0ae5e994c601207968c3fc79e3 100644 (file)
@@ -4,13 +4,14 @@ import os
 import subprocess
 from os import path
 from signal import SIGTERM
 import subprocess
 from os import path
 from signal import SIGTERM
+import logging ; log = logging.getLogger('pymin.services.ppp')
 
 from pymin.seqtools import Sequence
 from pymin.dispatcher import Handler, handler, HandlerError
 from pymin.services.util import Restorable, ConfigWriter, ReloadHandler, \
                                 TransactionalHandler, DictSubHandler, call
 
 
 from pymin.seqtools import Sequence
 from pymin.dispatcher import Handler, handler, HandlerError
 from pymin.services.util import Restorable, ConfigWriter, ReloadHandler, \
                                 TransactionalHandler, DictSubHandler, call
 
-__ALL__ = ('PppHandler',)
+__all__ = ('PppHandler',)
 
 class ConnectionError(HandlerError, KeyError):
     r"""
 
 class ConnectionError(HandlerError, KeyError):
     r"""
@@ -89,52 +90,69 @@ class PppHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler):
 
     def __init__(self, pickle_dir='.', config_dir='.'):
         r"Initialize Ppphandler object, see class documentation for details."
 
     def __init__(self, pickle_dir='.', config_dir='.'):
         r"Initialize Ppphandler object, see class documentation for details."
+        log.debug(u'PppHandler(%r, %r)', pickle_dir, config_dir)
         self._persistent_dir = pickle_dir
         self._config_writer_cfg_dir = config_dir
         self._config_build_templates()
         self._restore()
         self._persistent_dir = pickle_dir
         self._config_writer_cfg_dir = config_dir
         self._config_build_templates()
         self._restore()
+        log.debug(u'PppHandler(): restoring connections...')
         for conn in self.conns.values():
             if conn._running:
         for conn in self.conns.values():
             if conn._running:
+                log.debug(u'PppHandler(): starting connection %r', conn.name)
                 conn._running = False
                 self.start(conn.name)
         self.conn = ConnectionHandler(self)
 
     @handler(u'Start one or all the connections.')
     def start(self, name=None):
                 conn._running = False
                 self.start(conn.name)
         self.conn = ConnectionHandler(self)
 
     @handler(u'Start one or all the connections.')
     def start(self, name=None):
+        log.debug(u'PppHandler.start(%r)', name)
         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:
         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:
+                    log.debug(u'PppHandler.start: starting connection %r', name)
                     call(('pppd', 'call', name))
                     self.conns[name]._running = True
                     self._dump_attr('conns')
             else:
                     call(('pppd', 'call', name))
                     self.conns[name]._running = True
                     self._dump_attr('conns')
             else:
+                log.debug(u'PppHandler.start: connection not found')
                 raise ConnectionNotFoundError(name)
 
     @handler(u'Stop one or all the connections.')
     def stop(self, name=None):
                 raise ConnectionNotFoundError(name)
 
     @handler(u'Stop one or all the connections.')
     def stop(self, name=None):
+        log.debug(u'PppHandler.stop(%r)', name)
+        names = [name]
         names = [name]
         if name is None:
             names = self.conns.keys()
         for name in names:
             if name in self.conns:
                 if self.conns[name]._running:
         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))
-                    if path.exists('/var/run/ppp-' + name + '.pid'):
-                        pid = file('/var/run/ppp-' + name + '.pid').readline()
+                    pid_file = '/var/run/ppp-' + name + '.pid'
+                    log.debug(u'PppHandler.stop: getting pid from %r', pid_file)
+                    if path.exists(pid_file):
+                        pid = file(pid_file).readline()
+                        pid = int(pid.strip())
                         try:
                         try:
-                            os.kill(int(pid.strip()), SIGTERM)
-                        except OSError:
-                            pass # XXX report error?
+                            log.debug(u'PppHandler.stop: killing pid %r', pid)
+                            os.kill(pid, SIGTERM)
+                        except OSError, e:
+                            log.debug(u'PppHandler.stop: error killing: %r', e)
+                    else:
+                        log.debug(u'PppHandler.stop: pid file not found')
                     self.conns[name]._running = False
                     self._dump_attr('conns')
                     self.conns[name]._running = False
                     self._dump_attr('conns')
+                else:
+                    log.debug(u'PppHandler.stop: connection not running')
             else:
             else:
+                log.debug(u'PppHandler.stop: connection not found')
                 raise ConnectionNotFoundError(name)
 
     @handler(u'Restart one or all the connections (even disconnected ones).')
     def restart(self, name=None):
                 raise ConnectionNotFoundError(name)
 
     @handler(u'Restart one or all the connections (even disconnected ones).')
     def restart(self, name=None):
+        log.debug(u'PppHandler.restart(%r)', name)
         names = [name]
         if name is None:
             names = self.conns.keys()
         names = [name]
         if name is None:
             names = self.conns.keys()
@@ -145,6 +163,7 @@ class PppHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler):
     @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."
     @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."
+        log.debug(u'PppHandler.reload(%r)', name)
         names = [name]
         if name is None:
             names = self.conns.keys()
         names = [name]
         if name is None:
             names = self.conns.keys()
@@ -156,25 +175,32 @@ class PppHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler):
     @handler(u'Tell if the service is running.')
     def running(self, name=None):
         r"reload() -> None :: Reload the configuration of the service."
     @handler(u'Tell if the service is running.')
     def running(self, name=None):
         r"reload() -> None :: Reload the configuration of the service."
+        log.debug(u'PppHandler.running(%r)', name)
         if name is None:
             return [c.name for c in self.conns.values() if c._running]
         if name in self.conns:
             return int(self.conns[name]._running)
         else:
         if name is None:
             return [c.name for c in self.conns.values() if c._running]
         if name in self.conns:
             return int(self.conns[name]._running)
         else:
+            log.debug(u'PppHandler.running: connection not found')
             raise ConnectionNotFoundError(name)
 
     def handle_timer(self):
             raise ConnectionNotFoundError(name)
 
     def handle_timer(self):
+        log.debug(u'PppHandler.handle_timer()')
         for c in self.conns.values():
         for c in self.conns.values():
+            log.debug(u'PppHandler.handle_timer: processing connection %r', c)
             p = subprocess.Popen(('pgrep', '-f', 'pppd call ' + c.name),
                                     stdout=subprocess.PIPE)
             pid = p.communicate()[0]
             p = subprocess.Popen(('pgrep', '-f', 'pppd call ' + c.name),
                                     stdout=subprocess.PIPE)
             pid = p.communicate()[0]
-            if p.wait() == 0 and len(pid) > 0:
+            if p.returncode == 0 and len(pid) > 0:
+                log.debug(u'PppHandler.handle_timer: pid present, running')
                 c._running = True
             else:
                 c._running = True
             else:
+                log.debug(u'PppHandler.handle_timer: pid absent, NOT running')
                 c._running = False
 
     def _write_config(self):
         r"_write_config() -> None :: Generate all the configuration files."
                 c._running = False
 
     def _write_config(self):
         r"_write_config() -> None :: Generate all the configuration files."
+        log.debug(u'PppHandler._write_config()')
         #guardo los pass que van el pap-secrets
         vars_pap = dict()
         for conn in self.conns.values():
         #guardo los pass que van el pap-secrets
         vars_pap = dict()
         for conn in self.conns.values():
@@ -197,6 +223,13 @@ class PppHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler):
 
 
 if __name__ == '__main__':
 
 
 if __name__ == '__main__':
+
+    logging.basicConfig(
+        level   = logging.DEBUG,
+        format  = '%(asctime)s %(levelname)-8s %(message)s',
+        datefmt = '%H:%M:%S',
+    )
+
     p = PppHandler()
     p.conn.add('ppp_c','nico','nico',type='PPP',device='tty0')
     p.conn.add('pppoe_c','fede','fede',type='OE',device='tty1')
     p = PppHandler()
     p.conn.add('ppp_c','nico','nico',type='PPP',device='tty0')
     p.conn.add('pppoe_c','fede','fede',type='OE',device='tty1')