def provider_config_from_environment()

in hostfactory/host_provider/src/util.py [0:0]


def provider_config_from_environment(pro_conf_dir=os.getenv('PRO_CONF_DIR', os.getcwd())):    
    config_file = os.path.join(pro_conf_dir, "conf", "azureccprov_config.json")
    if os.name == 'nt':
        # TODO: Why does the path matter?   Can we use one or the other for both OSs?
        config_file = os.path.join(pro_conf_dir, "azureccprov_config.json")


    hf_conf_dir = os.getenv('HF_CONFDIR', os.path.join(pro_conf_dir, "..", "..", ".."))
    hf_config_file = os.path.join(hf_conf_dir, "hostfactoryconf.json")
    
    delayed_log_statements = []
    
    # on disk configuration
    config = {}
    if os.path.exists(config_file):
        delayed_log_statements.append((logging.DEBUG, "Loading provider config: %s" % config_file))
        config = load_json(config_file)
    else:
        try:
            with open(config_file, "w") as fw:
                json.dump({}, fw)
            delayed_log_statements.append((logging.WARN, "Provider config does not exist, creating an empty one: %s" % config_file))
        except IOError:
            delayed_log_statements.append((logging.DEBUG, "Provider config does not exist and can't write a default one: %s" % config_file))
            
    if os.path.exists(hf_config_file):
        config.update(load_json(hf_config_file))

    import logging as logginglib
    log_level_name = config.get("log_level", "info")
    
    log_levels = {
        "debug": logginglib.DEBUG,
        "info": logginglib.INFO,
        "warn": logginglib.WARN,
        "error": logginglib.ERROR
    }
    
    fine = False
    if log_level_name.lower() == "fine":
        fine = True
        log_level_name = "debug"
    
    if log_level_name.lower() not in log_levels:
        delayed_log_statements.append(((logging.WARN, "Unknown logging level: %s" % log_level_name.lower())))
        log_level_name = "info"
    
    logger = init_logging(log_levels[log_level_name.lower()])
    
    for level, message in delayed_log_statements:
        logger.log(level, message)    
    return ProviderConfig(config), logger, fine