]> git.llucax.com Git - software/pymin.git/blob - services/vrrp/__init__.py
5ddbbafbc8716f66387941a10d80dc07cdc44801
[software/pymin.git] / services / vrrp / __init__.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from pymin import procman
4 from pymin.service.util import Restorable, TransactionalHandler, \
5                                ReloadHandler, RestartHandler, \
6                                ServiceHandler, ParametersHandler
7
8 # Logger
9 import logging ; log = logging.getLogger('pymin.services.vrrp')
10
11 __all__ = ('VrrpHandler', 'get_service')
12
13
14 def get_service(config):
15     return VrrpHandler(config.vrrp.pickle_dir, config.vrrp.config_dir)
16
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