def load_conf(xfile, options=None, parse_conf_func=None, load_creds=False, envvar=None, custom_file=None): signal.signal(signal.SIGTERM, stop) signal.signal(signal.SIGINT, stop) conf = {'_config_directory': None} if os.path.exists(xfile): with open(xfile, 'r') as f: conf = helpers.load_yaml(f) config_directory = os.path.dirname(os.path.abspath(xfile)) conf['_config_directory'] = config_directory if custom_file: conf = helpers.merge( helpers.load_conf_yaml_file(custom_file, config_directory), conf) conf['_config_directory'] = config_directory elif envvar and os.environ.get(envvar): c = StringIO(os.environ[envvar]) conf = helpers.load_yaml(c.getvalue()) c.close() conf['_config_directory'] = None if parse_conf_func: conf = parse_conf_func(conf) else: conf = parse_conf(conf, load_creds) for x in ('modules', 'plugins'): conf = import_conf_files(x, conf) init_modules(conf) init_plugins(conf) if _INOTIFY: init_inotify(conf) if not options or not isinstance(options, object): return conf for def_option in iterkeys(get_default_options()): if getattr(options, def_option, None) is None \ and def_option in conf['general']: setattr(options, def_option, conf['general'][def_option]) setattr(options, 'configuration', conf) return options
def import_file(filepath, config_dir = None, xvars = None): if not xvars: xvars = {} if config_dir and not filepath.startswith(os.path.sep): filepath = os.path.join(config_dir, filepath) with open(filepath, 'r') as f: return load_yaml(Template(f.read(), imports = _TPL_IMPORTS).render(**xvars))
def load(self, config_path): if not config_path: LOG.warning("missing configuration directory") return if not os.path.isdir(config_path): LOG.error("invalid configuration directory: %r", config_path) return for xfile in os.listdir(config_path): xpath = os.path.join(config_path, xfile) if not xpath.endswith('.yml') or not os.path.isfile(xpath): continue f = None with open(xpath, 'r') as f: name = os.path.splitext(os.path.basename(xpath))[0] cfg = helpers.load_yaml(f) self.notif_names.add(name) self.notifications[name] = { 'cfg': cfg, 'tpl': None, 'tags': None, 'notifiers': [] } ref = self.notifications[name] ref['tags'] = self._parse_tags(cfg['general'].get('tags'), name) if cfg['general'].get('template') and os.path.isfile( cfg['general']['template']): with open(cfg['general']['template'], 'r') as t: ref['tpl'] = t.read() uri_scheme = urisup.uri_help_split( cfg['general']['uri'])[0].lower() if uri_scheme not in NOTIFIERS: raise NotImplementedError("unsupported URI scheme: %r" % uri_scheme) ref['notifiers'] = NOTIFIERS[uri_scheme] if f: f.close()
def load_conf(xfile, options = None): signal.signal(signal.SIGTERM, stop) signal.signal(signal.SIGINT, stop) config_dir = os.path.dirname(os.path.abspath(xfile)) with open(xfile, 'r') as f: conf = parse_conf(load_yaml(f)) for name, module in six.iteritems(MODULES): LOG.info("module init: %r", name) module.init(conf) for x in ('module', 'plugin', 'filter'): path = conf['general'].get('%ss_path' % x) if path and os.path.isdir(path): DwhoLibLoader.load_dir(x, path) if not conf.get('endpoints'): raise CovenantConfigurationError("Missing 'endpoints' section in configuration") for name, ept_cfg in six.iteritems(conf['endpoints']): cfg = {'general': copy.copy(conf['general']), 'covenant': {'endpoint_name': name, 'config_dir': config_dir}, 'vars' : {}} metrics = [] probes = [] if 'plugin' not in ept_cfg: raise CovenantConfigurationError("Missing 'plugin' option in endpoint: %r" % name) if ept_cfg['plugin'] not in PLUGINS: raise CovenantConfigurationError("Invalid plugin %r in endpoint: %r" % (ept_cfg['plugin'], name)) cfg['covenant']['plugin_name'] = ept_cfg['plugin'] if ept_cfg.get('import_vars'): cfg['vars'].update(import_file(ept_cfg['import_vars'], config_dir, cfg)) if 'vars' in ept_cfg: cfg['vars'].update(copy.deepcopy(ept_cfg['vars'])) if ept_cfg.get('import_metrics'): metrics.extend(import_file(ept_cfg['import_metrics'], config_dir, cfg)) if 'metrics' in ept_cfg: metrics.extend(copy.deepcopy(ept_cfg['metrics'])) if ept_cfg.get('import_probes'): probes.extend(import_file(ept_cfg['import_probes'], config_dir, cfg)) if 'probes' in ept_cfg: probes.extend(copy.deepcopy(ept_cfg['probes'])) if not metrics and not probes: raise CovenantConfigurationError("Missing 'metrics' or 'probes' option in endpoint: %r" % name) if metrics and probes: raise CovenantConfigurationError("'metrics' and 'probes' aren't allowed in a same endpoint: %r" % name) cfg['credentials'] = None if ept_cfg.get('credentials'): cfg['credentials'] = ept_cfg['credentials'] cfg['metrics'] = metrics cfg['probes'] = probes endpoint = PLUGINS[ept_cfg['plugin']](name) ENDPOINTS.register(endpoint) LOG.info("endpoint init: %r", name) endpoint.init(cfg) LOG.info("endpoint safe_init: %r", name) endpoint.safe_init() DWHO_THREADS.append(endpoint.at_stop) if not options or not isinstance(options, object): return conf for def_option in six.iterkeys(get_default_options()): if getattr(options, def_option, None) is None \ and def_option in conf['general']: setattr(options, def_option, conf['general'][def_option]) setattr(options, 'configuration', conf) return options
def load_conf(xfile, options=None, envvar=None): signal.signal(signal.SIGTERM, stop) signal.signal(signal.SIGINT, stop) conf = {'_config_directory': None} if os.path.exists(xfile): with open(xfile, 'r') as f: conf = parse_conf(load_yaml(f)) conf['_config_directory'] = os.path.dirname(os.path.abspath(xfile)) elif envvar and os.environ.get(envvar): c = StringIO(os.environ[envvar]) conf = parse_conf(load_yaml(c.getvalue())) c.close() conf['_config_directory'] = None for x in ('modules', 'plugins'): conf = import_conf_files(x, conf) init_modules(conf) init_plugins(conf) if not conf.get('dns'): conf['dns'] = {} if conf['dns'] and conf['dns'].get('domains'): for name, domain_cfg in viewitems(conf['dns']['domains']): cfg = {'rrsets': [], 'vars': {}} for x in ('vars', 'rrsets'): if isinstance(cfg[x], list): append_func = getattr(cfg[x], 'extend') else: append_func = getattr(cfg[x], 'update') if domain_cfg.get("import_%s" % x): append_func( import_file(domain_cfg["import_%s" % x], conf['_config_directory'], cfg)) if x in domain_cfg: append_func(domain_cfg[x]) if not cfg['rrsets']: cfg = None if cfg: conf['dns']['domains'][name] = cfg if not options or not isinstance(options, object): return conf for def_option in iterkeys(get_default_options()): if getattr(options, def_option, None) is None \ and def_option in conf['general']: setattr(options, def_option, conf['general'][def_option]) setattr(options, 'configuration', conf) return options
def load(src): """ Parse the first XYS schema in a stream and produce the corresponding internal representation. """ return _transschema(helpers.load_yaml(src, Loader = yaml.Loader))
def load_conf(xfile, options=None): signal.signal(signal.SIGTERM, stop) signal.signal(signal.SIGINT, stop) config_dir = os.path.dirname(os.path.abspath(xfile)) with open(xfile, 'r') as f: conf = parse_conf(load_yaml(f)) for name, module in MODULES.iteritems(): LOG.info("module init: %r", name) module.init(conf) for x in ('module', 'plugin'): path = conf['general'].get('%ss_path' % x) if path and os.path.isdir(path): DwhoLibLoader.load_dir(x, path) if not conf.get('endpoints'): raise FdReplayConfigurationError( "Missing 'endpoints' section in configuration") for name, ept_cfg in conf['endpoints'].iteritems(): cfg = { 'general': dict(conf['general']), 'fd-replay': { 'endpoint_name': name, 'config_dir': config_dir }, 'vars': {}, 'config': {} } if 'plugin' not in ept_cfg: raise FdReplayConfigurationError( "Missing 'plugin' option in endpoint: %r" % name) if ept_cfg['plugin'] not in PLUGINS: raise FdReplayConfigurationError( "Invalid plugin %r in endpoint: %r" % (ept_cfg['plugin'], name)) cfg['fd-replay']['plugin_name'] = ept_cfg['plugin'] for x in ('config', 'vars'): if ept_cfg.get("import_%s" % x): cfg[x].update( import_file(ept_cfg["import_%s" % x], config_dir, cfg)) if x in ept_cfg: cfg[x].update(dict(ept_cfg[x])) cfg['credentials'] = None if ept_cfg.get('credentials'): cfg['credentials'] = ept_cfg['credentials'] endpoint = PLUGINS[ept_cfg['plugin']](name) ENDPOINTS.register(endpoint) LOG.info("endpoint init: %r", name) endpoint.init(cfg) LOG.info("endpoint safe_init: %r", name) endpoint.safe_init() DWHO_THREADS.append(endpoint.at_stop) if not options or not isinstance(options, object): return conf for def_option in get_default_options().iterkeys(): if getattr(options, def_option, None) is None \ and def_option in conf['general']: setattr(options, def_option, conf['general'][def_option]) setattr(options, 'configuration', conf) return options