forked from jupyterhub/grafana-dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.py
executable file
·195 lines (158 loc) · 6.22 KB
/
deploy.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
import json
import argparse
import os
from glob import glob
from functools import partial
import subprocess
from urllib.request import urlopen, Request
from urllib.parse import urlencode
from urllib.error import HTTPError
from copy import deepcopy
import re
# UID for the folder under which our dashboards will be setup
DEFAULT_FOLDER_UID = '70E5EE84-1217-4021-A89E-1E3DE0566D93'
def grafana_request(endpoint, token, path, data=None):
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
method = 'GET' if data is None else 'POST'
req = Request(f'{endpoint}/api{path}', headers=headers, method=method)
if not isinstance(data, bytes):
data = json.dumps(data).encode()
with urlopen(req, data) as resp:
return json.load(resp)
def ensure_folder(name, uid, api):
try:
return api(f'/folders/{uid}')
except HTTPError as e:
if e.code == 404:
# We got a 404 in
folder = {
'uid': uid,
'title': name
}
return api('/folders', folder)
else:
raise
def build_dashboard(dashboard_path, api, global_dash=False):
datasources = api("/datasources")
datasources_names = [ds["name"] for ds in datasources]
# We pass the list of all datasources because the global dashboards
# use this information to show info about all datasources in the same panel
return json.loads(subprocess.check_output(
[
"jsonnet", "-J", "vendor", dashboard_path,
"--tla-code", f"datasources={datasources_names}"
]
).decode())
def layout_dashboard(dashboard):
"""
Automatically layout panels.
- Default to 12x10 panels
- Reset x axes when we encounter a row
- Assume 24 unit width
Grafana's autolayout is not available in the API, so we
have to do thos.
"""
# Make a copy, since we're going to modify this dict
dashboard = deepcopy(dashboard)
cur_x = 0
cur_y = 0
for panel in dashboard['panels']:
pos = panel['gridPos']
pos['h'] = pos.get('h', 10)
pos['w'] = pos.get('w', 12)
pos['x'] = cur_x
pos['y'] = cur_y
cur_y += pos['h']
if panel['type'] == 'row':
cur_x = 0
else:
cur_x = (cur_x + pos['w']) % 24
return dashboard
def deploy_dashboard(dashboard_path, folder_uid, api, global_dash=False):
db = build_dashboard(dashboard_path, api, global_dash)
if not db:
return
db = layout_dashboard(db)
db = populate_template_variables(api, db)
data = {
'dashboard': db,
'folderId': folder_uid,
'overwrite': True
}
api('/dashboards/db', data)
def get_label_values(api, ds_id, template_query):
"""
Return response to a `label_values` template query
`label_values` isn't actually a prometheus thing - it is an API call that
grafana makes. This function tries to mimic that. Useful for populating variables
in a dashboard
"""
# re.DOTALL allows the query to be multi-line
match = re.match(r'label_values\((?P<query>.*),\s*(?P<label>.*)\)', template_query, re.DOTALL)
query = match.group('query')
label = match.group('label')
query = {'match[]': query}
# Send a request to the backing prometheus datastore
proxy_url = f'/datasources/proxy/{ds_id}/api/v1/series?{urlencode(query)}'
metrics = api(proxy_url)['data']
return sorted(set(m[label] for m in metrics))
def populate_template_variables(api, db):
"""
Populate options for template variables.
For list of hubs and similar, users should be able to select a hub from
a dropdown list. This is not auto populated by grafana if you are
using the API (https://community.grafana.com/t/template-update-variable-api/1882/4)
so we do it here.
"""
# We gonna make modifications to db, so let's make a copy
db = deepcopy(db)
for var in db.get('templating', {}).get('list', []):
datasources = api("/datasources")
if var["type"] == "datasource":
var["options"] = [{"text": ds["name"], "value": ds["name"]} for ds in datasources]
# default selection: first datasource in list
if datasources and not var.get("current"):
var["current"] = {
"selected": True,
"tags": [],
"text": datasources[0]["name"],
"value": datasources[0]["name"],
}
var["options"][0]["selected"] = True
elif var['type'] == 'query':
template_query = var['query']
# This requires our token to have admin permissions
# Default to the first datasource
prom_id = datasources[0]["id"]
labels = get_label_values(api, prom_id, template_query)
var["options"] = [{"text": l, "value": l} for l in labels]
if labels and not var.get("current"):
# default selection: all current values
# logical alternative: pick just the first
var["current"] = {
"selected": True,
"tags": [],
"text": labels[0],
"value": labels[0],
}
var["options"][0]["selected"] = True
return db
def main():
parser = argparse.ArgumentParser()
parser.add_argument('grafana_url', help='Grafana endpoint to deploy dashboards to')
parser.add_argument('--dashboards-dir', default="dashboards", help='Directory of jsonnet dashboards to deploy')
parser.add_argument('--folder-name', default='JupyterHub Default Dashboards', help='Name of Folder to deploy to')
parser.add_argument('--folder-uid', default=DEFAULT_FOLDER_UID, help='UID of grafana folder to deploy to')
args = parser.parse_args()
grafana_token = os.environ['GRAFANA_TOKEN']
api = partial(grafana_request, args.grafana_url, grafana_token)
folder = ensure_folder(args.folder_name, args.folder_uid, api)
for dashboard in glob(f'{args.dashboards_dir}/*.jsonnet'):
deploy_dashboard(dashboard, folder['id'], api)
print(f'Deployed {dashboard}')
if __name__ == '__main__':
main()