From 778093da01ac9a173aa660f1474a8c14fc259dd1 Mon Sep 17 00:00:00 2001 From: Robert Putt Date: Fri, 15 Dec 2017 17:38:22 +0000 Subject: [PATCH] Split entrypoints into api and worker for simplicity. Change-Id: I7045fd4b8a0b0a70265bafb16505f0c145cb53f6 --- nemesis.conf.sample | 1 + python_nemesis/api_app.py | 32 +++++++++++++++++++ python_nemesis/base_app.py | 17 +--------- python_nemesis/config.py | 3 +- python_nemesis/plugins/null_plugin.py | 22 +++++++++++++ python_nemesis/worker/__init__.py | 46 +++++++++++++++++++++++++++ python_nemesis/worker/loader.py | 25 +++++++++++++++ python_nemesis/worker_app.py | 29 +++++++++++++++++ 8 files changed, 158 insertions(+), 17 deletions(-) create mode 100644 python_nemesis/api_app.py create mode 100644 python_nemesis/plugins/null_plugin.py create mode 100644 python_nemesis/worker/__init__.py create mode 100644 python_nemesis/worker/loader.py create mode 100644 python_nemesis/worker_app.py diff --git a/nemesis.conf.sample b/nemesis.conf.sample index 7ab0f2b..c38f650 100644 --- a/nemesis.conf.sample +++ b/nemesis.conf.sample @@ -1,6 +1,7 @@ [DEFAULT] rpc_backend = rabbit transport_url = rabbit://:@/ +analysis_plugins = [keystone_authtoken] identity_uri = diff --git a/python_nemesis/api_app.py b/python_nemesis/api_app.py new file mode 100644 index 0000000..8316ef8 --- /dev/null +++ b/python_nemesis/api_app.py @@ -0,0 +1,32 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from python_nemesis.api.v1 import V1_API +from python_nemesis.base_app import configure_app +from python_nemesis.base_app import configure_blueprints +from python_nemesis.base_app import configure_extensions +from python_nemesis.base_app import configure_notifier +from python_nemesis.base_app import create_app + + +def create_api_app(): + app = create_app('nemesis-api') + configure_app(app) + configure_extensions(app) + configure_notifier(app) + configure_blueprints(app, [V1_API]) + return app + + +if __name__ == "__main__": # pragma: no cover + app = create_api_app() + app.run(threaded=True) diff --git a/python_nemesis/base_app.py b/python_nemesis/base_app.py index b5c3fed..7338eb3 100644 --- a/python_nemesis/base_app.py +++ b/python_nemesis/base_app.py @@ -78,7 +78,7 @@ def configure_notifier(app): topics=topics) -def create_app(app_name=None, blueprints=None): +def create_app(app_name=None): """Create the flask app. This function is intended to be used with the app factory @@ -91,19 +91,4 @@ def create_app(app_name=None, blueprints=None): :rtype: :py:class:`flask.Flask` """ app = Flask(app_name) - - configure_app(app) - configure_extensions(app) - configure_notifier(app) - - # Here we register the application blueprints. - from python_nemesis.api.v1 import V1_API - blueprints = [V1_API] - configure_blueprints(app, blueprints) - return app - - -if __name__ == "__main__": # pragma: no cover - app = create_app('nemesis-api') - app.run(threaded=True) diff --git a/python_nemesis/config.py b/python_nemesis/config.py index a6dd748..1327f11 100644 --- a/python_nemesis/config.py +++ b/python_nemesis/config.py @@ -11,11 +11,12 @@ # under the License. from oslo_config import cfg +from oslo_config import types DEFAULT_OPT_GRP = cfg.OptGroup(name='DEFAULT') DEFAULT_OPTS = [ - cfg.StrOpt('test_value', default="this is a value") + cfg.ListOpt('analysis_plugins', item_type=types.String()), ] SQLALCHEMY_OPT_GRP = cfg.OptGroup(name='sqlalchemy') diff --git a/python_nemesis/plugins/null_plugin.py b/python_nemesis/plugins/null_plugin.py new file mode 100644 index 0000000..264d5c3 --- /dev/null +++ b/python_nemesis/plugins/null_plugin.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +class NemesisPlugin(object): + + def __init__(self, file_path): + self.file_path = file_path + + def analyse(self): + return None diff --git a/python_nemesis/worker/__init__.py b/python_nemesis/worker/__init__.py new file mode 100644 index 0000000..6dca6b7 --- /dev/null +++ b/python_nemesis/worker/__init__.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from flask import current_app +import oslo_messaging + + +class NewFileEndpoint(object): + filter_rule = oslo_messaging.NotificationFilter( + event_type='nemsis.new_file') + + def info(self, ctxt, publisher_id, event_type, payload, metadata): + print(payload) + + +def run_worker(): + cfg = current_app.config['cfg'] + transport = oslo_messaging.get_notification_transport(cfg) + + targets = [ + oslo_messaging.Target(topic='nemesis_notifications') + ] + endpoints = [ + NewFileEndpoint() + ] + + pool = "nemesis_notifications.info" + server = oslo_messaging.get_notification_listener(transport, + targets, + endpoints, + executor='threading', + pool=pool) + + server.start() + server.wait() diff --git a/python_nemesis/worker/loader.py b/python_nemesis/worker/loader.py new file mode 100644 index 0000000..a58d930 --- /dev/null +++ b/python_nemesis/worker/loader.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import importlib + + +def load_class(class_str): + # Dynamically loads a class based on the string provided. + class_data = class_str.split(".") + module_path = ".".join(class_data[:-1]) + class_str = class_data[-1] + + module = importlib.import_module(module_path) + return getattr(module, class_str) diff --git a/python_nemesis/worker_app.py b/python_nemesis/worker_app.py new file mode 100644 index 0000000..7b5d01e --- /dev/null +++ b/python_nemesis/worker_app.py @@ -0,0 +1,29 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from python_nemesis.base_app import configure_app +from python_nemesis.base_app import configure_extensions +from python_nemesis.base_app import create_app +from python_nemesis.worker import run_worker + + +def create_worker_app(): + app = create_app('nemesis-worker') + configure_app(app) + configure_extensions(app) + return app + + +if __name__ == "__main__": # pragma: no cover + app = create_worker_app() + with app.app_context(): + run_worker()