]> git.llucax.com Git - software/pymin.git/blobdiff - pymin/services/vrrp/__init__.py
Give a nice error if a VPN key can't be readed (refs #22).
[software/pymin.git] / pymin / services / vrrp / __init__.py
index c8563a699b6923f091ea50be67532ddf94121d26..30be5a12e2df86c5e947866f0f53dad219266bc7 100644 (file)
@@ -4,17 +4,19 @@ import os
 from os import path
 from signal import SIGTERM
 from subprocess import Popen, PIPE
+import logging ; log = logging.getLogger('pymin.services.vrrp')
 
+from pymin import procman
 from pymin.seqtools import Sequence
 from pymin.dispatcher import Handler, handler, HandlerError
 from pymin.services.util import Restorable, TransactionalHandler, \
                                 ReloadHandler, RestartHandler, \
                                 ServiceHandler, ParametersHandler, call
 
-__ALL__ = ('VrrpHandler',)
-
-pid_filename = 'vrrp.pid'
+__all__ = ('VrrpHandler',)
 
+# FIXME the the command should not use new parameters unless commit where called
+#       i.e. integrate commit with procman to update internal procman parameters.
 class VrrpHandler(Restorable, ParametersHandler, ReloadHandler, RestartHandler,
                         ServiceHandler, TransactionalHandler):
 
@@ -23,36 +25,57 @@ class VrrpHandler(Restorable, ParametersHandler, ReloadHandler, RestartHandler,
     _persistent_attrs = ['params']
 
     _restorable_defaults = dict(
-                            params = dict( ipaddress='192.168.0.1',
-                                            id = '1',
-                                            prio = '',
-                                            dev = 'eth0',
-                                    ),
-                            )
+        params = dict(
+                ipaddress = '192.168.0.1',
+                id        = '1',
+                prio      = '',
+                dev       = 'eth0',
+                persist   = True,
+            ),
+        )
+
+    @property
+    def _command(self):
+        command = ['vrrpd', '-i', self.params['dev'], '-v', self.params['id']]
+        if self.params['prio']:
+            command.extend(('-p', self.params['prio']))
+        command.append(self.params['ipaddress'])
+        return command
 
     def _service_start(self):
-        if self.params['prio'] != '':
-            call(('vrrp', '-i', self.params['dev'], '-v', self.params['id'],
-                    '-p', self.params['prio'], self.params['ipaddress']))
-        else:
-            call(('vrrp', '-i', self.params['dev'], '-v', self.params['id'], \
-                    self.params['ipaddress']))
+        log.debug(u'VrrpHandler._service_start()')
+        procinfo = procman.get('vrrp')
+        procinfo.command = self._command
+        procinfo.persist = self.params['persist']
+        procman.start('vrrp')
 
     def _service_stop(self):
-        try:
-            pid = file(path.join(self._pid_dir, pid_filename )).read().strip()
-            os.kill(int(pid), SIGTERM)
-        except (IOError, OSError):
-            # TODO log
-            pass
+        log.debug(u'VrrpHandler._service_stop()')
+        procman.stop('vrrp')
+
+    def _service_restart(self):
+        procinfo = procman.get('vrrp')
+        procinfo.command = self._command
+        procinfo.persist = self.params['persist']
+        procman.restart('vrrp')
 
     def __init__(self, pickle_dir='.', config_dir='.', pid_dir='.'):
+        log.debug(u'VrrpHandler(%r, %r, $r)', pickle_dir, config_dir, pid_dir)
         self._persistent_dir = pickle_dir
         self._pid_dir = pid_dir
+        procman.register('vrrp', None)
         ServiceHandler.__init__(self)
 
 
 if __name__ == '__main__':
+
+    logging.basicConfig(
+        level   = logging.DEBUG,
+        format  = '%(asctime)s %(levelname)-8s %(message)s',
+        datefmt = '%H:%M:%S',
+    )
+
     v = VrrpHandler()
-    v.set('prio','10')
-    v.commit()
\ No newline at end of file
+    v.set('prio', '10')
+    v.commit()
+