clearml-server/apiserver/elastic/apply_mappings.py

92 lines
2.8 KiB
Python
Raw Normal View History

2019-06-10 21:24:35 +00:00
#!/usr/bin/env python3
"""
Apply elasticsearch mappings to given hosts.
"""
import argparse
import json
2024-03-18 13:37:44 +00:00
import logging
2019-06-10 21:24:35 +00:00
from pathlib import Path
from typing import Optional, Sequence, Tuple
2019-06-10 21:24:35 +00:00
from elasticsearch import Elasticsearch
2019-06-11 15:51:59 +00:00
HERE = Path(__file__).resolve().parent
2024-03-18 13:37:44 +00:00
logging.getLogger('elasticsearch').setLevel(logging.WARNING)
logging.getLogger('elastic_transport').setLevel(logging.WARNING)
def apply_mappings_to_cluster(
hosts: Sequence, key: Optional[str] = None, es_args: dict = None, http_auth: Tuple = None
):
"""Hosts maybe a sequence of strings or dicts in the form {"host": <host>, "port": <port>}"""
2024-03-18 13:37:44 +00:00
def _send_component_template(ct_file):
with ct_file.open() as json_data:
body = json.load(json_data)
template_name = f"{ct_file.stem}"
res = es.cluster.put_component_template(name=template_name, body=body)
return {"component_template": template_name, "result": res}
def _send_index_template(it_file):
with it_file.open() as json_data:
body = json.load(json_data)
template_name = f"{it_file.stem}"
res = es.indices.put_index_template(name=template_name, body=body)
return {"index_template": template_name, "result": res}
def _send_template(f):
2019-06-10 21:24:35 +00:00
with f.open() as json_data:
data = json.load(json_data)
template_name = f.stem
2023-07-26 15:46:28 +00:00
res = es.indices.put_template(name=template_name, body=data)
return {"mapping": template_name, "result": res}
2019-06-10 21:24:35 +00:00
2024-03-18 13:37:44 +00:00
es = Elasticsearch(hosts=hosts, http_auth=http_auth, **(es_args or {}))
p = HERE / "index_templates"
if key:
2024-03-18 13:37:44 +00:00
folders = [p / key]
else:
2024-03-18 13:37:44 +00:00
folders = [
f for f in p.iterdir() if f.is_dir()
]
2024-03-18 13:37:44 +00:00
ret = []
for f in folders:
for ct in (f / "component_templates").glob("*.json"):
ret.append(_send_component_template(ct))
for it in f.glob("*.json"):
ret.append(_send_index_template(it))
return ret
# p = HERE / "mappings"
# if key:
# files = (p / key).glob("*.json")
# else:
# files = p.glob("**/*.json")
#
# return [_send_template(f) for f in files]
2019-06-10 21:24:35 +00:00
def parse_args():
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("--key", help="host key, e.g. events, datasets etc.")
parser.add_argument(
"--hosts",
nargs="+",
help="list of es hosts from the same cluster, where each host is http[s]://[user:password@]host:port",
)
2019-06-10 21:24:35 +00:00
return parser.parse_args()
def main():
args = parse_args()
print(">>>>> Applying mapping to " + str(args.hosts))
res = apply_mappings_to_cluster(args.hosts, args.key)
print(res)
2019-06-10 21:24:35 +00:00
if __name__ == "__main__":
main()