# ============LICENSE_START===================================================
-# Copyright (C) 2019-2020 Nordix Foundation.
+# Copyright (C) 2019-2021 Nordix Foundation.
# ============================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=====================================================
+import logging as logging
import os
+import pathlib
+import ssl
from urllib.parse import quote
from connexion import App
from flask_sqlalchemy import SQLAlchemy
-
-import mod.pmsh_logging as logger
+from onaplogging import monkey
+from onaplogging.mdcContext import MDC
+from ruamel.yaml import YAML
db = SQLAlchemy()
basedir = os.path.abspath(os.path.dirname(__file__))
_connexion_app = None
+logger = logging.getLogger('onap_logger')
def _get_app():
def launch_api_server(app_config):
connex_app = _get_app()
connex_app.add_api('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))
+ if app_config.enable_tls:
+ logger.info('Launching secure http API server')
+ ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
+ ssl_ctx.load_cert_chain(app_config.cert_path, app_config.key_path)
+ connex_app.run(port=os.environ.get('PMSH_API_PORT', '8443'), ssl_options=ssl_ctx,
+ server="tornado")
+ else:
+ logger.info('Launching unsecure http API server')
+ connex_app.run(port=os.environ.get('PMSH_API_PORT', '8443'), server="tornado")
def create_app():
- logger.create_loggers(os.getenv('LOGS_PATH'))
+ create_logger()
connex_app = _get_app()
app = connex_app.app
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
return app
+def create_logger():
+ config_file_path = os.getenv('LOGGER_CONFIG')
+ update_logging_config(config_file_path)
+ monkey.patch_loggingYaml()
+ logging.config.yamlConfig(filepath=config_file_path,
+ watchDog=os.getenv('DYNAMIC_LOGGER_CONFIG', True))
+ old_record = logging.getLogRecordFactory()
+
+ def augment_record(*args, **kwargs):
+ new_record = old_record(*args, **kwargs)
+ new_record.mdc = MDC.result()
+ return new_record
+
+ logging.setLogRecordFactory(augment_record)
+
+
+def update_logging_config(config_file_path):
+ config_yaml = YAML()
+ config_file = pathlib.Path(config_file_path)
+ data = config_yaml.load(config_file)
+ data['handlers']['onap_log_handler']['filename'] = \
+ f'{os.getenv("LOGS_PATH")}/application.log'
+ config_yaml.dump(data, config_file)
+
+
def get_db_connection_url():
pg_host = os.getenv('PMSH_PG_URL')
pg_user = os.getenv('PMSH_PG_USERNAME')
pg_user_pass = os.getenv('PMSH_PG_PASSWORD')
pmsh_db_name = os.getenv('PMSH_DB_NAME', 'pmsh')
pmsh_db_port = os.getenv('PMSH_PG_PORT', '5432')
- db_url = f'postgres+psycopg2://{quote(str(pg_user), safe="")}:' \
+ db_url = f'postgresql+psycopg2://{quote(str(pg_user), safe="")}:' \
f'{quote(str(pg_user_pass), safe="")}@{pg_host}:{pmsh_db_port}/{pmsh_db_name}'
if 'None' in db_url:
raise Exception(f'Invalid DB connection URL: {db_url} .. exiting app!')