Make startup scripts robust (#37)

* Clean up file if exists and mkdir -p

* Make importing dashboards more robust

* Update info msgs
This commit is contained in:
Colin Seymour
2024-08-30 12:45:36 +01:00
committed by GitHub
parent d97377bc0e
commit 04e01df081
3 changed files with 30 additions and 19 deletions
+21 -16
View File
@@ -11,13 +11,14 @@
set -o errexit
readonly HOST=$(bashio::config 'grafana_host')
readonly PORT=$(bashio::config 'grafana_port')
readonly USER=$(bashio::config 'grafana_user')
readonly PASS=$(bashio::config 'grafana_pass')
readonly DASHBOARDS_DIRECTORY="/dashboards"
readonly FOLDER_NAME=$(bashio::config 'grafana_folder_name')
HOST=$(bashio::config 'grafana_host')
PORT=$(bashio::config 'grafana_port')
USER=$(bashio::config 'grafana_user')
PASS=$(bashio::config 'grafana_pass')
DASHBOARDS_DIRECTORY="/dashboards"
FOLDER_NAME=$(bashio::config 'grafana_folder_name')
readonly HOST PORT USER PASS DASHBOARDS_DIRECTORY FOLDER_NAME
main() {
@@ -25,13 +26,12 @@ main() {
URL="http://$HOST:$PORT"
LOGIN="$USER:$PASS"
case $task in
backup) backup;;
restore) restore;;
*) exit 1;;
esac
}
@@ -42,9 +42,7 @@ backup() {
dashboard_json=$(get_dashboard "$dashboard")
if [[ -z "$dashboard_json" ]]; then
echo "ERROR:
Couldn't retrieve dashboard $dashboard.
"
echo "ERROR: Couldn't retrieve dashboard $dashboard."
exit 1
fi
@@ -57,10 +55,18 @@ backup() {
restore() {
bashio::log.info "Checking for Grafana folder: $FOLDER_NAME"
FLD=$(curl --silent --show-error \
folders=$(curl --silent --show-error \
--user "$LOGIN" -H "Content-Type: application/json" \
$URL/api/folders | jq ".[] | select(.title==\"$FOLDER_NAME\")")
"$URL/api/folders")
if [[ $folders == *"statusCode"* ]]; then
bashio::log.error "Error getting Grafana folders: $(echo "$folders" | jq -r .message)"
bashio::log.debug "$folders"
exit 1
fi
FLD=$(echo "$folders" | jq ".[] | select(.title==\"$FOLDER_NAME\")")
if [[ -z "$FLD" ]]; then
bashio::log.info "Not found... creating"
FLD=$(curl \
@@ -72,7 +78,7 @@ restore() {
"$URL/api/folders")
fi
FOLDER_ID=$(echo $FLD | jq -r .id)
FOLDER_ID=$(echo "$FLD" | jq -r .id)
if [[ -z "$FOLDER_ID" ]]; then
bashio::log.error "Could not determine Grafana folder id"
@@ -123,5 +129,4 @@ list_dashboards() {
cut -d '/' -f2
}
main "$@"
+7 -1
View File
@@ -1,5 +1,11 @@
#!/command/with-contenv bashio
# -*- bash -*-
# shellcheck shell=bash
mkdir /run/nginx
bashio::log.info "Starting NGINX..."
if [ -f /run/nginx ]; then
rm -f /run/nginx
fi
mkdir -p /run/nginx
exec nginx -g 'daemon off;error_log /proc/1/fd/1 error;'
+2 -2
View File
@@ -54,7 +54,7 @@ done
# Import dashboards
if bashio::config.true 'grafana_import_dashboards'; then
/dashboards.sh restore
/dashboards.sh restore || bashio::log.error "Failed to import dashboards"
fi
# Create the PostgreSQL database if it doesn't exist
@@ -78,7 +78,7 @@ fi
ulimit -n 1048576
bashio::log.info "Starting TeslaMate"
bashio::log.info "Starting TeslaMate..."
cd /opt/app
exec $(/usr/bin/env sh) /entrypoint.sh bin/teslamate start