1 # vim: set encoding=utf-8 et sw=4 sts=4 :
5 from pymin.seqtools import Sequence
6 from pymin.dispatcher import Handler, handler, HandlerError
7 from pymin.services.util import Restorable, ConfigWriter, ReloadHandler, \
8 TransactionalHandler, DictSubHandler, call
10 __ALL__ = ('PppHandler',)
12 class ConnectionError(HandlerError, KeyError):
14 ConnectionError(hostname) -> ConnectionError instance
16 This is the base exception for all connection related errors.
19 def __init__(self, connection):
20 r"Initialize the object. See class documentation for more info."
21 self.message = u'Connection error: "%s"' % connection
23 class ConnectionNotFoundError(ConnectionError):
24 def __init__(self, connection):
25 r"Initialize the object. See class documentation for more info."
26 self.message = u'Connection not found error: "%s"' % connection
28 class Connection(Sequence):
30 def __init__(self, name, username, password, type, **kw):
32 self.username = username
33 self.password = password
37 if not 'device' in kw:
38 raise ConnectionError('Bad arguments for type=OE')
39 self.device = kw['device']
40 elif type == 'TUNNEL':
41 if not 'server' in kw:
42 raise ConnectionError('Bad arguments for type=TUNNEL')
43 self.server = kw['server']
44 self.username = self.username.replace('\\','\\\\')
46 if not 'device' in kw:
47 raise ConnectionError('Bad arguments for type=PPP')
48 self.device = kw['device']
50 raise ConnectionError('Bad arguments, unknown or unspecified type')
53 if self.type == 'TUNNEL':
54 return (self.name, self.username, self.password, self.type, self.server)
55 elif self.type == 'PPP' or self.type == 'OE':
56 return (self.name, self.username, self.password, self.type, self.device)
58 def update(self, device=None, username=None, password=None):
59 if device is not None:
61 if username is not None:
62 self.username = username
63 if password is not None:
64 self.password = password
67 class ConnectionHandler(DictSubHandler):
69 handler_help = u"Manages connections for the ppp service"
71 _cont_subhandler_attr = 'conns'
72 _cont_subhandler_class = Connection
74 class PppHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler):
76 handler_help = u"Manage ppp service"
78 _persistent_attrs = ['conns']
80 _restorable_defaults = dict(
84 _config_writer_files = ('options.X','pap-secrets','chap-secrets','nameX')
85 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
87 def __init__(self, pickle_dir='.', config_dir='.'):
88 r"Initialize Ppphandler object, see class documentation for details."
89 self._persistent_dir = pickle_dir
90 self._config_writer_cfg_dir = config_dir
91 self._config_build_templates()
93 for conn in self.conns.values():
97 self.conn = ConnectionHandler(self)
99 @handler(u'Start one or all the connections.')
100 def start(self, name=None):
103 names = self.conns.keys()
105 if name in self.conns:
106 if not self.conns[name]._running:
108 self.conns[name]._running = True
109 self._dump_attr('conns')
111 raise ConnectionNotFoundError(name)
113 @handler(u'Stop one or all the connections.')
114 def stop(self, name=None):
117 names = self.conns.keys()
119 if name in self.conns:
120 if self.conns[name]._running:
122 self.conns[name]._running = False
123 self._dump_attr('conns')
125 raise ConnectionNotFoundError(name)
127 @handler(u'Restart one or all the connections (even disconnected ones).')
128 def restart(self, name=None):
131 names = self.conns.keys()
136 @handler(u'Restart only one or all the already running connections.')
137 def reload(self, name=None):
138 r"reload() -> None :: Reload the configuration of the service."
141 names = self.conns.keys()
143 if self.conns[name]._running:
147 @handler(u'Tell if the service is running.')
148 def running(self, name=None):
149 r"reload() -> None :: Reload the configuration of the service."
151 return [c.name for c in self.conns.values() if c._running]
152 if name in self.conns:
153 return int(self.conns[name]._running)
155 raise ConnectionNotFoundError(name)
157 def _write_config(self):
158 r"_write_config() -> None :: Generate all the configuration files."
159 #guardo los pass que van el pap-secrets
161 for conn in self.conns.values():
162 if conn.type == 'OE' or conn.type == 'PPP':
163 vars_pap[conn.name] = conn
164 vars = dict(conns=vars_pap)
165 self._write_single_config('pap-secrets','pap-secrets',vars)
166 #guardo los pass que van el chap-secrets
168 for conn in self.conns.values():
169 if conn.type == 'TUNNEL' :
170 vars_chap[conn.name] = conn
171 vars = dict(conns=vars_chap)
172 self._write_single_config('chap-secrets','chap-secrets',vars)
174 for conn in self.conns.values():
175 vars = dict(conn=conn)
176 self._write_single_config('nameX',conn.name, vars)
177 self._write_single_config('options.X','options.' + conn.name, vars)
180 if __name__ == '__main__':
182 p.conn.add('ppp_c','nico','nico',type='PPP',device='tty0')
183 p.conn.add('pppoe_c','fede','fede',type='OE',device='tty1')
184 p.conn.add('ppptunnel_c','dominio\luca','luca',type='TUNNEL',server='192.168.0.23')