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 \
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
36 if not 'device' in kw:
37 raise ConnectionError('Bad arguments for type=OE')
38 self.device = kw['device']
39 elif type == 'TUNNEL':
40 if not 'server' in kw:
41 raise ConnectionError('Bad arguments for type=TUNNEL')
42 self.server = kw['server']
43 self.username = self.username.replace('\\','\\\\')
45 if not 'device' in kw:
46 raise ConnectionError('Bad arguments for type=PPP')
47 self.device = kw['device']
49 raise ConnectionError('Bad arguments, unknown or unspecified type')
52 if self.type == 'TUNNEL':
53 return (self.name, self.username, self.password, self.type, self.server)
54 elif self.type == 'PPP' or self.type == 'OE':
55 return (self.name, self.username, self.password, self.type, self.device)
57 def update(self, device=None, username=None, password=None):
58 if device is not None:
60 if username is not None:
61 self.username = username
62 if password is not None:
63 self.password = password
66 class ConnectionHandler(DictSubHandler):
68 handler_help = u"Manages connections for the ppp service"
70 _cont_subhandler_attr = 'conns'
71 _cont_subhandler_class = Connection
73 class PppHandler(Restorable, ConfigWriter, TransactionalHandler):
75 handler_help = u"Manage ppp service"
77 _persistent_attrs = ['conns']
79 _restorable_defaults = dict(
83 _config_writer_files = ('options.X','pap-secrets','chap-secrets','nameX')
84 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
86 def __init__(self, pickle_dir='.', config_dir='.'):
87 r"Initialize Ppphandler object, see class documentation for details."
88 self._persistent_dir = pickle_dir
89 self._config_writer_cfg_dir = config_dir
90 self._config_build_templates()
92 self.conn = ConnectionHandler(self)
94 @handler('Starts the service')
95 def start(self, name):
96 if name in self.conns:
100 raise ConnectionNotFoundError(name)
102 @handler('Stops the service')
103 def stop(self, name):
104 if name in self.conns:
105 #call(('poff', name))
108 raise ConnectionNotFoundError(name)
110 @handler('Reloads the service')
112 for conn in self.conns.values():
114 self.start(conn.name)
116 def _write_config(self):
117 r"_write_config() -> None :: Generate all the configuration files."
118 #guardo los pass que van el pap-secrets
120 for conn in self.conns.values():
121 if conn.type == 'OE' or conn.type == 'PPP':
122 vars_pap[conn.name] = conn
123 vars = dict(conns=vars_pap)
124 self._write_single_config('pap-secrets','pap-secrets',vars)
125 #guardo los pass que van el chap-secrets
127 for conn in self.conns.values():
128 if conn.type == 'TUNNEL' :
129 vars_chap[conn.name] = conn
130 vars = dict(conns=vars_chap)
131 self._write_single_config('chap-secrets','chap-secrets',vars)
133 for conn in self.conns.values():
134 vars = dict(conn=conn)
135 self._write_single_config('nameX',conn.name, vars)
136 self._write_single_config('options.X','options.' + conn.name, vars)
139 if __name__ == '__main__':
141 p.conn.add('ppp_c','nico','nico',type='PPP',device='tty0')
142 p.conn.add('pppoe_c','fede','fede',type='OE',device='tty1')
143 p.conn.add('ppptunnel_c','dominio\luca','luca',type='TUNNEL',server='192.168.0.23')