Created
March 24, 2017 09:11
-
-
Save arcolife/9680b89f42c262d0b02f735a66f48e16 to your computer and use it in GitHub Desktop.
template uploader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # | |
| # usage: | |
| # $ es-create-vos-index-templates <config path> | |
| # | |
| # Create the sar index templates, with explicit mappings found in | |
| # ../lib/mappings/*.json. | |
| from __future__ import print_function | |
| import os, sys | |
| cfg_name = "/etc/pbench-index-test.cfg" | |
| import configparser | |
| config = configparser.ConfigParser() | |
| config.read(cfg_name) | |
| try: | |
| URL = config.get('Server', 'server') | |
| except configparser.NoSectionError: | |
| print("Need a [ElasticSearch] section with host and port defined in %s" | |
| " configuration file" % cfg_name, file=sys.stderr) | |
| sys.exit(1) | |
| except configparser.NoOptionError: | |
| host = config.get('ElasticSearch', 'host') | |
| port = config.get('ElasticSearch', 'port') | |
| else: | |
| host, port = URL.rsplit(':', 1) | |
| hosts = [dict(host=host, port=port),] | |
| INDEX_PREFIX = config.get('Settings', 'index_prefix') | |
| SAR_VERSION = '02' | |
| def fetch_mapping(mapping_fn): | |
| with open(mapping_fn, "r") as mappingfp: | |
| try: | |
| mapping = json.load(mappingfp) | |
| except ValueError as err: | |
| print("%s: %s" % (mapping_fn, err), file=sys.stderr) | |
| sys.exit(1) | |
| keys = list(mapping.keys()) | |
| if len(keys) != 1: | |
| print("Invalid mapping file: %s" % mapping_fn, file=sys.stderr) | |
| sys.exit(1) | |
| return keys[0], mapping | |
| import glob, json | |
| LIBDIR = '/home/arcolife/workspace/utils/RH/ARCO/Forked/pbench/server/pbench/lib/mappings/' | |
| key, mapping = fetch_mapping(os.path.join(LIBDIR, "sar.json")) | |
| sar_mappings = { key: mapping } | |
| sar_body = dict( | |
| template='%s.sar-*' % (INDEX_PREFIX,), | |
| mappings=sar_mappings) | |
| # Silence logging messages from the elasticsearch client library | |
| import logging | |
| try: | |
| from logging import NullHandler | |
| except ImportError: | |
| class NullHandler(logging.Handler): | |
| def handle(self, record): | |
| pass | |
| def emit(self, record): | |
| pass | |
| def createLock(self): | |
| self.lock = None | |
| logging.getLogger('elasticsearch').addHandler(NullHandler()) | |
| from elasticsearch import Elasticsearch | |
| es = Elasticsearch(hosts) | |
| def create_template(tname, tbody): | |
| if es.indices.exists_template(name=tname): | |
| return | |
| try: | |
| res = es.indices.put_template(name=tname, body=tbody) | |
| except Exception as err: | |
| print(repr(err), file=sys.stderr) | |
| sys.exit(1) | |
| else: | |
| try: | |
| if not res['acknowledged']: | |
| print('ERROR - Template creation was not acknowledged', file=sys.stderr) | |
| sys.exit(1) | |
| except KeyError: | |
| print('ERROR - Template creation failed: %r' % res, file=sys.stderr) | |
| sys.exit(1) | |
| print("Created template %s" % tname) | |
| tname = '%s.sar-%s' % (INDEX_PREFIX, SAR_VERSION) | |
| sar_body['template'] = '%s.sar-*' % (INDEX_PREFIX,) | |
| create_template(tname, sar_body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment