Skip to content

Commit

Permalink
Make startup scripts robust (#37)
Browse files Browse the repository at this point in the history
* Clean up file if exists and mkdir -p

* Make importing dashboards more robust

* Update info msgs
  • Loading branch information
lildude authored Aug 30, 2024
1 parent d97377b commit 04e01df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
37 changes: 21 additions & 16 deletions scripts/dashboards.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@

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() {
local task=$1

URL="http://$HOST:$PORT"
LOGIN="$USER:$PASS"

case $task in
backup) backup;;
restore) restore;;
*) exit 1;;
esac

}


Expand All @@ -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

Expand All @@ -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 \
Expand All @@ -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"
Expand Down Expand Up @@ -123,5 +129,4 @@ list_dashboards() {
cut -d '/' -f2
}


main "$@"
8 changes: 7 additions & 1 deletion services/nginx/run
Original file line number Diff line number Diff line change
@@ -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;'
4 changes: 2 additions & 2 deletions services/teslamate/run
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 04e01df

Please sign in to comment.