From 35151fead46d6ce23eecae1d67e2f9ad1b557803 Mon Sep 17 00:00:00 2001 From: Orfeas Theofanis Date: Mon, 5 Feb 2024 15:54:38 +0200 Subject: [PATCH 1/2] add env vars to grafana connection in post_start --- post_start/script/set_default_graf_dash.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/post_start/script/set_default_graf_dash.sh b/post_start/script/set_default_graf_dash.sh index 1eeb22f..e5985eb 100644 --- a/post_start/script/set_default_graf_dash.sh +++ b/post_start/script/set_default_graf_dash.sh @@ -1,12 +1,19 @@ #!/bin/sh +# Set your Grafana instance's connection configuration. +GRAFANA_USER=${GRAFANA_USER:-admin} +GRAFANA_PASS=${GRAFANA_PASS:-admin} +GRAFANA_HOST=${GRAFANA_HOST:-grafana} +GRAFANA_PORT=${GRAFANA_PORT:-3000} +GRAFANA_URI=${GRAFANA_USER}:${GRAFANA_PASS}@${GRAFANA_HOST}:${GRAFANA_PORT} + echo "Setting Grafana default dashboard..." DASH_UID="sJUFc-NWk" DASH_ID=0 for i in 1 2 3 4 5; do - curl -H 'Content-Type: application/json' -X GET http://admin:admin@grafana:3000/api/dashboards/uid/$DASH_UID && RESP=$(curl -H 'Content-Type: application/json' -X GET http://admin:admin@grafana:3000/api/dashboards/uid/$DASH_UID) && DASH_ID=$( echo "$RESP" | jq '.dashboard.id' ) && break || sleep 15; + curl -H 'Content-Type: application/json' -X GET "http://${GRAFANA_URI}/api/dashboards/uid/$DASH_UID" && RESP=$(curl -H 'Content-Type: application/json' -X GET "http://${GRAFANA_URI}/api/dashboards/uid/$DASH_UID") && DASH_ID=$( echo "$RESP" | jq '.dashboard.id' ) && break || sleep 15; done for i in 1 2 3 4 5; do - curl -d "{\"homeDashboardId\":$DASH_ID}" -H 'Content-Type: application/json' -X PUT http://admin:admin@grafana:3000/api/org/preferences && break || sleep 15; + curl -d "{\"homeDashboardId\":$DASH_ID}" -H 'Content-Type: application/json' -X PUT "http://${GRAFANA_URI}/api/org/preferences" && break || sleep 15; done From c0ed111a1d448e8fbaec9c4794286aa229f2fb43 Mon Sep 17 00:00:00 2001 From: Orfeas Theofanis Date: Mon, 5 Feb 2024 16:46:04 +0200 Subject: [PATCH 2/2] add quotes to properly handle spaces in user/pass --- post_start/script/set_default_graf_dash.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/post_start/script/set_default_graf_dash.sh b/post_start/script/set_default_graf_dash.sh index e5985eb..d19285f 100644 --- a/post_start/script/set_default_graf_dash.sh +++ b/post_start/script/set_default_graf_dash.sh @@ -5,15 +5,14 @@ GRAFANA_USER=${GRAFANA_USER:-admin} GRAFANA_PASS=${GRAFANA_PASS:-admin} GRAFANA_HOST=${GRAFANA_HOST:-grafana} GRAFANA_PORT=${GRAFANA_PORT:-3000} -GRAFANA_URI=${GRAFANA_USER}:${GRAFANA_PASS}@${GRAFANA_HOST}:${GRAFANA_PORT} echo "Setting Grafana default dashboard..." DASH_UID="sJUFc-NWk" DASH_ID=0 for i in 1 2 3 4 5; do - curl -H 'Content-Type: application/json' -X GET "http://${GRAFANA_URI}/api/dashboards/uid/$DASH_UID" && RESP=$(curl -H 'Content-Type: application/json' -X GET "http://${GRAFANA_URI}/api/dashboards/uid/$DASH_UID") && DASH_ID=$( echo "$RESP" | jq '.dashboard.id' ) && break || sleep 15; + curl -H 'Content-Type: application/json' -u "${GRAFANA_USER}:${GRAFANA_PASS}" -X GET http://${GRAFANA_HOST}:${GRAFANA_PORT}/api/dashboards/uid/${DASH_UID} && RESP=$(curl -H 'Content-Type: application/json' -u "${GRAFANA_USER}:${GRAFANA_PASS}" -X GET http://${GRAFANA_HOST}:${GRAFANA_PORT}/api/dashboards/uid/${DASH_UID}) && DASH_ID=$( echo "$RESP" | jq '.dashboard.id' ) && break || sleep 15; done for i in 1 2 3 4 5; do - curl -d "{\"homeDashboardId\":$DASH_ID}" -H 'Content-Type: application/json' -X PUT "http://${GRAFANA_URI}/api/org/preferences" && break || sleep 15; + curl -d "{\"homeDashboardId\":${DASH_ID}}" -H 'Content-Type: application/json' -u "${GRAFANA_USER}:${GRAFANA_PASS}" -X PUT http://${GRAFANA_HOST}:${GRAFANA_PORT}/api/org/preferences && break || sleep 15; done