From 92af0732ea5cce5b9c62ca01e06fb6eac28987e7 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 9 Feb 2018 22:59:21 +0800 Subject: [PATCH] pybind/mgr/restful: move global variable into context.py otherwise we need to tackle with the circular import. it's not fatal, if we can "import foo" in py2 and "from . import foo" in py3, respectively. but we cannot unify them using "from . import foo", because the circular reference issue. and conditionalize the import statement is ugly. so, i think we'd better break the loop. so i am moving the "instance" variable into its own file. Signed-off-by: Kefu Chai --- src/pybind/mgr/restful/api/config.py | 12 ++++++------ src/pybind/mgr/restful/api/crush.py | 6 +++--- src/pybind/mgr/restful/api/doc.py | 4 ++-- src/pybind/mgr/restful/api/mon.py | 6 +++--- src/pybind/mgr/restful/api/osd.py | 14 +++++++------- src/pybind/mgr/restful/api/pool.py | 16 ++++++++-------- src/pybind/mgr/restful/api/request.py | 24 ++++++++++++------------ src/pybind/mgr/restful/api/server.py | 6 +++--- src/pybind/mgr/restful/context.py | 2 ++ src/pybind/mgr/restful/decorators.py | 8 ++++---- src/pybind/mgr/restful/hooks.py | 4 ++-- src/pybind/mgr/restful/module.py | 9 +++------ 12 files changed, 55 insertions(+), 56 deletions(-) create mode 100644 src/pybind/mgr/restful/context.py diff --git a/src/pybind/mgr/restful/api/config.py b/src/pybind/mgr/restful/api/config.py index 51fabfad4d6..4176c003c4a 100644 --- a/src/pybind/mgr/restful/api/config.py +++ b/src/pybind/mgr/restful/api/config.py @@ -1,7 +1,7 @@ from pecan import expose, request from pecan.rest import RestController -from restful import common, module +from restful import common, context from restful.decorators import auth @@ -12,7 +12,7 @@ class ConfigOsd(RestController): """ Show OSD configuration options """ - flags = module.instance.get("osd_map")['flags'] + flags = context.instance.get("osd_map")['flags'] # pause is a valid osd config command that sets pauserd,pausewr flags = flags.replace('pauserd,pausewr', 'pause') @@ -33,7 +33,7 @@ class ConfigOsd(RestController): valid_flags = set(args.keys()) & set(common.OSD_FLAGS) invalid_flags = list(set(args.keys()) - valid_flags) if invalid_flags: - module.instance.log.warn("%s not valid to set/unset" % invalid_flags) + context.instance.log.warn("%s not valid to set/unset" % invalid_flags) for flag in list(valid_flags): if args[flag]: @@ -46,7 +46,7 @@ class ConfigOsd(RestController): 'key': flag, }) - return module.instance.submit_request([commands], **kwargs) + return context.instance.submit_request([commands], **kwargs) @@ -61,7 +61,7 @@ class ConfigClusterKey(RestController): """ Show specific configuration option """ - return module.instance.get("config").get(self.key, None) + return context.instance.get("config").get(self.key, None) @@ -72,7 +72,7 @@ class ConfigCluster(RestController): """ Show all cluster configuration options """ - return module.instance.get("config") + return context.instance.get("config") @expose() diff --git a/src/pybind/mgr/restful/api/crush.py b/src/pybind/mgr/restful/api/crush.py index 11a04264da2..ee8589df0a4 100644 --- a/src/pybind/mgr/restful/api/crush.py +++ b/src/pybind/mgr/restful/api/crush.py @@ -1,7 +1,7 @@ from pecan import expose from pecan.rest import RestController -from restful import common, module +from restful import common, context from collections import defaultdict from restful.decorators import auth @@ -14,8 +14,8 @@ class CrushRule(RestController): """ Show crush rules """ - rules = module.instance.get('osd_map_crush')['rules'] - nodes = module.instance.get('osd_map_tree')['nodes'] + rules = context.instance.get('osd_map_crush')['rules'] + nodes = context.instance.get('osd_map_tree')['nodes'] for rule in rules: rule['osd_count'] = len(common.crush_rule_osds(nodes, rule)) diff --git a/src/pybind/mgr/restful/api/doc.py b/src/pybind/mgr/restful/api/doc.py index 96e4d6a3379..f1038c21b16 100644 --- a/src/pybind/mgr/restful/api/doc.py +++ b/src/pybind/mgr/restful/api/doc.py @@ -1,7 +1,7 @@ from pecan import expose from pecan.rest import RestController -from restful import module +from restful import context import restful @@ -12,4 +12,4 @@ class Doc(RestController): """ Show documentation information """ - return module.instance.get_doc_api(restful.api.Root) + return context.instance.get_doc_api(restful.api.Root) diff --git a/src/pybind/mgr/restful/api/mon.py b/src/pybind/mgr/restful/api/mon.py index 5b19aaefb29..807d6df3bf7 100644 --- a/src/pybind/mgr/restful/api/mon.py +++ b/src/pybind/mgr/restful/api/mon.py @@ -1,7 +1,7 @@ from pecan import expose, response from pecan.rest import RestController -from restful import module +from restful import context from restful.decorators import auth @@ -18,7 +18,7 @@ class MonName(RestController): """ mon = filter( lambda x: x['name'] == self.name, - module.instance.get_mons() + context.instance.get_mons() ) if len(mon) != 1: @@ -36,7 +36,7 @@ class Mon(RestController): """ Show the information for all the monitors """ - return module.instance.get_mons() + return context.instance.get_mons() @expose() diff --git a/src/pybind/mgr/restful/api/osd.py b/src/pybind/mgr/restful/api/osd.py index b42f33941f1..99f9ed4c8e7 100644 --- a/src/pybind/mgr/restful/api/osd.py +++ b/src/pybind/mgr/restful/api/osd.py @@ -1,7 +1,7 @@ from pecan import expose, request, response from pecan.rest import RestController -from restful import common, module +from restful import common, context from restful.decorators import auth @@ -16,7 +16,7 @@ class OsdIdCommand(RestController): """ Show implemented commands for the OSD id """ - osd = module.instance.get_osd_by_id(self.osd_id) + osd = context.instance.get_osd_by_id(self.osd_id) if not osd: response.status = 500 @@ -36,7 +36,7 @@ class OsdIdCommand(RestController): """ command = request.json.get('command', None) - osd = module.instance.get_osd_by_id(self.osd_id) + osd = context.instance.get_osd_by_id(self.osd_id) if not osd: response.status = 500 @@ -46,7 +46,7 @@ class OsdIdCommand(RestController): response.status = 500 return {'message': 'Command "%s" not available' % command} - return module.instance.submit_request([[{ + return context.instance.submit_request([[{ 'prefix': 'osd ' + command, 'who': str(self.osd_id) }]], **kwargs) @@ -65,7 +65,7 @@ class OsdId(RestController): """ Show the information for the OSD id """ - osd = module.instance.get_osds(ids=[str(self.osd_id)]) + osd = context.instance.get_osds(ids=[str(self.osd_id)]) if len(osd) != 1: response.status = 500 return {'message': 'Failed to identify the OSD id "%d"' % self.osd_id} @@ -112,7 +112,7 @@ class OsdId(RestController): 'weight': args['reweight'] }) - return module.instance.submit_request([commands], **kwargs) + return context.instance.submit_request([commands], **kwargs) @@ -127,7 +127,7 @@ class Osd(RestController): # TODO Filter by ids pool_id = kwargs.get('pool', None) - return module.instance.get_osds(pool_id) + return context.instance.get_osds(pool_id) @expose() diff --git a/src/pybind/mgr/restful/api/pool.py b/src/pybind/mgr/restful/api/pool.py index b9d2a8bae20..abf3e98133a 100644 --- a/src/pybind/mgr/restful/api/pool.py +++ b/src/pybind/mgr/restful/api/pool.py @@ -1,7 +1,7 @@ from pecan import expose, request, response from pecan.rest import RestController -from restful import common, module +from restful import common, context from restful.decorators import auth @@ -16,7 +16,7 @@ class PoolId(RestController): """ Show the information for the pool id """ - pool = module.instance.get_pool_by_id(self.pool_id) + pool = context.instance.get_pool_by_id(self.pool_id) if not pool: response.status = 500 @@ -41,7 +41,7 @@ class PoolId(RestController): return {'message': 'Bad request: malformed JSON or wrong Content-Type'} # Get the pool info for its name - pool = module.instance.get_pool_by_id(self.pool_id) + pool = context.instance.get_pool_by_id(self.pool_id) if not pool: response.status = 500 return {'message': 'Failed to identify the pool id "%d"' % self.pool_id} @@ -53,7 +53,7 @@ class PoolId(RestController): return {'message': 'Invalid arguments found: "%s"' % str(invalid)} # Schedule the update request - return module.instance.submit_request(common.pool_update_commands(pool['pool_name'], args), **kwargs) + return context.instance.submit_request(common.pool_update_commands(pool['pool_name'], args), **kwargs) @expose(template='json') @@ -62,13 +62,13 @@ class PoolId(RestController): """ Remove the pool data for the pool id """ - pool = module.instance.get_pool_by_id(self.pool_id) + pool = context.instance.get_pool_by_id(self.pool_id) if not pool: response.status = 500 return {'message': 'Failed to identify the pool id "%d"' % self.pool_id} - return module.instance.submit_request([[{ + return context.instance.submit_request([[{ 'prefix': 'osd pool delete', 'pool': pool['pool_name'], 'pool2': pool['pool_name'], @@ -84,7 +84,7 @@ class Pool(RestController): """ Show the information for all the pools """ - pools = module.instance.get('osd_map')['pools'] + pools = context.instance.get('osd_map')['pools'] # pgp_num is called pg_placement_num, deal with that for pool in pools: @@ -128,7 +128,7 @@ class Pool(RestController): return {'message': 'Invalid arguments found: "%s"' % str(invalid)} # Schedule the creation and update requests - return module.instance.submit_request( + return context.instance.submit_request( [[create_command]] + common.pool_update_commands(pool_name, args), **kwargs diff --git a/src/pybind/mgr/restful/api/request.py b/src/pybind/mgr/restful/api/request.py index b22b80bff8a..82c7fe16a75 100644 --- a/src/pybind/mgr/restful/api/request.py +++ b/src/pybind/mgr/restful/api/request.py @@ -1,7 +1,7 @@ from pecan import expose, request, response from pecan.rest import RestController -from restful import module +from restful import context from restful.decorators import auth, lock, paginate @@ -18,7 +18,7 @@ class RequestId(RestController): """ request = filter( lambda x: x.id == self.request_id, - module.instance.requests + context.instance.requests ) if len(request) != 1: @@ -36,9 +36,9 @@ class RequestId(RestController): """ Remove the request id from the database """ - for index in range(len(module.instance.requests)): - if module.instance.requests[index].id == self.request_id: - return module.instance.requests.pop(index) + for index in range(len(context.instance.requests)): + if context.instance.requests[index].id == self.request_id: + return context.instance.requests.pop(index) # Failed to find the job to cancel response.status = 500 @@ -54,7 +54,7 @@ class Request(RestController): """ List all the available requests """ - return module.instance.requests + return context.instance.requests @expose(template='json') @@ -64,17 +64,17 @@ class Request(RestController): """ Remove all the finished requests """ - num_requests = len(module.instance.requests) + num_requests = len(context.instance.requests) - module.instance.requests = filter( + context.instance.requests = filter( lambda x: not x.is_finished(), - module.instance.requests + context.instance.requests ) # Return the job statistics return { - 'cleaned': num_requests - len(module.instance.requests), - 'remaining': len(module.instance.requests), + 'cleaned': num_requests - len(context.instance.requests), + 'remaining': len(context.instance.requests), } @@ -84,7 +84,7 @@ class Request(RestController): """ Pass through method to create any request """ - return module.instance.submit_request([[request.json]], **kwargs) + return context.instance.submit_request([[request.json]], **kwargs) @expose() diff --git a/src/pybind/mgr/restful/api/server.py b/src/pybind/mgr/restful/api/server.py index 9e6a20784ec..8ce63493754 100644 --- a/src/pybind/mgr/restful/api/server.py +++ b/src/pybind/mgr/restful/api/server.py @@ -1,7 +1,7 @@ from pecan import expose from pecan.rest import RestController -from restful import module +from restful import context from restful.decorators import auth @@ -16,7 +16,7 @@ class ServerFqdn(RestController): """ Show the information for the server fqdn """ - return module.instance.get_server(self.fqdn) + return context.instance.get_server(self.fqdn) @@ -27,7 +27,7 @@ class Server(RestController): """ Show the information for all the servers """ - return module.instance.list_servers() + return context.instance.list_servers() @expose() diff --git a/src/pybind/mgr/restful/context.py b/src/pybind/mgr/restful/context.py new file mode 100644 index 00000000000..a05ea8548df --- /dev/null +++ b/src/pybind/mgr/restful/context.py @@ -0,0 +1,2 @@ +# Global instance to share +instance = None diff --git a/src/pybind/mgr/restful/decorators.py b/src/pybind/mgr/restful/decorators.py index e5d7d62ae87..47f82f5a68d 100644 --- a/src/pybind/mgr/restful/decorators.py +++ b/src/pybind/mgr/restful/decorators.py @@ -4,7 +4,7 @@ from functools import wraps import traceback -import module +from . import context # Handle authorization @@ -19,13 +19,13 @@ def auth(f): username, password = b64decode(request.authorization[1]).split(':') # Check that the username exists - if username not in module.instance.keys: + if username not in context.instance.keys: response.status = 401 response.headers['WWW-Authenticate'] = 'Basic realm="Login Required"' return {'message': 'auth: No such user'} # Check the password - if module.instance.keys[username] != password: + if context.instance.keys[username] != password: response.status = 401 response.headers['WWW-Authenticate'] = 'Basic realm="Login Required"' return {'message': 'auth: Incorrect password'} @@ -38,7 +38,7 @@ def auth(f): def lock(f): @wraps(f) def decorated(*args, **kwargs): - with module.instance.requests_lock: + with context.instance.requests_lock: return f(*args, **kwargs) return decorated diff --git a/src/pybind/mgr/restful/hooks.py b/src/pybind/mgr/restful/hooks.py index 8a62c84fc3a..7623a71538f 100644 --- a/src/pybind/mgr/restful/hooks.py +++ b/src/pybind/mgr/restful/hooks.py @@ -2,8 +2,8 @@ from pecan.hooks import PecanHook import traceback -from . import module +from . import context class ErrorHook(PecanHook): def on_error(self, stat, exc): - module.instance.log.error(str(traceback.format_exc())) + context.instance.log.error(str(traceback.format_exc())) diff --git a/src/pybind/mgr/restful/module.py b/src/pybind/mgr/restful/module.py index b67474d7911..72af5b8464c 100644 --- a/src/pybind/mgr/restful/module.py +++ b/src/pybind/mgr/restful/module.py @@ -13,6 +13,7 @@ import traceback import socket from . import common +from . import context from uuid import uuid4 from pecan import jsonify, make_app @@ -29,9 +30,6 @@ try: except: iteritems = dict.items -# Global instance to share -instance = None - class CannotServe(Exception): pass @@ -93,7 +91,7 @@ class CommandsRequest(object): results.append(result) # Run the command - instance.send_command(result, 'mon', '', json.dumps(commands[index]), tag) + context.instance.send_command(result, 'mon', '', json.dumps(commands[index]), tag) return results @@ -234,8 +232,7 @@ class Module(MgrModule): def __init__(self, *args, **kwargs): super(Module, self).__init__(*args, **kwargs) - global instance - instance = self + context.instance = self self.requests = [] self.requests_lock = threading.RLock() -- 2.39.5