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