Index: cloud_sync.py =================================================================== diff -u -r95e6c4e3c00fc928880a99aad15f4c906cd02d7e -rb02f69db02eb8157d85f8d5c5b4e571784a6c212 --- cloud_sync.py (.../cloud_sync.py) (revision 95e6c4e3c00fc928880a99aad15f4c906cd02d7e) +++ cloud_sync.py (.../cloud_sync.py) (revision b02f69db02eb8157d85f8d5c5b4e571784a6c212) @@ -1,76 +1,88 @@ -import os -import sys -import logging -from logging.handlers import TimedRotatingFileHandler - import werkzeug werkzeug.cached_property = werkzeug.utils.cached_property from flask import Flask, Response, request from flask_restplus import Api, Resource, fields, reqparse -from cloudsync.utils.helpers import * -from cloudsync.utils.globals import * -from cloudsync.utils.reachability import ReachabilityProvider -from cloudsync.busses.file_output_bus import FileOutputBus -from cloudsync.handlers.error_handler import ErrorHandler -from cloudsync.handlers.cs_mft_dcs_request_handler import NetworkRequestHandler -from cloudsync.handlers.ui_cs_request_handler import UICSMessageHandler -from cloudsync.busses.file_input_bus import FileInputBus -from cloudsync.utils.heartbeat import HeartBeatProvider from cloudsync.handlers.error import Error +from cloudsync.utils.heartbeat import HeartBeatProvider +from cloudsync.busses.file_input_bus import FileInputBus +from cloudsync.handlers.ui_cs_request_handler import UICSMessageHandler +from cloudsync.handlers.cs_mft_dcs_request_handler import NetworkRequestHandler +from cloudsync.handlers.error_handler import ErrorHandler +from cloudsync.busses.file_output_bus import FileOutputBus +from cloudsync.utils.reachability import ReachabilityProvider +from cloudsync.utils.globals import * +from cloudsync.utils.helpers import * +from cloudsync.utils.logging import LoggingConfig +import os +import sys -VERSION = "0.4.8" +VERSION = "0.4.15" + arguments = sys.argv -log_level = int(arguments[1]) -if not os.path.exists(CS_LOG_PATH): - os.makedirs(CS_LOG_PATH) - app = Flask(__name__) api = Api(app=app, version=VERSION, title="CloudSync Registration API", description="Interface with DIA Manufacturing Tool * DCS") -# Remove existing handlers -for handler in app.logger.handlers: - app.logger.removeHandler(handler) +logconf = LoggingConfig() +logconf.initiate(app=app) -# Create a TimedRotatingFileHandler that writes logs to a new file every midnight, in UTC time -handler = TimedRotatingFileHandler(CS_LOG_FILE, when="midnight", interval=1, utc=True) -handler.suffix = "%m-%d-%Y" +sleep(5) # wait for UI to prepare the configurations partition -# Set the log level -handler.setLevel(log_level) +try: + ok = False + print(SETUP_CONSOLE_LINE) -# Add a formatter -default_formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s | {%(pathname)s:%(lineno)d}') -handler.setFormatter(default_formatter) + if EXEC_MODE_UPGRADE_KEY in arguments: # ---------- upgrade + # Read from $HOME + if os.path.isfile(CONFIG_PATH) and os.access(CONFIG_PATH, os.R_OK | os.W_OK): #TODO test if this check needed? + EXEC_MODE = EXEC_MODE_UPGRADE + g_config = helpers_read_config(CONFIG_PATH) + ok = True + else: + if EXEC_MODE_UPDATE_KEY in arguments: # ---------- update + EXEC_MODE = EXEC_MODE_UPDATE + print(f"CloudSync starting in {EXEC_MODE} mode") + print("CloudSync update config started...") + # Update the $HOME from /var/configuraitons/ and update result in both for later update + oldConfig = helpers_read_config(OPERATION_CONFIG_FILE_PATH) + newConfig = helpers_read_config(CONFIG_PATH) + newConfig.update(oldConfig) + helpers_write_config(None , CONFIG_PATH , newConfig) + helpers_write_config(OPERATION_CONFIG_PATH, OPERATION_CONFIG_FILE_PATH, newConfig) + print("CloudSync update config done.") -# Add the handler to the logger -app.logger.addHandler(handler) -app.logger.setLevel(log_level) + # Read from /var/configuraitons/ # ---------- normal + if os.path.isfile(OPERATION_CONFIG_FILE_PATH) and os.access(OPERATION_CONFIG_FILE_PATH, os.R_OK): #TODO test if this check needed? + g_config = helpers_read_config(OPERATION_CONFIG_FILE_PATH) + CONFIG_PATH = OPERATION_CONFIG_FILE_PATH + ok = True -# Get the root logger -root_logger = logging.getLogger() + if ok: + print(f"CloudSync started in {EXEC_MODE} mode") + print(f"Using config: {CONFIG_PATH}") + else: + g_utils.logger.error(f"Error reading config file in {EXEC_MODE}") -# Add the handlers to the root logger -root_logger.addHandler(handler) -root_logger.setLevel(log_level) + print(SETUP_CONSOLE_LINE) -g_utils.add_logger(app.logger) +except Exception as e: + g_utils.logger.error(f"Error reading config file - {e}") + print(SETUP_CONSOLE_LINE) + sys.exit(0) -sleep(5) # wait for UI to prepare the configurations partition - try: - g_config = helpers_read_config(CONFIG_PATH) - - if g_config[CONFIG_DEVICE][CONFIG_DEVICE_MODE] == 'operation': - g_config = helpers_read_config(OPERATION_CONFIG_FILE_PATH) - CONFIG_PATH = OPERATION_CONFIG_FILE_PATH - reachability_provider = ReachabilityProvider(logger=app.logger, url_reachability=g_config[CONFIG_KEBORMED][CONFIG_KEBORMED_REACHABILITY_URL]) +except Exception as e: + g_utils.logger.error( + "Reachability URL missing from config file. Using Default URL - {0}".format(DEFAULT_REACHABILITY_URL)) + reachability_provider = ReachabilityProvider( + logger=app.logger, url_reachability=DEFAULT_REACHABILITY_URL) +try: g_utils.add_reachability_provider(reachability_provider=reachability_provider) output_channel = FileOutputBus(logger=app.logger, max_size=100, file_channels_path=UI2CS_FILE_CHANNELS_PATH) @@ -91,6 +103,11 @@ heartbeat_provider = HeartBeatProvider(logger=app.logger, network_request_handler=network_request_handler, output_channel=output_channel) + logconf.set_network_provider(network_request_handler=network_request_handler) + logconf.set_error_provider(error_handler=error_handler) + logconf.set_configuration(g_config=g_config) + logconf.set_log_level(g_config[CONFIG_LOGS][CONFIG_LOGS_DEFAULT_LOG_LEVEL]) + if g_config[CONFIG_DEVICE][CONFIG_DEVICE_MODE] == 'operation': heartbeat_provider.send_heartbeat = True else: @@ -231,7 +248,7 @@ def main(): if g_config[CONFIG_DEVICE][CONFIG_DEVICE_MODE] == 'registration': - app.run(debug=False, use_reloader=False, host="0.0.0.0", port=g_config[CONFIG_DEVICE][CONFIG_DEVICE_PORT]) + app.run(debug=False, use_reloader=False, host="0.0.0.0",port=g_config[CONFIG_DEVICE][CONFIG_DEVICE_PORT]) else: while True: sleep(1)