Index: cloud_sync.py =================================================================== diff -u -rcc7bbd932684e69aa456c114596625546630903e -rb02f69db02eb8157d85f8d5c5b4e571784a6c212 --- cloud_sync.py (.../cloud_sync.py) (revision cc7bbd932684e69aa456c114596625546630903e) +++ cloud_sync.py (.../cloud_sync.py) (revision b02f69db02eb8157d85f8d5c5b4e571784a6c212) @@ -1,64 +1,88 @@ -"""Implementation of CloudSync controller""" - -import argparse -import logging -import socket -import sys - 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.3" -try: - arguments = sys.argv - log_level = int(arguments[1]) +VERSION = "0.4.15" - if not os.path.exists(CS_LOG_PATH): - os.makedirs(CS_LOG_PATH) +arguments = sys.argv - logging.basicConfig(filename=CS_LOG_FILE, - level=log_level, - format='[%(asctime)s]: %(levelname)s - %(message)s | {%(pathname)s:%(lineno)d}', - ) +app = Flask(__name__) +api = Api(app=app, version=VERSION, title="CloudSync Registration API", + description="Interface with DIA Manufacturing Tool * DCS") - app = Flask(__name__) - api = Api(app=app, version=VERSION, title="CloudSync Registration API", - description="Interface with DIA Manufacturing Tool * DCS") +logconf = LoggingConfig() +logconf.initiate(app=app) - for handler in app.logger.handlers: - app.logger.removeHandler(handler) - handler = logging.FileHandler(CS_LOG_FILE) - handler.setLevel(log_level) - default_formatter = logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s') - handler.setFormatter(default_formatter) +sleep(5) # wait for UI to prepare the configurations partition - app.logger.setLevel(log_level) +try: + ok = False + print(SETUP_CONSOLE_LINE) - g_utils.add_logger(app.logger) + 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.") - g_config = helpers_read_config(CONFIG_PATH) + # 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 - if g_config[CONFIG_DEVICE][CONFIG_DEVICE_MODE] == 'operation': - helpers_read_config(OPERATION_CONFIG_FILE_PATH) - CONFIG_PATH = OPERATION_CONFIG_FILE_PATH + 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}") - reachability_provider = ReachabilityProvider(logger=app.logger) + print(SETUP_CONSOLE_LINE) +except Exception as e: + g_utils.logger.error(f"Error reading config file - {e}") + print(SETUP_CONSOLE_LINE) + sys.exit(0) + +try: + 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) @@ -68,15 +92,22 @@ network_request_handler = NetworkRequestHandler(logger=app.logger, max_size=1, output_channel=output_channel, reachability_provider=reachability_provider, error_handler=error_handler) - message_handler = UICSMessageHandler(logger=app.logger, max_size=20, network_request_handler=network_request_handler, + message_handler = UICSMessageHandler(logger=app.logger, max_size=20, + network_request_handler=network_request_handler, output_channel=output_channel, reachability_provider=reachability_provider, error_handler=error_handler) - ui_cs_bus = FileInputBus(logger=app.logger, file_channels_path=UI2CS_FILE_CHANNELS_PATH, input_channel_name="inp.buf", + ui_cs_bus = FileInputBus(logger=app.logger, file_channels_path=UI2CS_FILE_CHANNELS_PATH, + input_channel_name="inp.buf", g_config=g_config, message_handler=message_handler) 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: @@ -86,10 +117,8 @@ parser = reqparse.RequestParser() except Exception as e: - error = Error("{0},2,{1},Failed to start CS - {2}".format(OutboundMessageIDs.CS2UI_ERROR.value, - ErrorIDs.GENERIC_ERROR.value, - e)) - self.error_handler.enqueue_error(error=error) + g_utils.logger.error("Failed to start CS - {0}".format(e)) + sys.exit(0) @api.route("/version") @@ -138,9 +167,9 @@ if public_key is None: invalid_params.append("public_key") if len(invalid_params) > 0: - error = Error("{0},2,{1}, invalid credentials".format(OutboundMessageIDs.CS2UI_ERROR.value, - ErrorIDs.CS_SAVE_CREDENTIALS_ERROR.value, - invalid_params)) + error = Error("{0},2,{1}, invalid credentials: {2}".format(OutboundMessageIDs.CS2UI_ERROR.value, + ErrorIDs.CS_SAVE_CREDENTIALS_ERROR.value, + invalid_params)) error_handler.enqueue_error(error=error) return {"invalidAttributes": invalid_params}, BAD_REQUEST @@ -219,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)