[IMP] wip
[odoo/odoo.git] / bin / tools / maintenance.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import xmlrpclib
23 import config
24 import logging
25 import pooler
26
27 _logger = logging.getLogger(__name__)
28
29 class RemoteConnectionException(Exception):
30     pass
31
32 class RemoteConnection:
33     
34     def __init__(self, server, db, login, password):
35         self._server = server.strip()
36         if not self._server.endswith("/"):
37             self._server += "/"
38         self._db = db
39         self._login = login
40         self._password = password
41         
42         rpc = xmlrpclib.ServerProxy(self._server + "xmlrpc/common")
43         try:
44             self._userid = rpc.login(self._db, self._login, self._password)
45         except:
46             raise RemoteConnectionException("Unable to contact the remote server")
47
48         if not self._userid:
49             raise RemoteConnectionException("Unable to contact the remote server")
50         
51         self._rpc = xmlrpclib.ServerProxy(self._server + "xmlrpc/object")
52         
53     def get_remote_object(self, object):
54         return RemoteObject(self, object)
55     
56 class RemoteObject(object):
57     
58     def __init__(self, connection, object):
59         self._c = connection
60         self._object = object
61     
62     def __getattr__(self, fun):
63         def remote_call(*args, **kwargs):
64             return self._c._rpc.execute(self._c._db, self._c._userid,
65                                           self._c._password, self._object, fun, *args, **kwargs)
66         return remote_call
67     
68     def __getitem__(self, item):
69         return getattr(self, item)
70         
71 class RemoteContractException(Exception): pass
72
73 def remote_contract(cr, uid, contract_id):
74     pool = pooler.get_pool(cr.dbname)
75     dbuuid = pool.get('ir.config_parameter').get_param(cr, uid, 'database.uuid')
76     
77     try:
78         ro = RemoteConnection(config.config.get("maintenance_server"), config.config.get("maintenance_db"),
79                               config.config.get("maintenance_login"), config.config.get("maintenance_password")
80                               ).get_remote_object('maintenance.maintenance')
81     except:
82         _logger.exception("Exception")
83         raise RemoteContractException("Unable to contact the migration server")
84
85     info = ro.check_contract_6({
86                 "contract_name": contract_id,
87                 "dbuuid": dbuuid,
88                 "dbname": cr.dbname})
89     for n in info:
90         setattr(ro, n, info[n])
91     
92     return ro
93
94
95 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
96