Skip to content

Commit b934211

Browse files
psycofdjbenjaminguttmann-avtq
authored andcommitted
grafana: fix set homepage script
1 parent ddb56b0 commit b934211

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

jobs/grafana/templates/bin/grafana-homepage

+46-12
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,50 @@ JQ_CMD=/var/vcap/packages/grafana_jq/bin/jq
77
<% if_p('grafana.dashboards.custom_homepage_json') do |homepage_json| %>
88
GRAFANA_API="<%= p('grafana.server.protocol', 'http') %>://<%= spec.ip %>:<%= p('grafana.server.http_port') %>"
99
GRAFANA_CREDENTIALS="<%= p('grafana.security.admin_user') %>:<%= p('grafana.security.admin_password') %>"
10-
upsert_dash_resp="$(curl -u ${GRAFANA_CREDENTIALS} -ksf -X POST "${GRAFANA_API}/api/dashboards/db" \
11-
-H 'Content-Type: application/json' \
12-
-d '{ "dashboard": <%= homepage_json %>, "overwrite": true }')"
13-
dash_slug="$( ${JQ_CMD} -r .slug <<< "${upsert_dash_resp}" )"
14-
get_dash_resp="$(curl -u ${GRAFANA_CREDENTIALS} -ksf -X GET "${GRAFANA_API}/api/dashboards/db/${dash_slug}" \
15-
-H 'Content-Type: application/json')"
16-
dash_id="$( ${JQ_CMD} -r .dashboard.id <<< "${get_dash_resp}" )"
17-
curl -u ${GRAFANA_CREDENTIALS} -ksf -X PUT "${GRAFANA_API}/api/org/preferences" \
18-
-H 'Content-Type: application/json' \
19-
-d "{ \"homeDashboardId\": ${dash_id} }"
20-
<% end %>
10+
DASHBOARD=$(cat - <<'EOF'
11+
<%= homepage_json %>
12+
EOF
13+
)
14+
15+
function error() {
16+
echo $@ >&2
17+
exit 1
18+
}
19+
20+
function create_or_update_dashboard() {
21+
local json="$1"; shift
22+
23+
body=$(curl \
24+
-kfsSL \
25+
-u ${GRAFANA_CREDENTIALS} \
26+
-X POST \
27+
-H 'Content-Type: application/json' \
28+
${GRAFANA_API}/api/dashboards/db \
29+
-d "{ \"dashboard\": ${json}, \"overwrite\": true }") || {
30+
error "unable to create or update home dashboard from grafana api"
31+
}
32+
33+
id=$(${JQ_CMD} -er .id <<< "${body}") || {
34+
error "unexpected api response, missing id field in ${body}"
35+
}
2136

22-
exit 0
37+
echo ${id}
38+
}
39+
40+
function set_home_dashboard() {
41+
local id=$1; shift
42+
43+
curl \
44+
-kfsSL \
45+
-u ${GRAFANA_CREDENTIALS} \
46+
-X PUT \
47+
-H 'Content-Type: application/json' \
48+
"${GRAFANA_API}/api/org/preferences" \
49+
-d "{ \"homeDashboardId\": ${id} }" || {
50+
error "could not set home dashboard preference"
51+
}
52+
}
53+
54+
set_home_dashboard $(create_or_update_dashboard "${DASHBOARD}")
55+
56+
<% end %>

0 commit comments

Comments
 (0)