# vim: set encoding=utf-8 et sw=4 sts=4 :
++import os
from os import path
++from signal import SIGTERM
from pymin.seqtools import Sequence
from pymin.dispatcher import Handler, handler, HandlerError
self._config_writer_cfg_dir = config_dir
self._config_build_templates()
self._restore()
+ for conn in self.conns.values():
+ if conn._running:
+ conn._running = False
+ self.start(conn.name)
self.conn = ConnectionHandler(self)
- @handler('Starts the service')
- def start(self, name):
- if name in self.conns:
- call(['pppd','call', name],stdout=None, stderr=None)
- #print ('pon', name)
- else:
- raise ConnectionNotFoundError(name)
-
- @handler('Stops the service')
- def stop(self, name):
+ @handler(u'Start one or all the connections.')
+ def start(self, name=None):
+ names = [name]
+ if name is None:
+ names = self.conns.keys()
+ for name in names:
+ if name in self.conns:
+ if not self.conns[name]._running:
- call(('pon', name))
++ call(('pppd', 'call', name))
+ self.conns[name]._running = True
+ self._dump_attr('conns')
+ else:
+ raise ConnectionNotFoundError(name)
+
+ @handler(u'Stop one or all the connections.')
+ def stop(self, name=None):
+ names = [name]
+ if name is None:
+ names = self.conns.keys()
+ for name in names:
+ if name in self.conns:
+ if self.conns[name]._running:
+ call(('poff', name))
++ if path.exists('/var/run/ppp-' + name + '.pid'):
++ pid = file('/var/run/ppp-' + name + '.pid').readline()
++ try:
++ os.kill(int(pid.strip()), SIGTERM)
++ except OSError:
++ pass # XXX report error?
+ self.conns[name]._running = False
+ self._dump_attr('conns')
+ else:
+ raise ConnectionNotFoundError(name)
+
+ @handler(u'Restart one or all the connections (even disconnected ones).')
+ def restart(self, name=None):
+ names = [name]
+ if name is None:
+ names = self.conns.keys()
+ for name in names:
+ self.stop(name)
+ self.start(name)
+
+ @handler(u'Restart only one or all the already running connections.')
+ def reload(self, name=None):
+ r"reload() -> None :: Reload the configuration of the service."
+ names = [name]
+ if name is None:
+ names = self.conns.keys()
+ for name in names:
+ if self.conns[name]._running:
+ self.stop(name)
+ self.start(name)
+
+ @handler(u'Tell if the service is running.')
+ def running(self, name=None):
+ r"reload() -> None :: Reload the configuration of the service."
+ if name is None:
+ return [c.name for c in self.conns.values() if c._running]
if name in self.conns:
- if path.exists('/var/run/ppp-' + name + '.pid'):
- pid = file('/var/run/ppp-' + name + '.pid').readline().strip()
- call(['kill',pid],stdout=None, stderr=None)
- #print ('poff', name)
+ return int(self.conns[name]._running)
else:
raise ConnectionNotFoundError(name)
p.commit()
print p.conn.list()
print p.conn.show()
++
__ALL__ = ('VrrpHandler',)
- pid_filename = 'vrrp.pid'
-
-class VrrpHandler(Restorable, ParametersHandler, TransactionalHandler):
+class VrrpHandler(Restorable, ParametersHandler, ReloadHandler, RestartHandler,
+ ServiceHandler, TransactionalHandler):
+
handler_help = u"Manage VRRP service"
- _persistent_attrs = 'params'
+ _persistent_attrs = ['params']
_restorable_defaults = dict(
params = dict( ipaddress='192.168.0.1',
),
)
- def __init__(self, pickle_dir='.', config_dir='.', pid_dir='.'):
- self._persistent_dir = pickle_dir
- self._pid_dir = pid_dir
- self._restore()
-
- @handler('Starts the service')
- def start(self):
+ def _service_start(self):
if self.params['prio'] != '':
- call(('/usr/local/bin/vrrpd','-i',self.params['dev'],'-v',self.params['id'],'-p',self.params['prio'],self.params['ipaddress']))
- #print ('vrrpd','-i',self.params['dev'],'-v',self.params['id'],'-p',self.params['prio'],self.params['ipaddress'])
+ call(('vrrp', '-i', self.params['dev'], '-v', self.params['id'],
+ '-p', self.params['prio'], self.params['ipaddress']))
else:
- call(('/usr/local/bin/vrrpd','-i',self.params['dev'],'-v',self.params['id'],self.params['ipaddress']))
- #print ('vrrpd','-i',self.params['dev'],'-v',self.params['id'],self.params['ipaddress'])
-
- @handler('Stop the service')
- def stop(self):
- try :
- pid = 'vrrpd' + '_' + self.params['dev'] + '_' + self.params['id'] + '.pid'
- f = file(path.join(self._pid_dir, pid ), 'r')
- call(('kill',f.read().strip('\n')))
- #print('kill','<',f.read())
- except IOError:
+ call(('vrrp', '-i', self.params['dev'], '-v', self.params['id'], \
+ self.params['ipaddress']))
+
+ def _service_stop(self):
+ try:
++ pid_filename = 'vrrpd_%(dev)s_%(id)s.pid' % self.params
+ pid = file(path.join(self._pid_dir, pid_filename )).read().strip()
+ os.kill(int(pid), SIGTERM)
+ except (IOError, OSError):
+ # TODO log
pass
- @handler('Reloads the service')
- def reload(self):
- self.stop()
- self.start()
+ def __init__(self, pickle_dir='.', config_dir='.', pid_dir='.'):
+ self._persistent_dir = pickle_dir
+ self._pid_dir = pid_dir
+ ServiceHandler.__init__(self)
if __name__ == '__main__':
v = VrrpHandler()
v.set('prio','10')
- v.commit()
+ v.commit()
++