summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndyWalshe <andy.walshe@est.tech>2020-02-13 12:55:49 +0000
committerAndyWalshe <andy.walshe@est.tech>2020-02-21 09:36:48 +0000
commitde549f5f1bb3e0a6f94e9755ae0800b469114113 (patch)
tree4a181006669fb8a9179563aee2d3f91ac0593511
parent06d595819918da57b6ce3ffeb9346c7913f2c66b (diff)
Add basic healthcheck for PMSH
Signed-off-by: AndyWalshe <andy.walshe@est.tech> Issue-ID: DCAEGEN2-1842 Change-Id: Idef8542e9b063f457e402c25fdf369d885548674
-rw-r--r--components/pm-subscription-handler/Dockerfile2
-rw-r--r--components/pm-subscription-handler/pmsh_service/mod/__init__.py17
-rwxr-xr-xcomponents/pm-subscription-handler/pmsh_service/mod/healthcheck.py30
-rw-r--r--components/pm-subscription-handler/pmsh_service/mod/pmsh_swagger.yml34
-rwxr-xr-xcomponents/pm-subscription-handler/pmsh_service/pmsh_service_main.py9
-rwxr-xr-xcomponents/pm-subscription-handler/tests/test_healthcheck.py27
6 files changed, 111 insertions, 8 deletions
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')