[IMP] db and common service cleanup

- move check_super into db
- remove some hardcoding from dispacth
- hardcode wsgi handler and rpc endpoints
This commit is contained in:
Antony Lesuisse
2015-08-01 17:20:43 +02:00
parent 070b9d2a08
commit a88c520d03
7 changed files with 60 additions and 79 deletions
-7
View File
@@ -677,13 +677,6 @@ class module(osv.osv):
self._update_dependencies(cr, uid, mod, terp.get('depends', []))
self._update_category(cr, uid, mod, terp.get('category', 'Uncategorized'))
# Trigger load_addons if new module have been discovered it exists on
# wsgi handlers, so they can react accordingly
if tuple(res) != (0, 0):
for handler in openerp.service.wsgi_server.module_handlers:
if hasattr(handler, 'load_addons'):
handler.load_addons()
return res
def download(self, cr, uid, ids, download=True, context=None):
+2 -5
View File
@@ -112,8 +112,6 @@ def dispatch_rpc(service_name, method, params):
dispatch = openerp.service.model.dispatch
elif service_name == 'report':
dispatch = openerp.service.report.dispatch
else:
dispatch = openerp.service.wsgi_server.rpc_handlers.get(service_name)
result = dispatch(method, params)
if rpc_request_flag or rpc_response_flag:
@@ -1471,7 +1469,7 @@ class Root(object):
return request.registry['ir.http'].routing_map()
def db_list(force=False, httprequest=None):
dbs = dispatch_rpc("db", "list", [force])
dbs = openerp.service.db.list_dbs(force)
return db_filter(dbs, httprequest=httprequest)
def db_filter(dbs, httprequest=None):
@@ -1621,6 +1619,5 @@ class CommonController(Controller):
nsession = root.session_store.new()
return nsession.sid
# register main wsgi handler
# main wsgi handler
root = Root()
openerp.service.wsgi_server.register_wsgi_handler(root)
+8 -8
View File
@@ -17,14 +17,6 @@ RPC_VERSION_1 = {
'protocol_version': 1,
}
def dispatch(method, params):
if method not in ['login', 'about', 'timezone_get',
'version', 'authenticate', 'set_loglevel']:
raise Exception("Method not found: %s" % method)
fn = globals()['exp_' + method]
return fn(*params)
def exp_login(db, login, password):
# TODO: legacy indirection through 'security', should use directly
# the res.users model
@@ -57,3 +49,11 @@ def exp_set_loglevel(loglevel, logger=None):
# TODO Previously, the level was set on the now deprecated
# `openerp.netsvc.Logger` class.
return True
def dispatch(method, params):
g = globals()
exp_method_name = 'exp_' + method
if exp_method_name in g:
return g[exp_method_name](*params)
else:
raise Exception("Method not found: %s" % method)
+48 -31
View File
@@ -21,13 +21,21 @@ import openerp.release
import openerp.sql_db
import openerp.tools
import security
_logger = logging.getLogger(__name__)
class DatabaseExists(Warning):
pass
#----------------------------------------------------------
# Master password required
#----------------------------------------------------------
def check_super(passwd):
if passwd == openerp.tools.config['admin_passwd']:
return True
else:
raise openerp.exceptions.AccessDenied()
# This should be moved to openerp.modules.db, along side initialize().
def _initialize_db(id, db_name, demo, lang, user_password):
try:
@@ -56,22 +64,6 @@ def _initialize_db(id, db_name, demo, lang, user_password):
except Exception, e:
_logger.exception('CREATE DATABASE failed:')
def dispatch(method, params):
if method in ['create', 'get_progress', 'drop', 'dump', 'restore', 'rename',
'change_admin_password', 'migrate_databases',
'create_database', 'duplicate_database']:
passwd = params[0]
params = params[1:]
security.check_super(passwd)
elif method in ['db_exist', 'list', 'list_lang', 'server_version']:
# params = params
# No security check for these methods
pass
else:
raise KeyError("Method not found: %s" % method)
fn = globals()['exp_' + method]
return fn(*params)
def _create_empty_database(name):
db = openerp.sql_db.db_connect('postgres')
with closing(db.cursor()) as cr:
@@ -122,9 +114,8 @@ def _drop_conn(cr, db_name):
except Exception:
pass
def exp_drop(db_name):
if db_name not in exp_list(True):
if db_name not in list_dbs(True):
return False
openerp.modules.registry.RegistryManager.delete(db_name)
openerp.sql_db.close_db(db_name)
@@ -287,12 +278,28 @@ def exp_rename(old_name, new_name):
shutil.move(old_fs, new_fs)
return True
def exp_change_admin_password(new_password):
openerp.tools.config['admin_passwd'] = new_password
openerp.tools.config.save()
return True
def exp_migrate_databases(databases):
for db in databases:
_logger.info('migrate database %s', db)
openerp.tools.config['update']['base'] = True
openerp.modules.registry.RegistryManager.new(db, force_demo=False, update_module=True)
return True
#----------------------------------------------------------
# No master password required
#----------------------------------------------------------
@openerp.tools.mute_logger('openerp.sql_db')
def exp_db_exist(db_name):
## Not True: in fact, check if connection to database is possible. The database may exists
return bool(openerp.sql_db.db_connect(db_name))
def exp_list(document=False):
def list_dbs(force=False):
if not openerp.tools.config['list_db'] and not document:
raise openerp.exceptions.AccessDenied()
chosen_template = openerp.tools.config['db_template']
@@ -318,10 +325,10 @@ def exp_list(document=False):
res.sort()
return res
def exp_change_admin_password(new_password):
openerp.tools.config['admin_passwd'] = new_password
openerp.tools.config.save()
return True
def exp_list(document=False):
if not openerp.tools.config['list_db']:
raise openerp.exceptions.AccessDenied()
return list_dbs()
def exp_list_lang():
return openerp.tools.scan_languages()
@@ -332,9 +339,19 @@ def exp_server_version():
"""
return openerp.release.version
def exp_migrate_databases(databases):
for db in databases:
_logger.info('migrate database %s', db)
openerp.tools.config['update']['base'] = True
openerp.modules.registry.RegistryManager.new(db, force_demo=False, update_module=True)
return True
#----------------------------------------------------------
# db service dispatch
#----------------------------------------------------------
def dispatch(method, params):
g = globals()
exp_method_name = 'exp_' + method
if method in ['db_exist', 'list', 'list_lang', 'server_version']:
return g[exp_method_name](*params)
elif exp_method_name in g:
passwd = params[0]
params = params[1:]
check_super(passwd)
return g[exp_method_name](*params)
else:
raise KeyError("Method not found: %s" % method)
-6
View File
@@ -8,12 +8,6 @@ def login(db, login, password):
res_users = openerp.registry(db)['res.users']
return res_users._login(db, login, password)
def check_super(passwd):
if passwd == openerp.tools.config['admin_passwd']:
return True
else:
raise openerp.exceptions.AccessDenied()
def check(db, uid, passwd):
res_users = openerp.registry(db)['res.users']
return res_users.check(db, uid, passwd)
+1 -1
View File
@@ -769,7 +769,7 @@ class WorkerCron(Worker):
if config['db_name']:
db_names = config['db_name'].split(',')
else:
db_names = openerp.service.db.exp_list(True)
db_names = openerp.service.db.list_dbs(True)
return db_names
def process_work(self):
+1 -21
View File
@@ -152,24 +152,6 @@ def wsgi_xmlrpc(environ, start_response):
params, method = xmlrpclib.loads(data)
return xmlrpc_return(start_response, service, method, params, string_faultcode)
# WSGI handlers registered through the register_wsgi_handler() function below.
module_handlers = []
# RPC endpoints registered through the register_rpc_endpoint() function below.
rpc_handlers = {}
def register_wsgi_handler(handler):
""" Register a WSGI handler.
Handlers are tried in the order they are added. We might provide a way to
register a handler for specific routes later.
"""
module_handlers.append(handler)
def register_rpc_endpoint(endpoint, handler):
""" Register a handler for a given RPC enpoint.
"""
rpc_handlers[endpoint] = handler
def application_unproxied(environ, start_response):
""" WSGI entry point."""
# cleanup db/uid trackers - they're set at HTTP dispatch in
@@ -184,9 +166,7 @@ def application_unproxied(environ, start_response):
with openerp.api.Environment.manage():
# Try all handlers until one returns some result (i.e. not None).
wsgi_handlers = [wsgi_xmlrpc]
wsgi_handlers += module_handlers
for handler in wsgi_handlers:
for handler in [wsgi_xmlrpc, openerp.http.root]:
result = handler(environ, start_response)
if result is None:
continue