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 Error(HandlerError):
14 Error(command) -> Error instance :: Base ConnectionHandler exception class.
16 All exceptions raised by the ConnectionHandler inherits from this one, so you can
17 easily catch any ConnectionHandler exception.
19 message - A descriptive error message.
23 class ConnectionError(Error, KeyError):
25 ConnectionError(hostname) -> ConnectionError instance
27 This is the base exception for all connection related errors.
30 def __init__(self, connection):
31 r"Initialize the object. See class documentation for more info."
32 self.message = u'Connection error: "%s"' % connection
34 class ConnectionNotFoundError(ConnectionError):
35 def __init__(self, connection):
36 r"Initialize the object. See class documentation for more info."
37 self.message = u'Connection not found error: "%s"' % connection
39 class Connection(Sequence):
41 def __init__(self, name, username, password, type, **kw):
43 self.username = username
44 self.password = password
47 if not 'device' in kw:
48 raise ConnectionError('Bad arguments for type=OE')
49 self.device = kw['device']
50 elif type == 'TUNNEL':
51 if not 'server' in kw:
52 raise ConnectionError('Bad arguments for type=TUNNEL')
53 self.server = kw['server']
54 self.username = self.username.replace('\\','\\\\')
56 if not 'device' in kw:
57 raise ConnectionError('Bad arguments for type=PPP')
58 self.device = kw['device']
60 raise ConnectionError('Bad arguments, unknown or unspecified type')
63 if self.type == 'TUNNEL':
64 return (self.name, self.username, self.password, self.type, self.server)
65 elif self.type == 'PPP' or self.type == 'OE':
66 return (self.name, self.username, self.password, self.type, self.device)
68 def update(self, device=None, username=None, password=None):
69 if device is not None:
71 if username is not None:
72 self.username = username
73 if password is not None:
74 self.password = password
77 class ConnectionHandler(DictSubHandler):
79 handler_help = u"Manages connections for the ppp service"
81 _dict_subhandler_attr = 'conns'
82 _dict_subhandler_class = Connection
84 class PppHandler(Restorable, ConfigWriter, TransactionalHandler):
86 handler_help = u"Manage ppp service"
88 _persistent_attrs = ('conns')
90 _restorable_defaults = dict(
94 _config_writer_files = ('options.X','pap-secrets','chap-secrets','nameX')
95 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
97 def __init__(self, pickle_dir='.', config_dir='.'):
98 r"Initialize Ppphandler object, see class documentation for details."
99 self._persistent_dir = pickle_dir
100 self._config_writer_cfg_dir = config_dir
101 self._config_build_templates()
103 self.conn = ConnectionHandler(self)
105 @handler('Starts the service')
106 def start(self, name):
107 if name in self.conns:
111 raise ConnectionNotFoundError(name)
113 @handler('Stops the service')
114 def stop(self, name):
115 if name in self.conns:
116 #call(('poff', name))
119 raise ConnectionNotFoundError(name)
121 @handler('Reloads the service')
123 for conn in self.conns.values():
125 self.start(conn.name)
127 def _write_config(self):
128 r"_write_config() -> None :: Generate all the configuration files."
129 #guardo los pass que van el pap-secrets
131 for conn in self.conns.values():
132 if conn.type == 'OE' or conn.type == 'PPP':
133 vars_pap[conn.name] = conn
134 vars = dict(conns=vars_pap)
135 self._write_single_config('pap-secrets','pap-secrets',vars)
136 #guardo los pass que van el chap-secrets
138 for conn in self.conns.values():
139 if conn.type == 'TUNNEL' :
140 vars_chap[conn.name] = conn
141 vars = dict(conns=vars_chap)
142 self._write_single_config('chap-secrets','chap-secrets',vars)
144 for conn in self.conns.values():
145 vars = dict(conn=conn)
146 self._write_single_config('nameX',conn.name, vars)
147 self._write_single_config('options.X','options.' + conn.name, vars)
150 if __name__ == '__main__':
152 p.conn.add('ppp_c','nico','nico',type='PPP',device='tty0')
153 p.conn.add('pppoe_c','fede','fede',type='OE',device='tty1')
154 p.conn.add('ppptunnel_c','dominio\luca','luca',type='TUNNEL',server='192.168.0.23')