def instantiate_modules_for_section( section_name: str, section_config: dict, application: ApplicationManager, cb: Callable[[ApplicationManager, Module, dict], None] = None, instance_name_prefix="", ) -> List[Module]: result = [] for name, module_def in section_config.items(): if not (isinstance(module_def, dict) and "provider" in module_def): raise ConfigValidationError( "/".join((section_name, name)), 'Should be dictionary containing mandatory key "provider"', ) module_def = add_expression_evaluator(module_def) instance = application.register_module_instance( instance_name_prefix + name, module_def.get("provider")) try: # Parse and set parameters for param_def in instance.PARAMS: if param_def.name in module_def: val = module_def.get(param_def.name) if param_def.parser is not None: val = param_def.parser.parse( val, application, "/".join((section_name, name, param_def.name)), ) val = param_def.sanitize(val) param_def.validate(val) setattr(instance, param_def.name, val) elif param_def.is_required: raise ConfigValidationError( "/".join((section_name, name, param_def.name)), "Parameter {} is required".format(param_def.name), ) # Run validation of the module device instance.validate() if cb is not None: cb(application, instance, module_def) instance.on_configured() logger.info("Configured module {}({}))".format( name, instance.__class__.__name__)) result.append(instance) except Exception as e: raise ConfigValidationError( "/".join((section_name, name)), "Invalid module configuration: " + str(e), e, ) return result
def save_instance_config(config: dict, application: ApplicationManager): if 'app' in config: app_config = add_expression_evaluator(config.get('app', {})) settings = application.get_instance_settings() settings.id = app_config.get('id', 'healthcheckbot0') # Context Path for path in app_config.get('classpath', []): if isinstance(path, str): if not os.path.exists(path): raise ConfigValidationError( 'app/classpath', 'path ' + path + ' doesn\'t exist') settings.context_path.append(path) else: raise ConfigValidationError( 'app/classpath', 'Context path must be the list of strings')
def on_configured(self): self.gelf_logger = logging.getLogger("GELF") self.gelf_logger.setLevel(logging.DEBUG) self.gelf_logger.propagate = False if self.gelf_protocol == "udp": self.gelf_logger.addHandler( GELFHandler( host=self.gelf_host, port=self.gelf_port, facility=self.facility, debugging_fields=False, extra_fields=True, ) ) elif self.gelf_protocol == "tcp": handler = GELFTcpHandler( host=self.gelf_host, facility=self.facility, port=self.gelf_port, debugging_fields=False, extra_fields=True, ) handler.level = logging.DEBUG self.gelf_logger.addHandler(handler) else: raise ConfigValidationError( "/".join(("watchers", self.name)), "Parameter gelf_protocol must be one of: udp, tcp but {} given".format(self.gelf_protocol), )
def save_instance_config(config: dict, application: ApplicationManager): if "app" in config: app_config = add_expression_evaluator(config.get("app", {})) settings = application.get_instance_settings() settings.id = app_config.get("id", "healthcheckbot0") # Context Path for path in app_config.get("classpath", []): if isinstance(path, str): if not os.path.exists(path): raise ConfigValidationError( "app/classpath", "path " + path + " doesn't exist") settings.context_path.append(path) else: raise ConfigValidationError( "app/classpath", "Context path must be the list of strings")
def _register_watcher_module(application: ApplicationManager, watcher: WatcherModule, module_def: dict): for trigger_name in module_def.get('triggers', tuple()): trigger = application.get_trigger_by_name(trigger_name) if trigger is None: raise ConfigValidationError( 'triggers', "Trigger \'{}\' doesn't exist".format(trigger_name)) trigger.register_watcher(watcher) assertions = instantiate_modules_for_section( 'watchers/{}/custom_assertions'.format(watcher.name), module_def.get('custom_assertions', dict()), application, instance_name_prefix=watcher.name + '__') watcher.custom_assertions = assertions
def on_configured(self): self.gelf_logger = logging.getLogger('GELF') self.gelf_logger.setLevel(logging.DEBUG) self.gelf_logger.propagate = False if self.gelf_protocol == 'udp': self.gelf_logger.addHandler( GELFHandler(host=self.gelf_host, port=self.gelf_port, debugging_fields=False, extra_fields=True)) elif self.gelf_protocol == 'tcp': handler = GELFTcpHandler(host=self.gelf_host, port=self.gelf_port, debugging_fields=False, extra_fields=True) handler.level = logging.DEBUG self.gelf_logger.addHandler(handler) else: raise ConfigValidationError( '/'.join(('watchers', self.name)), "Parameter gelf_protocol must be one of: udp, tcp but {} given" .format(self.gelf_protocol))