]> git.llucax.com Git - software/pymin.git/blob - 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
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 import os
4 from os import path
5 from signal import SIGTERM
6 from subprocess import Popen, PIPE
7 import logging ; log = logging.getLogger('pymin.services.vrrp')
8
9 from pymin import procman
10 from pymin.seqtools import Sequence
11 from pymin.dispatcher import Handler, handler, HandlerError
12 from pymin.services.util import Restorable, TransactionalHandler, \
13                                 ReloadHandler, RestartHandler, \
14                                 ServiceHandler, ParametersHandler, call
15
16 __all__ = ('VrrpHandler',)
17
18 # FIXME the the command should not use new parameters unless commit where called
19 #       i.e. integrate commit with procman to update internal procman parameters.
20 class VrrpHandler(Restorable, ParametersHandler, ReloadHandler, RestartHandler,
21                         ServiceHandler, TransactionalHandler):
22
23     handler_help = u"Manage VRRP service"
24
25     _persistent_attrs = ['params']
26
27     _restorable_defaults = dict(
28         params = dict(
29                 ipaddress = '192.168.0.1',
30                 id        = '1',
31                 prio      = '',
32                 dev       = 'eth0',
33                 persist   = True,
34             ),
35         )
36
37     @property
38     def _command(self):
39         command = ['vrrpd', '-i', self.params['dev'], '-v', self.params['id']]
40         if self.params['prio']:
41             command.extend(('-p', self.params['prio']))
42         command.append(self.params['ipaddress'])
43         return command
44
45     def _service_start(self):
46         log.debug(u'VrrpHandler._service_start()')
47         procinfo = procman.get('vrrp')
48         procinfo.command = self._command
49         procinfo.persist = self.params['persist']
50         procman.start('vrrp')
51
52     def _service_stop(self):
53         log.debug(u'VrrpHandler._service_stop()')
54         procman.stop('vrrp')
55
56     def _service_restart(self):
57         procinfo = procman.get('vrrp')
58         procinfo.command = self._command
59         procinfo.persist = self.params['persist']
60         procman.restart('vrrp')
61
62     def __init__(self, pickle_dir='.', config_dir='.', pid_dir='.'):
63         log.debug(u'VrrpHandler(%r, %r, $r)', pickle_dir, config_dir, pid_dir)
64         self._persistent_dir = pickle_dir
65         self._pid_dir = pid_dir
66         procman.register('vrrp', None)
67         ServiceHandler.__init__(self)
68
69
70 if __name__ == '__main__':
71
72     logging.basicConfig(
73         level   = logging.DEBUG,
74         format  = '%(asctime)s %(levelname)-8s %(message)s',
75         datefmt = '%H:%M:%S',
76     )
77
78     v = VrrpHandler()
79     v.set('prio', '10')
80     v.commit()
81