[merge]
[odoo/odoo.git] / bin / tools / maintenance.py
index 959d9f7..f8f8e9f 100644 (file)
@@ -1,62 +1,93 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
-#
-#    OpenERP, Open Source Management Solution  
-#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
-#    $Id$
+#    
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
+#    GNU Affero General Public License for more details.
 #
-#    You should have received a copy of the GNU General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
 #
 ##############################################################################
 
 import xmlrpclib
+import config
+import pooler
 
-class remote_contract(object):
-    def __init__(self, contract_id, contract_password, modules=None):
-        self.__server = 'http://localhost:8069/xmlrpc/'
-        self.__db = "trunk"
-        self.__password = "admin"
-        self.__login = "admin"
-        
-        rpc = xmlrpclib.ServerProxy(self.__server + 'common')
-        self.__userid = rpc.login(self.__db, self.__login, self.__password)
 
-        self.__rpc = xmlrpclib.ServerProxy(self.__server + 'object')
-        
+class RemoteConnectionException(Exception):
+    pass
 
-        contract = {
-            'name': contract_id,
-            'password': contract_password,
-        }
-        if modules is None:
-            modules = []
+class RemoteConnection:
+    
+    def __init__(self, server, db, login, password):
+        self._server = server.strip()
+        if not self._server.endswith("/"):
+            self._server += "/"
+        self._db = db
+        self._login = login
+        self._password = password
+        
+        rpc = xmlrpclib.ServerProxy(self._server + "xmlrpc/common")
+        try:
+            self._userid = rpc.login(self._db, self._login, self._password)
+        except:
+            raise RemoteConnectionException("Unable to contact the remote server")
 
-        info = self.check_contract(modules, contract)
-        for n in info:
-            setattr(self, n, info[n])
+        if not self._userid:
+            raise RemoteConnectionException("Unable to contact the remote server")
         
-        self.name = contract_id
-        self.contract_id = self.name
-        self.password = contract_password
+        self._rpc = xmlrpclib.ServerProxy(self._server + "xmlrpc/object")
         
+    def get_remote_object(self, object):
+        return RemoteObject(self, object)
+    
+class RemoteObject(object):
+    
+    def __init__(self, connection, object):
+        self._c = connection
+        self._object = object
+    
     def __getattr__(self, fun):
         def remote_call(*args, **kwargs):
-            return self.__rpc.execute(self.__db, self.__userid, self.__password, 'maintenance.maintenance', fun, *args, **kwargs)
+            return self._c._rpc.execute(self._c._db, self._c._userid,
+                                          self._c._password, self._object, fun, *args, **kwargs)
         return remote_call
-
+    
     def __getitem__(self, item):
         return getattr(self, item)
+        
+class RemoteContractException(Exception): pass
+
+def remote_contract(cr, uid, contract_id):
+    pool = pooler.get_pool(cr.dbname)
+    dbuuid = pool.get('ir.config_parameter').get_param(cr, uid, 'database.uuid')
+    
+    try:
+        ro = RemoteConnection(config.config.get("maintenance_server"), config.config.get("maintenance_db"),
+                              config.config.get("maintenance_login"), config.config.get("maintenance_password")
+                              ).get_remote_object('maintenance.maintenance')
+    except:
+        raise RemoteContractException("Unable to contact the migration server")
+
+    info = ro.check_contract_6({
+                "contract_name": contract_id,
+                "dbuuid": dbuuid,
+                "dbname": cr.dbname})
+    for n in info:
+        setattr(ro, n, info[n])
+    
+    return ro
+
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: