2020-03-05 12:54:34 +00:00
|
|
|
from furl import furl
|
|
|
|
|
|
|
|
from config import config
|
2020-08-10 05:30:40 +00:00
|
|
|
from elastic.apply_mappings import apply_mappings_to_host, get_template
|
2020-03-05 12:54:34 +00:00
|
|
|
from es_factory import get_cluster_config
|
|
|
|
|
|
|
|
log = config.logger(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
class MissingElasticConfiguration(Exception):
|
|
|
|
"""
|
|
|
|
Exception when cluster configuration is not found in config files
|
|
|
|
"""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-08-10 05:30:40 +00:00
|
|
|
def _url_from_host_conf(conf: dict) -> str:
|
|
|
|
return furl(scheme="http", host=conf["host"], port=conf["port"]).url
|
|
|
|
|
|
|
|
|
|
|
|
def init_es_data() -> bool:
|
|
|
|
"""Return True if the db was empty"""
|
2020-03-05 12:54:34 +00:00
|
|
|
hosts_config = get_cluster_config("events").get("hosts")
|
|
|
|
if not hosts_config:
|
|
|
|
raise MissingElasticConfiguration("for cluster 'events'")
|
|
|
|
|
2020-08-10 05:30:40 +00:00
|
|
|
empty_db = not get_template(_url_from_host_conf(hosts_config[0]), "events*")
|
|
|
|
|
2020-03-05 12:54:34 +00:00
|
|
|
for conf in hosts_config:
|
2020-08-10 05:30:40 +00:00
|
|
|
host = _url_from_host_conf(conf)
|
2020-03-05 12:54:34 +00:00
|
|
|
log.info(f"Applying mappings to host: {host}")
|
|
|
|
res = apply_mappings_to_host(host)
|
|
|
|
log.info(res)
|
2020-08-10 05:30:40 +00:00
|
|
|
|
|
|
|
return empty_db
|