def __init__(self, logger, conf_file=None): assert PanoptesValidators.valid_readable_file( conf_file), u'conf_file must be a readable file' self._logger = logger logger.info(u'Using configuration file: ' + conf_file) try: config = ConfigObj(conf_file, configspec=_CONFIG_SPEC_FILE, interpolation=u'template', file_error=True) except (ConfigObjError, IOError): raise validator = Validator() result = config.validate(validator, preserve_errors=True) if result is not True: errors = u'' for (section_list, key, error) in flatten_errors(config, result): if key is None: errors += u'Section(s) ' + u','.join( section_list) + u' are missing\n' else: errors += u'The "' + key + u'" key in section "' + u','\ .join(section_list) + u'" failed validation\n' raise SyntaxError(u'Error parsing the configuration file: %s' % errors) kafka_config = config[u'kafka'] if kafka_config[u'publish_to_site_topic'] is False and \ kafka_config[u'publish_to_global_topic'] is False: raise PanoptesConfigurationError( u'Panoptes metrics will not be published to the message queue. Set atleast one of ' u'`publish_to_site_topic` or `publish_to_global_topic` (or both) to true in the ' u'side wide configuration file') # If the settings aren't set to publish panoptes metrics to both site and global topics at the same time # Panoptes needs to check the consumers are consuming from the correct topic if not (kafka_config[u'publish_to_site_topic'] and kafka_config[u'consume_from_site_topic']): if ((kafka_config[u'publish_to_site_topic'] and not kafka_config[u'consume_from_site_topic']) or (kafka_config[u'publish_to_global_topic'] and kafka_config[u'consume_from_site_topic'])): raise PanoptesConfigurationError( u'Panoptes metrics will not be consumed. The consumer is set to ' u'consume from the incorrect topic. Change either `publish_to_' u'site_topic` or `publish_to_global_topic` in the site wide ' u'configuration file') self._setup_logging(config) self._get_sites(config) logger.info(u'Got list of sites: %s' % self._sites) self._get_redis_urls(config) logger.info(u'Got Redis URLs "%s"' % self.redis_urls) logger.info(u'Got Redis URLs by group "%s"' % self.redis_urls_by_group) logger.info(u'Got Redis URLs by namespace "%s"' % self.redis_urls_by_namespace) self._get_zookeeper_servers(config) logger.info(u'Got list of ZooKeeper servers: %s' % self._zookeeper_servers) self._get_kafka_brokers(config) logger.info(u'Got list of Kafka brokers: %s' % self._kafka_brokers) self._get_snmp_defaults(config) logger.info(u'Got SNMP defaults: %s' % self._snmp_defaults) self._get_x509_defaults(config) logger.info(u'Got x509 defaults: %s' % self._x509_defaults) for plugin_type in const.PLUGIN_TYPES: if config[plugin_type] is None: raise Exception(u'No configuration section for %s plugins' % plugin_type) plugins_paths = config[plugin_type][u'plugins_paths'] for plugins_path in plugins_paths: if not os.path.isdir(plugins_path): raise Exception( u'%s plugins path "%s" does not exist or is not accessible' % (plugin_type, plugins_path)) if not os.access(plugins_path, os.R_OK): raise Exception(u'%s plugins path "%s" is not accessible' % (plugin_type, plugins_path)) logger.info(plugin_type + u' plugins paths: ' + str(plugins_paths)) self._config = config
def test_valid_readable_file(self): self.assertTrue( PanoptesValidators.valid_readable_file(os.path.realpath(__file__))) self.assertFalse(PanoptesValidators.valid_readable_file(None))