def create_app(config_object='settings'): """ An application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/. :param config_object: The configuration object to use. """ app = Flask(__name__.split('.')[0]) #app.config.from_object(config_object) app.config.from_object(f"halo_app.config.Config_{os.getenv('HALO_STAGE', 'loc')}") with app.app_context(): stage = '/' + app.config['ENV_NAME'] if app.config['SSM_TYPE'] and app.config['SSM_TYPE'] != 'NONE': load_api_config(app.config['ENV_TYPE'], app.config['SSM_TYPE'], app.config['FUNC_NAME'], app.config['API_CONFIG']) HALO_HOST = BaseUtil.get_host_name() params = {} params["url"] = set_host_param_config(HALO_HOST) set_app_param_config(app.config['SSM_TYPE'], params ) val = get_app_param_config(app.config['SSM_TYPE'], app.config['FUNC_NAME'], "url") print("get_app_param_config=" + str(val)) #from tests.test_viewsx import TestLinkX #app.add_url_rule(stage, view_func=TestLinkX.as_view("member")) if 'INIT_DATA_MAP' in app.config and 'INIT_CLASS_NAME' in app.config: data_map = app.config['INIT_DATA_MAP'] class_name = app.config['INIT_CLASS_NAME'] load_global_data(class_name,data_map) from halo_app.sys_util import SysUtil SysUtil.get_boundary() site_map(app) return app
def set_config(ssm_parameter_path, value): """ Load configparser from config stored in SSM Parameter Store :param ssm_parameter_path: Path to app config in SSM Parameter Store :return: ConfigParser holding loaded config """ try: # set parameters for this app json.loads(value) ret = get_client().put_parameter(Name=ssm_parameter_path, Value=value, Type='String', Overwrite=True) full_config_path, short_config_path = BaseUtil.get_env() logger.debug(str(full_config_path) + "=" + str(ret)) return True except AbsHaloException as e: msg = "Encountered a client Exception setting config from SSM" logger.error(msg) raise SSMException(msg, e) except json.decoder.JSONDecodeException as e: msg = "Encountered a json Exception setting config from SSM" logger.error(msg) raise SSMException(msg, e) except Exception as e: msg = "Encountered an Exception setting config from SSM" logger.error(msg) raise SSMException(msg, e)
def set_app_param_config(params): """ :param var_name: :param var_value: :return: """ region_name = get_region() ssm_parameter_path = short_config_path + '/' + BaseUtil.get_func() app_config = get_app_config() try: param = app_config.get_param(settings.FUNC_NAME) for var_name in params.keys(): param[var_name] = params[var_name] value = '{' for i in param: value = value + '"' + str(i) + '":"' + str(param[i]) + '",' value = value[:-1] value = value + '}' return set_config(region_name, ssm_parameter_path, value) except CacheKeyException as e: value = '{' for var_name in params.keys(): value = value + '"' + str(var_name) + '":"' + str(params[var_name]) + '",' value = value[:-1] value = value + '}' return set_config(region_name, ssm_parameter_path, value)
def set_app_param_config(params): """ :param region_name: :param host: :return: """ full_config_path, short_config_path = BaseUtil.get_env() ssm_parameter_path = short_config_path + '/' + BaseUtil.get_func() value = '{' for var_name in params.keys(): value = value + '"' + str(var_name) + '":"' + str( params[var_name]) + '",' value = value[:-1] value = value + '}' return set_config(ssm_parameter_path, value)
def set_host_param_config(host): """ :param host: :return: """ if host: url = "https://" + host + "/" + BaseUtil.get_stage() else: url = host return url
def set_param_config(region_name, key, value): """ :param region_name: :param key: :param value: :return: """ full_config_path, short_config_path = BaseUtil.get_env() ssm_parameter_path = full_config_path + '/' + key return set_config(region_name, ssm_parameter_path, value)
def get_app_config(): """ :param region_name: :return: """ # Initialize app if it doesn't yet exist full_config_path, short_config_path = BaseUtil.get_env() logger.debug("Loading app config and creating new AppConfig..." + short_config_path) cache = get_cache(short_config_path) appconfig = MyConfig(cache, short_config_path) logger.debug("AppConfig is " + str(cache.items._sections)) return appconfig
from halo_app.infra.exceptions import CacheKeyException, CacheExpireException from halo_app.classes import AbsBaseClass # from .logs import log_json from halo_app.base_util import BaseUtil from halo_app.app.utilx import Util from halo_app.infra.providers.util import ProviderUtil from halo_app.settingsx import settingsx settings = settingsx() current_milli_time = lambda: int(round(time.time() * 1000)) logger = logging.getLogger(__name__) # Initialize boto3 client at global scope for connection reuse client = None full_config_path,short_config_path = BaseUtil.get_env() def get_client(region_name): """ :param region_name: :return: """ logger.debug("get_client") global client if not client: import boto3 client = boto3.client('ssm', region_name=region_name) return client def get_region():