]> git.llucax.com Git - software/pymin.git/blob - pymin/services/proxy/__init__.py
Added support to hook services to IPHandler.
[software/pymin.git] / pymin / services / proxy / __init__.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from os import path
4
5 from pymin.seqtools import Sequence
6 from pymin.dispatcher import Handler, handler, HandlerError
7 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
8                                 TransactionalHandler, ParametersHandler, \
9                                 DictSubHandler
10
11 import crypt
12
13 __ALL__ = ('ProxyHandler',)
14
15 class Host(Sequence):
16     def __init__(self,ip):
17         self.ip = ip
18     def as_tuple(self):
19         return (self.ip,)
20
21 # TODO convert to a SetSubHandler
22
23 class HostHandler(DictSubHandler):
24
25     handler_help = u"Manage proxy hosts"
26
27     _cont_subhandler_attr = 'hosts'
28     _cont_subhandler_class = Host
29
30 class User(Sequence):
31     def __init__(self, name, password):
32         self.name = name
33         self.password = crypt.crypt(password,'BA')
34     def as_tuple(self):
35         return (self.name, self.password)
36     def update(self, password=None):
37         if password is not None:
38             self.password = crypt.crypt(password,'BA')
39
40 class UserHandler(DictSubHandler):
41
42     handler_help = u"Manage proxy users"
43
44     _cont_subhandler_attr = 'users'
45     _cont_subhandler_class = User
46
47 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
48                    TransactionalHandler, ParametersHandler):
49
50     handler_help = u"Manage proxy service"
51
52     _initd_name = 'squid'
53
54     _persistent_attrs = ('params', 'hosts', 'users')
55
56     _restorable_defaults = dict(
57             hosts = dict(),
58             params  = dict(
59                 ip   = '192.168.0.1',
60                 port = '8080',
61             ),
62             users = dict(),
63     )
64
65     _config_writer_files = ('squid.conf','users.conf')
66     _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
67
68     def __init__(self, pickle_dir='.', config_dir='.'):
69         r"Initialize DhcpHandler object, see class documentation for details."
70         self._persistent_dir = pickle_dir
71         self._config_writer_cfg_dir = config_dir
72         self._config_build_templates()
73         InitdHandler.__init__(self)
74         self.host = HostHandler(self)
75         self.user = UserHandler(self)
76
77     def _get_config_vars(self, config_file):
78         if config_file == 'squid.conf':
79             return dict(hosts=self.hosts.values(), **self.params)
80         return dict(users=self.users)
81
82
83 if __name__ == '__main__':
84
85     px = ProxyHandler()
86     px.set('ip','192.66.66.66')
87     px.set('port','666')
88     px.host.add('192.168.0.25.25')
89     px.host.add('192.168.0.25.26')
90     px.host.add('192.168.0.25.27')
91     px.host.delete('192.168.0.25.27')
92     px.user.add('lala','soronga')
93     px.user.add('culo','sarasa')
94     px.commit()