Add basic healthcheck for PMSH 71/101671/7
authorAndyWalshe <andy.walshe@est.tech>
Thu, 13 Feb 2020 12:55:49 +0000 (12:55 +0000)
committerAndyWalshe <andy.walshe@est.tech>
Fri, 21 Feb 2020 09:36:48 +0000 (09:36 +0000)
Signed-off-by: AndyWalshe <andy.walshe@est.tech>
Issue-ID: DCAEGEN2-1842
Change-Id: Idef8542e9b063f457e402c25fdf369d885548674

components/pm-subscription-handler/Dockerfile
components/pm-subscription-handler/pmsh_service/mod/__init__.py
components/pm-subscription-handler/pmsh_service/mod/healthcheck.py [new file with mode: 0755]
components/pm-subscription-handler/pmsh_service/mod/pmsh_swagger.yml [new file with mode: 0644]
components/pm-subscription-handler/pmsh_service/pmsh_service_main.py
components/pm-subscription-handler/tests/test_healthcheck.py [new file with mode: 0755]

index b1f3129..8eed60b 100644 (file)
@@ -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
index 722188a..e09ec28 100644 (file)
@@ -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 (executable)
index 0000000..af82fc4
--- /dev/null
@@ -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 (file)
index 0000000..7bfecd8
--- /dev/null
@@ -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
index ab33032..5c81250 100755 (executable)
 # 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 (executable)
index 0000000..6e960d0
--- /dev/null
@@ -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')