def test_rename_index_works(sm_config, test_db): es_config = sm_config['elasticsearch'] alias = es_config['index'] yin_index = f'{alias}-yin' yang_index = f'{alias}-yang' es_man = ESIndexManager(es_config) # Clean up previous test runs if needed es_man.delete_index(alias) es_man.delete_index(yin_index) es_man.delete_index(yang_index) try: es_man.create_index(yin_index) es_man.remap_alias(yin_index, alias=alias) assert es_man.exists_index(alias) assert es_man.exists_index(yin_index) assert not es_man.exists_index(yang_index) es_man.create_index(yang_index) es_man.remap_alias(yang_index, alias=alias) assert es_man.exists_index(alias) assert es_man.exists_index(yang_index) assert es_man.exists_index(yin_index) finally: es_man.delete_index(alias) es_man.delete_index(yin_index) es_man.delete_index(yang_index)
def test_rename_index_works(test_db, sm_config): es_config = sm_config['elasticsearch'] alias = es_config['index'] es_man = ESIndexManager(es_config) es_man.create_index('{}-yin'.format(alias)) es_man.remap_alias('{}-yin'.format(alias), alias=alias) assert es_man.exists_index(alias) assert es_man.exists_index('{}-yin'.format(alias)) assert not es_man.exists_index('{}-yang'.format(alias)) es_man.create_index('{}-yang'.format(alias)) es_man.remap_alias('{}-yang'.format(alias), alias=alias) assert es_man.exists_index(alias) assert es_man.exists_index('{}-yang'.format(alias)) assert not es_man.exists_index('{}-yin'.format(alias))
def test_rename_index_works(test_db, sm_config): es_config = sm_config['elasticsearch'] alias = es_config['index'] es_man = ESIndexManager(es_config) es_man.create_index('{}-yin'.format(alias)) es_man.remap_alias('{}-yin'.format(alias), alias=alias) assert es_man.exists_index(alias) assert es_man.exists_index('{}-yin'.format(alias)) assert not es_man.exists_index('{}-yang'.format(alias)) es_man.create_index('{}-yang'.format(alias)) es_man.remap_alias('{}-yang'.format(alias), alias=alias) assert es_man.exists_index(alias) assert es_man.exists_index('{}-yang'.format(alias)) assert not es_man.exists_index('{}-yin'.format(alias))
def _reindex_all(conf): es_config = conf['elasticsearch'] alias = es_config['index'] es_man = ESIndexManager(es_config) new_index = es_man.another_index_name(es_man.internal_index_name(alias)) es_man.create_index(new_index) try: tmp_es_config = deepcopy(es_config) tmp_es_config['index'] = new_index db = DB(conf['db']) es_exp = ESExporter(db, tmp_es_config) rows = db.select('select id, name, config from dataset') _reindex_datasets(rows, es_exp) es_man.remap_alias(tmp_es_config['index'], alias=alias) except Exception as e: es_man.delete_index(new_index) raise e
def _reindex_all(sm_config): es_config = sm_config['elasticsearch'] alias = es_config['index'] es_man = ESIndexManager(es_config) old_index = es_man.internal_index_name(alias) new_index = es_man.another_index_name(old_index) es_man.create_index(new_index) try: inactive_es_config = get_inactive_index_es_config(es_config) db = DB() es_exp = ESExporter(db, { **sm_config, 'elasticsearch': inactive_es_config }) ds_ids = [r[0] for r in db.select('select id from dataset')] _reindex_datasets(ds_ids, es_exp) es_man.remap_alias(inactive_es_config['index'], alias=alias) except Exception as e: es_man.delete_index(new_index) raise e else: es_man.delete_index(old_index)
SMConfig.set_path(args.config_path) init_loggers(SMConfig.get_conf()['logs']) es_config = SMConfig.get_conf()['elasticsearch'] es_man = ESIndexManager(es_config) alias = es_config['index'] active_index = es_man.internal_index_name(alias) inactive_index = es_man.another_index_name(active_index) index = inactive_index if args.inactive else active_index if args.action == 'create': if args.drop: es_man.delete_index(index) es_man.create_index(index) if not args.inactive: es_man.remap_alias(index, alias) elif args.action == 'swap': es_man.remap_alias(inactive_index, alias) elif args.action == 'drop': assert args.inactive, 'drop must be used with --inactive ' es_man.delete_index(index) elif args.action == 'status': pass else: parser.error('Invalid action') # Print status regardless. The specific command just exists as a clean way # to indicate to not do anything print_status(es_man, alias)
import argparse from sm.engine.util import SMConfig, init_loggers from sm.engine.es_export import ESIndexManager if __name__ == '__main__': parser = argparse.ArgumentParser( description='Create ElasticSearch indices') parser.add_argument('--config', dest='config_path', default='conf/config.json', help='SM config path') parser.add_argument('--drop', action='store_true', help='Delete index if exists') args = parser.parse_args() SMConfig.set_path(args.config_path) init_loggers(SMConfig.get_conf()['logs']) es_config = SMConfig.get_conf()['elasticsearch'] es_man = ESIndexManager(es_config) alias = es_config['index'] index = es_man.internal_index_name(alias) if args.drop: es_man.delete_index(index) es_man.create_index(index) es_man.remap_alias(index, alias)