def test_cascading_config(tmp_file, config): # build web config def login(): result = requests.get("login_url", allow_redirects=False) assert result.status_code == 200 return {"params": json.loads(result.text)} loader = WebLoader("web_loader", "config_url", login) parser = PropertiesParser("parser") web_config = Config(loader, parser) web_config.load_config(sync=True) # build file config loader = FileLoader("loader", str(tmp_file)) parser = PropertiesParser("parser") tmp_file.write(config) file_config = Config(loader, parser) file_config.load_config(sync=True) # build cascading config cascading_config = CascadingConfig(file_config, web_config) cascading_config.get_int("a") assert file_config.get_int("a") == 1 assert web_config.get_int("a") == 2 assert cascading_config.get_int("a") == 2 assert file_config.get_string("str_key") == "aa" assert web_config.get_string("str_key") == "test" assert cascading_config.get_string("str_key") == "test" assert file_config.get_value("only_in_config") == "2" assert web_config.get_value("only_in_config") is None assert cascading_config.get_value("only_in_config") == "2"
def init_config(): # import parts from complexconfig.loader.file_loader import FileLoader from complexconfig.loader.web_loader import WebLoader from complexconfig.parser.yaml_parser import YamlParser from complexconfig.parser.properties_parser import PropertiesParser from complexconfig.parser.threathunter_json_parser import ThreathunterJsonParser from complexconfig.config import Config, PeriodicalConfig, CascadingConfig from complexconfig.configcontainer import configcontainer # init the global config on /etc/nebula/nebula.conf global_config_loader = FileLoader("global_config_loader", settings.Global_Conf_FN) global_config_parser = PropertiesParser("global_config_parser") # add sniffer prefix global_config = Config(global_config_loader, global_config_parser, cb_after_load=lambda x: {"sniffer": x}) # init the sniffer module configuration on /etc/nebula/sniffer/sniffer.conf file_config_loader = FileLoader("file_config_loader", settings.Sniffer_Conf_FN) file_config_parser = YamlParser("file_config_parser") # add sniffer prefix file_config = Config(file_config_loader, file_config_parser, cb_after_load=lambda x: {"sniffer": x}) file_config.load_config(sync=True) print_with_time("successfully loaded the file config from {}".format( settings.Sniffer_Conf_FN)) web_config_loader = WebLoader( "web_config_loader", file_config.get_string("sniffer.web_config.config_url"), params={"auth": file_config.get_string("sniffer.web_config.auth")}) web_config_parser = ThreathunterJsonParser("web_config_parser") web_config = Config(web_config_loader, web_config_parser) web_config.load_config(sync=True) print "WebLoader: web_config_loader, sniffer.web_config.config_url:{}, params:{}".format( file_config.get_string("sniffer.web_config.config_url"), {"auth": file_config.get_string("sniffer.web_config.auth")}) print_with_time("successfully loaded the web config from {}".format( file_config.get_string("sniffer.web_config.config_url"))) # build the cascading config # file config will be updated every half an hour, while the web config will be updated every 5 minute cascading_config = CascadingConfig(PeriodicalConfig(global_config, 1800), PeriodicalConfig(file_config, 1800), PeriodicalConfig(web_config, 300)) configcontainer.set_config("sniffer", cascading_config) print_with_time("successfully loaded config")
# init the global config global_config_loader = FileLoader("global_config_loader", Conf_Global_Path) global_config_parser = PropertiesParser("global_config_parser") global_config = Config(global_config_loader, global_config_parser) global_config.load_config(sync=True) # init the web config web_config_loader = FileLoader("web_config_loader", Conf_Web_Path) web_config_parser = PropertiesParser("web_config_parser") web_config = Config(web_config_loader, web_config_parser) web_config.load_config(sync=True) # build the cascading config # file config will be updated every half an hour, while the web config # will be updated every 5 minute cascading_config = CascadingConfig(global_config, web_config) configcontainer.set_config("nebula", cascading_config) _config = configcontainer.get_config("nebula") Nebula_Path = 'nebula' GRAFANA_PATH = 'grafana_app' SITE_TITLE = 'Nebula' # 前端版本 Nebula_Web_Version = '3.0.1' # API版本 API_VERSION = '1.4.0' Constructing = u'前方施工中,请自觉绕行...' # swagger模板位置 Swagger_Assets_Path = opath.join(Base_Path, Nebula_Path, "middleware/swagger") # web监听设置 WebUI_Address = '0.0.0.0'