clearml-server/server/elastic/apply_mappings.py

62 lines
1.5 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
from pathlib import Path
import requests
2019-06-11 15:51:59 +00:00
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
HERE = Path(__file__).resolve().parent
session = requests.Session()
adapter = HTTPAdapter(max_retries=Retry(5, backoff_factor=0.5))
session.mount("http://", adapter)
def get_template(host: str, template) -> dict:
url = f"{host}/_template/{template}"
res = session.get(url)
return res.json()
2019-06-10 21:24:35 +00:00
def apply_mappings_to_host(host: str):
def _send_mapping(f):
with f.open() as json_data:
data = json.load(json_data)
url = f"{host}/_template/{f.stem}"
2019-06-11 15:51:59 +00:00
session.delete(url)
r = session.post(
url, headers={"Content-Type": "application/json"}, data=json.dumps(data)
2019-06-10 21:24:35 +00:00
)
return {"mapping": f.stem, "result": r.text}
p = HERE / "mappings"
return [
_send_mapping(f) for f in p.iterdir() if f.is_file() and f.suffix == ".json"
]
def parse_args():
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("hosts", nargs="+")
return parser.parse_args()
def main():
args = parse_args()
for host in args.hosts:
2019-06-10 21:24:35 +00:00
print(">>>>> Applying mapping to " + host)
res = apply_mappings_to_host(host)
print(res)
if __name__ == "__main__":
main()