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