From a86243058c2daa560aebaecdb096ff63788a6f44 Mon Sep 17 00:00:00 2001 From: PrakashH Date: Mon, 28 Jan 2019 20:49:02 +0000 Subject: Heartbeat Microservice Support Heartbeat service monitors missing HB notification Issue-ID: DCAEGEN2-267 Change-Id: I0fd191b2a3495202e22f633ada4a1350a97557ad Signed-off-by: PrakashH --- miss_htbt_service/config_notif.py | 56 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'miss_htbt_service/config_notif.py') diff --git a/miss_htbt_service/config_notif.py b/miss_htbt_service/config_notif.py index 242b0e9..3ca0ef0 100644 --- a/miss_htbt_service/config_notif.py +++ b/miss_htbt_service/config_notif.py @@ -23,9 +23,12 @@ import string import sys import socket import yaml +import json import psycopg2 from pathlib import Path import os.path as path +from mod.trapd_get_cbs_config import get_cbs_config +import mod.trapd_settings as tds hb_properties_file = path.abspath(path.join(__file__, "../config/hbproperties.yaml")) @@ -33,7 +36,11 @@ def postgres_db_open(username,password,host,port,database_name): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True - connection = psycopg2.connect(database=database_name, user = username, password = password, host = host, port =port) + try: + connection = psycopg2.connect(database=database_name, user = username, password = password, host = host, port =port) + except Exception as e: + print("HB_Notif::postgress connect error:", e) + connection = True return connection def db_table_creation_check(connection_db,table_name): @@ -68,7 +75,7 @@ def commit_and_close_db(connection_db): except(psycopg2.DatabaseError, e): return False -def read_hb_properties(): +def read_hb_properties_default(): #Read the hbproperties.yaml for postgress and CBS related data s=open(hb_properties_file, 'r') a=yaml.load(s) @@ -90,6 +97,29 @@ def read_hb_properties(): s.close() return ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval +def read_hb_properties(jsfile): + try: + with open(jsfile, 'r') as outfile: + cfg = json.load(outfile) + except(Exception) as err: + print("Json file read error - %s",err) + return read_hb_properties_default() + try: + ip_address = str(cfg['pg_ipAddress']) + port_num = str(cfg['pg_portNum']) + user_name = str(cfg['pg_userName']) + password = str(cfg['pg_passwd']) + dbName = str(cfg['pg_dbName']) + db_name = dbName.lower() + cbs_polling_required = str(cfg['CBS_polling_allowed']) + cbs_polling_interval = str(cfg['CBS_polling_interval']) + if("SERVICE_NAME" in cfg.keys()): + os.environ['SERVICE_NAME'] = str(cfg['SERVICE_NAME']) + except(Exception) as err: + print("Json file read parameter error -%s ",err) + return read_hb_properties_default() + return ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval + def read_hb_common(user_name,password,ip_address,port_num,db_name): envPytest = os.getenv('pytest', "") if (envPytest == 'test'): @@ -115,7 +145,7 @@ def read_hb_common(user_name,password,ip_address,port_num,db_name): def update_hb_common(update_flg, process_id, state, user_name,password,ip_address,port_num,db_name): current_time = int(round(time.time())) source_name = socket.gethostname() - source_name = source_name + "-" + str(os.getenv('SERVICE_NAME')) + source_name = source_name + "-" + str(os.getenv('SERVICE_NAME', "")) envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True @@ -127,9 +157,27 @@ def update_hb_common(update_flg, process_id, state, user_name,password,ip_addres cur.close() return True +def fetch_json_file(): + if get_cbs_config(): + current_runtime_config_file_name = "../etc/download1.json" + envPytest = os.getenv('pytest', "") + if (envPytest == 'test'): + jsfile = "../etc/config.json" + return jsfile + print("Config_N:current config logged to : %s" % current_runtime_config_file_name) + with open(current_runtime_config_file_name, 'w') as outfile: + json.dump(tds.c_config, outfile) + jsfile = current_runtime_config_file_name + else: + print("MSHBD:CBS Config not available, using local config") + jsfile = "../etc/config.json" + print("Config_N: The json file is - %s", jsfile) + return jsfile + #if __name__ == "__main__": def config_notif_run(): - ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval = read_hb_properties() + jsfile = fetch_json_file() + ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval = read_hb_properties(jsfile) envPytest = os.getenv('pytest', "") if (envPytest == 'test'): return True -- cgit 1.2.3-korg