Skip to content

Commit

Permalink
Grafana installation fixed (#915)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
holgerfriedrich authored May 27, 2020
1 parent 1dcb30c commit 918e20c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 11 additions & 3 deletions functions/influxdb+grafana.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions functions/influxdb+grafana.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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
}

0 comments on commit 918e20c

Please sign in to comment.