Skip to content

Commit

Permalink
[task_panels] Add params with kibana url and version to create the .k…
Browse files Browse the repository at this point in the history
…ibana
  • Loading branch information
Alvaro del Castillo committed Jan 26, 2018
1 parent 7403b8c commit 518b5a5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
10 changes: 10 additions & 0 deletions mordred/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ def general_params(cls):
"optional": True,
"default": "git",
"type": str
},
"kibiter_url": {
"optional": True,
"default": None,
"type": str
},
"kibiter_version": {
"optional": True,
"default": None,
"type": str
}
}
}
Expand Down
60 changes: 34 additions & 26 deletions mordred/task_panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@
# Header mandatory in Ellasticsearc 6 (optional in earlier versions)
ES6_HEADER = {"Content-Type": "application/json"}

# We don't have this data so it just works for this value
ES6_KIBANA_INIT_URL = "http://kibiter:5601"
ES6_KIBANA_INIT_URL += "/api/kibana/settings/indexPattern:placeholder"
ES6_KIBANA_INIT_DATA = '{"value": "*"}'
# We need the version before the .kibana exists so we can not find it
ES6_KIBANA_VERSION = "6.1.0-1"
ES6_KIBANA_INIT_HEADERS = {
"Accept": "application/json",
"Content-Type": "application/json",
"kbn-version": ES6_KIBANA_VERSION
}


def es_version(url):
"""Get Elasticsearch version.
Expand Down Expand Up @@ -113,6 +101,7 @@ def __kibiter_version(self, major):
:param major: major Elasticsearch version
"""
version = None

es_url = self.conf['es_enrichment']['url']
if major == "6":
Expand All @@ -127,14 +116,18 @@ def __kibiter_version(self, major):
}
}
r = requests.get(url, data=json.dumps(query), headers=ES6_HEADER)
r.raise_for_status()
version = r.json()['hits']['hits'][0]['_id'].split(':', 1)[1]
try:
r.raise_for_status()
version = r.json()['hits']['hits'][0]['_id'].split(':', 1)[1]
except Exception:
logger.error("Can not find kibiter version")
else:
config_url = '.kibana/config/_search'
url = urljoin(es_url + "/", config_url)
r = requests.get(url)
r.raise_for_status()
version = r.json()['hits']['hits'][0]['_id']

logger.debug("Kibiter version: %s", version)
return version

Expand Down Expand Up @@ -171,19 +164,28 @@ def __configure_kibiter(self):

if 'panels' not in self.conf:
logger.warning("Panels config not availble. Not configuring Kibiter.")
return
return False

kibiter_major = es_version(self.conf['es_enrichment']['url'])
if kibiter_major == "6":
# Force the creation of the .kibana index
res = requests.post(ES6_KIBANA_INIT_URL, headers=ES6_KIBANA_INIT_HEADERS,
data=ES6_KIBANA_INIT_DATA)
try:
res.raise_for_status()
except Exception as ex:
print(ES6_KIBANA_INIT_HEADERS)
print(ES6_KIBANA_INIT_DATA)
logger.error("Can not create the .kibana in ES6 %s", ex)
if self.conf['panels']["kibiter_url"] and self.conf['panels']["kibiter_version"]:
# Force the creation of the .kibana index
# We don't have this data so it just works for this value
k6_init_url = self.conf['panels']["kibiter_url"]
k6_init_url += "/api/kibana/settings/indexPattern:placeholder"
k6_init_data = '{"value": "*"}'
k6_init_headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"kbn-version": self.conf['panels']['kibiter_version']
}

res = requests.post(k6_init_url, headers=k6_init_headers,
data=k6_init_data)
try:
res.raise_for_status()
except Exception as ex:
logger.error("Can not create the .kibana in ES6 %s", ex)

kibiter_time_from = self.conf['panels']['kibiter_time_from']
kibiter_default_index = self.conf['panels']['kibiter_default_index']
Expand All @@ -192,6 +194,8 @@ def __configure_kibiter(self):
kibiter_major, kibiter_default_index, kibiter_time_from)

kibiter_version = self.__kibiter_version(kibiter_major)
if not kibiter_version:
return False
print("Kibiter/Kibana: version found is %s" % kibiter_version)
time_picker = "{\n \"from\": \"" + kibiter_time_from \
+ "\",\n \"to\": \"now\",\n \"mode\": \"quick\"\n}"
Expand All @@ -214,6 +218,8 @@ def __configure_kibiter(self):
headers=ES6_HEADER)
r.raise_for_status()

return True

def __create_dashboard(self, panel_file, data_sources=None):
"""Upload a panel to Elasticsearch if it does not exist yet.
Expand All @@ -238,8 +244,10 @@ def __create_dashboard(self, panel_file, data_sources=None):

def execute(self):
# Configure kibiter
self.__configure_kibiter()
print("Kibiter/Kibana: configured!")
if self.__configure_kibiter():
print("Kibiter/Kibana: configured!")
else:
logger.error("Can not configure kibiter")

print("Panels, visualizations: uploading...")
# Create the commons panels
Expand Down

0 comments on commit 518b5a5

Please sign in to comment.