-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
90 lines (80 loc) · 3.78 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""
Create Grafana dashboards in Grafana in the management cluster for
all customer clusters.
"""
try:
from utils import grafana_request, create_customer_template
from requests_toolbelt import sessions
from requests import HTTPError
import yaml
import logging
import os
import glob
from sys import stdout
from time import sleep
except ImportError as err:
raise ImportError("Failed to import required modules: {}".format(err))
def main():
logging.basicConfig(filename="grafana_setup.log", level=logging.DEBUG)
logging.getLogger().addHandler(logging.StreamHandler(stdout))
dashboard_sources = yaml.safe_load(os.environ['DASHBOARD_SOURCES'])
session = sessions.BaseUrlSession(base_url=os.environ['GRAFANA_URL'])
session.headers.update(
{
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ['GRAFANA_API_TOKEN']}",
}
)
for customer in dashboard_sources["Customers"]:
datasource_info = []
for cluster in dashboard_sources["Customers"][customer]:
# v3 urls like https://prometheus.<domainSuffix>
# v4 urls like https://prometheus-k8s-openshift-monitoring.apps.<domainSuffix>
# domainSuffix like 1234-567890.reg00001-1.cna.ukcloud.com
clustername = ".".join(cluster["ClusterDataSourceUrl"].split(".")[-5:])
name = "{}-{}".format(customer, clustername)
datasource_info.append([name, clustername])
logging.debug(f"Creating data source for customer: {customer} cluster: {clustername}")
datasource = {
"name": name,
"type": "prometheus",
"url": cluster["ClusterDataSourceUrl"],
"access": "proxy",
"basicAuth": True,
"basicAuthUser": cluster["BasicAuthUsername"],
"basicAuthPassword": cluster["BasicAuthPassword"],
"isDefault": False
}
try:
resp = grafana_request(session, sub_endpoint="/api/datasources", method="POST", json=datasource)
logging.debug(f"JSON response for data source creation: data source: {cluster['ClusterDataSourceUrl']}\n"
f"JSON payload: {resp}")
# Sleep for a second to avoid Grafana raising 409 conflict error.
sleep(1)
except HTTPError as err:
logging.debug(f"Failed to create Grafana data source: {err}\n"
f"JSON payload: {datasource}")
dashboard = create_customer_template(datasource_info, customer)
try:
resp = grafana_request(session, sub_endpoint="/api/dashboards/db", method="POST", data=dashboard)
logging.debug(f"JSON response for dashboard creation: dashboard: {customer}\n"
f"JSON payload: {resp}")
except HTTPError as err:
logging.debug(f"Failed to create Grafana dashboard: {err}\n"
f"JSON payload: {dashboard}")
for json_file in glob.iglob(r'templates/*.json'):
with open(json_file, 'r') as dashboard_json_file:
dashboard_json = dashboard_json_file.read()
try:
resp = grafana_request(session, sub_endpoint="/api/dashboards/import", method="POST", data=dashboard_json)
logging.debug(f"JSON response for dashboard creation \n"
f"JSON payload: {resp}")
except HTTPError as err:
logging.debug(f"Failed to create Grafana dashboard: {err}\n"
f"JSON payload: {dashboard}")
session.close()
if __name__ == "__main__":
__version__ = "1.1.1"
main()