From de549f5f1bb3e0a6f94e9755ae0800b469114113 Mon Sep 17 00:00:00 2001 From: AndyWalshe Date: Thu, 13 Feb 2020 12:55:49 +0000 Subject: Add basic healthcheck for PMSH Signed-off-by: AndyWalshe Issue-ID: DCAEGEN2-1842 Change-Id: Idef8542e9b063f457e402c25fdf369d885548674 --- components/pm-subscription-handler/Dockerfile | 2 +- .../pmsh_service/mod/__init__.py | 17 ++++++++++- .../pmsh_service/mod/healthcheck.py | 30 +++++++++++++++++++ .../pmsh_service/mod/pmsh_swagger.yml | 34 ++++++++++++++++++++++ .../pmsh_service/pmsh_service_main.py | 9 ++---- .../tests/test_healthcheck.py | 27 +++++++++++++++++ 6 files changed, 111 insertions(+), 8 deletions(-) create mode 100755 components/pm-subscription-handler/pmsh_service/mod/healthcheck.py create mode 100644 components/pm-subscription-handler/pmsh_service/mod/pmsh_swagger.yml create mode 100755 components/pm-subscription-handler/tests/test_healthcheck.py (limited to 'components') diff --git a/components/pm-subscription-handler/Dockerfile b/components/pm-subscription-handler/Dockerfile index b1f3129b..8eed60b3 100644 --- a/components/pm-subscription-handler/Dockerfile +++ b/components/pm-subscription-handler/Dockerfile @@ -51,4 +51,4 @@ RUN pip install --upgrade pip && \ USER $PMSHUSER # run the app -ENTRYPOINT ["python", "./bin/pmsh_service.py"] \ No newline at end of file +ENTRYPOINT ["python", "./bin/pmsh_service_main.py"] \ No newline at end of file diff --git a/components/pm-subscription-handler/pmsh_service/mod/__init__.py b/components/pm-subscription-handler/pmsh_service/mod/__init__.py index 722188ae..e09ec285 100644 --- a/components/pm-subscription-handler/pmsh_service/mod/__init__.py +++ b/components/pm-subscription-handler/pmsh_service/mod/__init__.py @@ -25,11 +25,26 @@ import mod.pmsh_logging as logger db = SQLAlchemy() basedir = os.path.abspath(os.path.dirname(__file__)) +_connexion_app = None + + +def _get_app(): + global _connexion_app + if not _connexion_app: + _connexion_app = App(__name__, specification_dir=basedir) + return _connexion_app + + +def launch_api_server(app_config): + connex_app = _get_app() + connex_app.add_api('pmsh_swagger.yml') + connex_app.run(port=os.environ.get('PMSH_API_PORT', '8443'), + ssl_context=(app_config.cert_path, app_config.key_path)) def create_app(): logger.create_loggers(os.getenv('LOGS_PATH')) - connex_app = App(__name__, specification_dir=basedir) + connex_app = _get_app() app = connex_app.app app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_RECORD_QUERIES'] = True diff --git a/components/pm-subscription-handler/pmsh_service/mod/healthcheck.py b/components/pm-subscription-handler/pmsh_service/mod/healthcheck.py new file mode 100755 index 00000000..af82fc4e --- /dev/null +++ b/components/pm-subscription-handler/pmsh_service/mod/healthcheck.py @@ -0,0 +1,30 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019-2020 Nordix Foundation. +# ============================================================================ +# 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. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + + +def status(): + """ + Returns the health of the PMSH service + Args: + NA + Returns: + Dictionary detailing 'status' of either 'healthy' or 'unhealthy'. + Raises: + NA + """ + return {'status': 'healthy'} diff --git a/components/pm-subscription-handler/pmsh_service/mod/pmsh_swagger.yml b/components/pm-subscription-handler/pmsh_service/mod/pmsh_swagger.yml new file mode 100644 index 00000000..7bfecd81 --- /dev/null +++ b/components/pm-subscription-handler/pmsh_service/mod/pmsh_swagger.yml @@ -0,0 +1,34 @@ +swagger: "2.0" +info: + title: PM Subscription Handler Service + version: "1.0.0" + description: This is the swagger file that outlines the PM subscription handler api +consumes: + - "application/json" +produces: + - "application/json" + +schemes: + - https + +# Paths supported by the server application +paths: + /healthcheck: + get: + operationId: "mod.healthcheck.status" + tags: + - "HealthCheck" + description: >- + This is the health check endpoint. If this returns a 200, the server is alive. + responses: + 200: + description: Successful response + schema: + type: object + properties: + status: + type: string + description: Overall health of PMSH + enum: [healthy, unhealthy] + 503: + description: the pmsh service is unavailable diff --git a/components/pm-subscription-handler/pmsh_service/pmsh_service_main.py b/components/pm-subscription-handler/pmsh_service/pmsh_service_main.py index ab330320..5c81250f 100755 --- a/components/pm-subscription-handler/pmsh_service/pmsh_service_main.py +++ b/components/pm-subscription-handler/pmsh_service/pmsh_service_main.py @@ -16,12 +16,11 @@ # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END===================================================== import sys -import time import threading import mod.aai_client as aai import mod.pmsh_logging as logger -from mod import db, create_app +from mod import db, create_app, launch_api_server from mod.config_handler import ConfigHandler from mod.pmsh_utils import AppConfig from mod.subscription import Subscription, AdministrativeState @@ -81,14 +80,12 @@ def main(): threading.Timer(20.0, mr_sub.poll_policy_topic, [sub.subscriptionName, app]).start() + launch_api_server(app_conf) + except Exception as e: logger.debug(f'Failed to Init PMSH: {e}') sys.exit(e) - while True: - logger.debug(Subscription.get_all_nfs_subscription_relations()) - time.sleep(5) - if __name__ == '__main__': main() diff --git a/components/pm-subscription-handler/tests/test_healthcheck.py b/components/pm-subscription-handler/tests/test_healthcheck.py new file mode 100755 index 00000000..6e960d05 --- /dev/null +++ b/components/pm-subscription-handler/tests/test_healthcheck.py @@ -0,0 +1,27 @@ +# ============LICENSE_START=================================================== +# Copyright (C) 2019-2020 Nordix Foundation. +# ============================================================================ +# 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. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END===================================================== + +import unittest + +from pmsh_service.mod.healthcheck import status + + +class HealthcheckTestCase(unittest.TestCase): + + def test_status_response_healthy(self): + self.assertEqual(status()['status'], 'healthy') -- cgit 1.2.3-korg