1 # vim: set encoding=utf-8 et sw=4 sts=4 :
6 from signal import SIGTERM
7 import logging ; log = logging.getLogger('pymin.services.ppp')
9 from pymin.dispatcher import Handler, handler
10 from pymin.service.util import Restorable, ConfigWriter, ReloadHandler, \
11 TransactionalHandler, ItemNotFoundError, call
13 from conn import ConnectionHandler
15 __all__ = ('PppHandler',)
18 class PppHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler):
20 handler_help = u"Manage ppp service"
22 _persistent_attrs = ['conns']
24 _restorable_defaults = dict(
28 _config_writer_files = ('options.X','pap-secrets','chap-secrets','nameX')
29 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
31 def __init__(self, pickle_dir='.', config_dir='.'):
32 r"Initialize Ppphandler object, see class documentation for details."
33 log.debug(u'PppHandler(%r, %r)', pickle_dir, config_dir)
34 self._persistent_dir = pickle_dir
35 self._config_writer_cfg_dir = config_dir
36 self._config_build_templates()
38 log.debug(u'PppHandler(): restoring connections...')
39 for conn in self.conns.values():
41 log.debug(u'PppHandler(): starting connection %r', conn.name)
44 self.conn = ConnectionHandler(self)
46 @handler(u'Start one or all the connections.')
47 def start(self, name=None):
48 log.debug(u'PppHandler.start(%r)', name)
51 names = self.conns.keys()
53 if name in self.conns:
54 if not self.conns[name]._running:
55 log.debug(u'PppHandler.start: starting connection %r', name)
56 call(('pppd', 'call', name))
57 self.conns[name]._running = True
58 self._dump_attr('conns')
60 log.debug(u'PppHandler.start: connection not found')
61 raise ItemNotFoundError(name)
63 @handler(u'Stop one or all the connections.')
64 def stop(self, name=None):
65 log.debug(u'PppHandler.stop(%r)', name)
69 names = self.conns.keys()
71 if name in self.conns:
72 if self.conns[name]._running:
73 pid_file = '/var/run/ppp-' + name + '.pid'
74 log.debug(u'PppHandler.stop: getting pid from %r', pid_file)
75 if path.exists(pid_file):
76 pid = file(pid_file).readline()
77 pid = int(pid.strip())
79 log.debug(u'PppHandler.stop: killing pid %r', pid)
82 log.debug(u'PppHandler.stop: error killing: %r', e)
84 log.debug(u'PppHandler.stop: pid file not found')
85 self.conns[name]._running = False
86 self._dump_attr('conns')
88 log.debug(u'PppHandler.stop: connection not running')
90 log.debug(u'PppHandler.stop: connection not found')
91 raise ItemNotFoundError(name)
93 @handler(u'Restart one or all the connections (even disconnected ones).')
94 def restart(self, name=None):
95 log.debug(u'PppHandler.restart(%r)', name)
98 names = self.conns.keys()
103 @handler(u'Restart only one or all the already running connections.')
104 def reload(self, name=None):
105 r"reload() -> None :: Reload the configuration of the service."
106 log.debug(u'PppHandler.reload(%r)', name)
109 names = self.conns.keys()
111 if self.conns[name]._running:
115 @handler(u'Tell if the service is running.')
116 def running(self, name=None):
117 r"reload() -> None :: Reload the configuration of the service."
118 log.debug(u'PppHandler.running(%r)', name)
120 return [c.name for c in self.conns.values() if c._running]
121 if name in self.conns:
122 return int(self.conns[name]._running)
124 log.debug(u'PppHandler.running: connection not found')
125 raise ItemNotFoundError(name)
127 def handle_timer(self):
128 log.debug(u'PppHandler.handle_timer()')
129 for c in self.conns.values():
130 log.debug(u'PppHandler.handle_timer: processing connection %r', c)
131 p = subprocess.Popen(('pgrep', '-f', 'pppd call ' + c.name),
132 stdout=subprocess.PIPE)
133 pid = p.communicate()[0]
134 if p.returncode == 0 and len(pid) > 0:
135 log.debug(u'PppHandler.handle_timer: pid present, running')
138 log.debug(u'PppHandler.handle_timer: pid absent, NOT running')
141 def _write_config(self):
142 r"_write_config() -> None :: Generate all the configuration files."
143 log.debug(u'PppHandler._write_config()')
144 #guardo los pass que van el pap-secrets
146 for conn in self.conns.values():
147 if conn.type == 'OE' or conn.type == 'PPP':
148 vars_pap[conn.name] = conn
149 vars = dict(conns=vars_pap)
150 self._write_single_config('pap-secrets','pap-secrets',vars)
151 #guardo los pass que van el chap-secrets
153 for conn in self.conns.values():
154 if conn.type == 'TUNNEL' :
155 vars_chap[conn.name] = conn
156 vars = dict(conns=vars_chap)
157 self._write_single_config('chap-secrets','chap-secrets',vars)
159 for conn in self.conns.values():
160 vars = dict(conn=conn)
161 self._write_single_config('nameX',conn.name, vars)
162 self._write_single_config('options.X','options.' + conn.name, vars)
165 if __name__ == '__main__':
168 level = logging.DEBUG,
169 format = '%(asctime)s %(levelname)-8s %(message)s',
170 datefmt = '%H:%M:%S',
174 p.conn.add('ppp_c','nico','nico',type='PPP',device='tty0')
175 p.conn.add('pppoe_c','fede','fede',type='OE',device='tty1')
176 p.conn.add('ppptunnel_c','dominio\luca','luca',type='TUNNEL',server='192.168.0.23')