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