]> git.llucax.com Git - software/pymin.git/blob - pymin/services/vrrp/__init__.py
Add support for service running status and restoring at startup.
[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
8 from pymin.seqtools import Sequence
9 from pymin.dispatcher import Handler, handler, HandlerError
10 from pymin.services.util import Restorable, TransactionalHandler, \
11                                 ReloadHandler, RestartHandler, \
12                                 ServiceHandler, ParametersHandler, call
13
14 __ALL__ = ('VrrpHandler',)
15
16 pid_filename = 'vrrp.pid'
17
18 class VrrpHandler(Restorable, ParametersHandler, ReloadHandler, RestartHandler,
19                         ServiceHandler, TransactionalHandler):
20
21     handler_help = u"Manage VRRP service"
22
23     _persistent_attrs = ['params']
24
25     _restorable_defaults = dict(
26                             params = dict( ipaddress='192.168.0.1',
27                                             id = '1',
28                                             prio = '',
29                                             dev = 'eth0',
30                                     ),
31                             )
32
33     def _service_start(self):
34         if self.params['prio'] != '':
35             call(('vrrp', '-i', self.params['dev'], '-v', self.params['id'],
36                     '-p', self.params['prio'], self.params['ipaddress']))
37         else:
38             call(('vrrp', '-i', self.params['dev'], '-v', self.params['id'], \
39                     self.params['ipaddress']))
40
41     def _service_stop(self):
42         try:
43             pid = file(path.join(self._pid_dir, pid_filename )).read().strip()
44             os.kill(int(pid), SIGTERM)
45         except (IOError, OSError):
46             # TODO log
47             pass
48
49     def __init__(self, pickle_dir='.', config_dir='.', pid_dir='.'):
50         self._persistent_dir = pickle_dir
51         self._pid_dir = pid_dir
52         ServiceHandler.__init__(self)
53
54
55 if __name__ == '__main__':
56     v = VrrpHandler()
57     v.set('prio','10')
58     v.commit()