예제 #1
0
def main():
    mappings = db_mappings.get_mappings()
    parse_config(mapping_choices=mappings.keys())
    config.setup_logging()
    if not CONF.db:
        CONF.print_help()
        sys.exit(0)

    try:
        elk = ElasticSearchManager(mappings=mappings)
        if CONF.db.options.lower() == 'sync':
            elk.db_sync()
        elif CONF.db.options.lower() == 'update':
            elk.update_mappings()
        elif CONF.db.options.lower() == 'remove':
            elk.remove_mappings()
        elif CONF.db.options.lower() == 'show':
            elk.show_mappings()
        elif CONF.db.options.lower() == 'update-settings':
            elk.update_settings()
        else:
            raise Exception('Option {0} not found !'.format(CONF.db.options))
    except Exception as e:
        LOG.error(e)
        print(e)
예제 #2
0
def main():
    mappings = db_mappings.get_mappings()

    args = get_args(mapping_choices=mappings.keys())

    elasticsearch_url, elasticsearch_index, elasticsearch_replicas = \
        get_db_params(args)

    number_of_replicas = int(args.replicas or
                             elasticsearch_replicas or
                             DEFAULT_REPLICAS)

    es_manager = ElastichsearchEngine(es_url=elasticsearch_url,
                                      es_index=elasticsearch_index,
                                      args=args)
    if args.verbose:
        print("  db url: {0}".format(elasticsearch_url))
        print("db index: {0}".format(elasticsearch_index))

    if args.select_mapping:
        mappings = {args.select_mapping: mappings[args.select_mapping]}

    try:
        es_manager.put_mappings(mappings)
        es_manager.set_number_of_replicas(number_of_replicas)
    except Exception as e:
        print("ERROR {0}".format(e))
        return os.EX_DATAERR

    return es_manager.exit_code
예제 #3
0
    def test_main_calls_esmanager_put_mappings_with_mappings(self, mock_get_args, mock_get_db_params,
                                                             mock_ElastichsearchEngine):
        mock_get_db_params.return_value = Mock(), Mock()
        mock_es_manager = Mock()
        mock_ElastichsearchEngine.return_value = mock_es_manager

        res = main()
        self.assertEquals(res, os.EX_OK)
        mappings = db_mappings.get_mappings()
        mock_es_manager.put_mappings.assert_called_with(mappings)
예제 #4
0
    def __init__(self, **options):
        self.mappings = db_mappings.get_mappings().copy()
        self.conf = options.copy()
        self.index = self.conf['index']

        self.elk = elasticsearch.Elasticsearch(**options)
        # check if the cluster is up or not !
        if not self.elk.ping():
            raise Exception('ElasticSearch cluster is not available. '
                            'Cannot ping it')
        # clear the index cache
        try:
            self.elk.indices.clear_cache(index=self.conf['index'])
        except Exception as e:
            LOG.warning(e)
예제 #5
0
class ElasticSearchDB(db_base.DBDriver):
    _ES_OPTS = [
        cfg.ListOpt('hosts',
                    default=['http://127.0.0.1:9200'],
                    help='specify the storage hosts'),
        cfg.StrOpt('index',
                   default='freezer',
                   help='specify the name of the elasticsearch index'),
        cfg.IntOpt('timeout',
                   default=60,
                   help='specify the connection timeout'),
        cfg.IntOpt('retries',
                   default=20,
                   help='number of retries to allow before raising and error'),
        cfg.BoolOpt('use_ssl',
                    default=False,
                    help='explicitly turn on SSL'),
        cfg.BoolOpt('verify_certs',
                    default=False,
                    help='turn on SSL certs verification'),
        cfg.StrOpt('ca_certs',
                   help='path to CA certs on disk'),
        cfg.IntOpt('number_of_replicas',
                   default=0,
                   help='Number of replicas for elk cluster. Default is 0. '
                        'Use 0 for no replicas. This should be set to (number '
                        'of node in the ES cluter -1).'),
        cfg.StrOpt('mapping',
                   dest='select_mapping',
                   default='',
                   help='Specific mapping to upload. Valid choices: {0}'
                   .format(','.join(db_mappings.get_mappings()))),
        cfg.BoolOpt('erase',
                    dest='erase',
                    default=False,
                    help='Enable index deletion in case mapping update fails '
                         'due to incompatible changes'
                    ),
        cfg.StrOpt('test-only',
                   dest='test_only',
                   default=False,
                   help='Test the validity of the mappings, but take no action'
                   )
    ]

    def __init__(self, backend):
        super(ElasticSearchDB, self).__init__(backend)
        grp = cfg.OptGroup(backend)
        CONF.register_group(grp)
        CONF.register_opts(self._ES_OPTS, group=backend)
        # CONF.register_cli_opts(self._ES_CLI_OPTS)

        self.conf = CONF.get(backend)
        self.index = self.conf.index or DEFAULT_INDEX
        self._engine = None
        self._manage_engine = None

    def get_engine(self):
        if not self._engine:
            if CONF.enable_v1_api:
                self._engine = db_session_v1.\
                    ElasticSearchEngine(self.backend)
            else:
                self._engine = db_session_v2.\
                    ElasticSearchEngineV2(self.backend)
        return self._engine

    def get_api(self):
        return self.get_engine()

    def get_manage_engine(self):
        opts = dict(self.conf.items())
        self._manage_engine = es_manager.ElasticSearchManager(**opts)
        return self._manage_engine

    def db_sync(self):
        if not self._manage_engine:
            self._manage_engine = self.get_manage_engine()
        self._manage_engine.update_mappings()

    def db_remove(self):
        if not self._manage_engine:
            self._manage_engine = self.get_manage_engine()
        self._manage_engine.remove_mappings()

    def db_show(self):
        if not self._manage_engine:
            self._manage_engine = self.get_manage_engine()
        return self._manage_engine.show_mappings()

    def name(self):
        return "ElasticSearch"