From 918e20cccdc5f02a962d8ecd7e7d53233e0e3a10 Mon Sep 17 00:00:00 2001 From: Holger Friedrich Date: Wed, 27 May 2020 10:37:21 +0200 Subject: [PATCH] Grafana installation fixed (#915) * wait for Grafana service to setup grafana.db before using grafana-cli to avoid side effects, fixes #914 * adapted bats test to check connection to InfluxDB, to store settings in Grafana, to validate connection and settings file grafana.db Signed-off-by: Holger Friedrich --- functions/influxdb+grafana.bash | 14 +++++++++++--- functions/influxdb+grafana.bats | 12 ++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/functions/influxdb+grafana.bash b/functions/influxdb+grafana.bash index 342e7f922..4e6acbb35 100644 --- a/functions/influxdb+grafana.bash +++ b/functions/influxdb+grafana.bash @@ -53,7 +53,7 @@ influxdb_grafana_setup() { openhab_integration=false if [ -n "$INTERACTIVE" ]; then - text_influxDB_intro="A new InfluxDB instance can be installed locally on the openhabian system or an already running InfluxDB instance can be used. Please choose one of the options. " + text_influxDB_intro="A new InfluxDB instance can be installed locally on the openHABian system or an already running InfluxDB instance can be used. Please choose one of the options. " if ! (whiptail --title "InfluxDB" --yes-button "Install locally" --no-button "Use existing instance" --yesno "$text_influxDB_intro" 15 80) then text_influxDB_configure="Shall a new user and database be configured on the InfluxDB instance automatically or shall existing existing ones be used?" if ! (whiptail --title "InfluxDB" --yes-button "Create new" --no-button "Use existing" --yesno "$text_influxDB_configure" 15 80) then @@ -156,7 +156,7 @@ influxdb_grafana_setup() { (A new config file for openHAB will be created with basic settings.)" if (whiptail --title "openHAB integration, Continue?" --yes-button "Yes" --no-button "No" --yesno "$text_openHAB_integration" 15 80) then openhab_integration=true ; fi else - cond_echo "openHAB is not running. InfluxDB and grafana openHAB integration is skipped..." + cond_echo "openHAB is not running. InfluxDB and Grafana openHAB integration is skipped..." fi fi @@ -295,18 +295,26 @@ grafana_install(){ cond_redirect systemctl daemon-reload cond_redirect systemctl enable grafana-server.service cond_redirect systemctl start grafana-server.service - sleep 2 if [ $FAILED -eq 2 ]; then echo -n "FAILED "; return 2; else echo -n "OK "; fi cond_echo "" + echo -n "Wait for Grafana to start... " + curl --retry 5 --retry-connrefused -s http://localhost:3000 >/dev/null || FAILED=2 + if [ $FAILED -eq 2 ]; then echo -n "FAILED "; return 2; else echo -n "OK "; fi + sleep 10 + cond_echo "" + # password reset required if Grafana password was already set before (no first-time install) echo -n "Resetting Grafana admin password... " + curl --retry 5 --retry-connrefused -s http://localhost:3000 >/dev/null || FAILED=2 cond_redirect grafana-cli admin reset-admin-password admin || FAILED=2 if [ $FAILED -eq 2 ]; then echo -n "FAILED "; return 2; else echo -n "OK "; fi + cond_echo "" echo -n "Updating Grafana admin password... " curl --retry 7 --retry-connrefused --user admin:admin --header "Content-Type: application/json" --request PUT --data "{\"password\":\"$1\"}" http://localhost:3000/api/admin/users/1/password || FAILED=2 if [ $FAILED -eq 2 ]; then echo -n "FAILED "; return 2; else echo -n "OK "; fi + cond_echo "" echo -n "Updating Grafana configuration... " cond_redirect sed -i -e '/^# disable user signup \/ registration/ { n ; s/^;allow_sign_up = true/allow_sign_up = false/ }' /etc/grafana/grafana.ini || FAILED=2 diff --git a/functions/influxdb+grafana.bats b/functions/influxdb+grafana.bats index f6b68773d..1f90af148 100644 --- a/functions/influxdb+grafana.bats +++ b/functions/influxdb+grafana.bats @@ -11,6 +11,9 @@ load helpers run systemctl is-active --quiet influxdb.service [ "$status" -eq 0 ] echo -e "# \e[32mInfluxDB service running." >&3 + run curl --retry 5 --retry-connrefused -s --insecure --user "admin:Password1234" "http://localhost:8086/query" >/dev/null + [ "$status" -eq 0 ] + echo -e "# \e[32mInfluxDB service is responding." >&3 } @test "destructive-grafana_install" { @@ -21,4 +24,13 @@ load helpers run systemctl is-active --quiet grafana-server.service [ "$status" -eq 0 ] echo -e "# \e[32mGrafana service running." >&3 + run curl --retry 5 --retry-connrefused --user admin:Password1234 --header "Content-Type: application/json" --request PUT --data "{\"password\":\"Password234\"}" http://localhost:3000/api/admin/users/1/password + [ "$status" -eq 0 ] + echo -e "# \e[32mGrafana settings sucessfully changed." >&3 + run grafana_install "Password3456" + [ "$status" -eq 0 ] + echo -e "# \e[32mGrafana re-installation successful." >&3 + run curl --retry 5 --retry-connrefused --user admin:Password3456 --header "Content-Type: application/json" --request PUT --data "{\"password\":\"Password234\"}" http://localhost:3000/api/admin/users/1/password + [ "$status" -eq 0 ] + echo -e "# \e[32mGrafana settings sucessfully changed." >&3 }