Switch to new S6 service configuration (#40)

* Add new service files

* Remove moved file

* Move files

* Add exec perm

* Update for new services

* Remove xz-utils after use

* Check labels

* Move labels to build

* Typo

* List images

* Fixit

* Add super lint

* Update labels

* Whoops

* Remove superlint

* Add teslamate dependency

* Fix url
This commit is contained in:
Colin Seymour
2024-10-27 16:43:53 +00:00
committed by GitHub
parent 3d167fc6ee
commit 86fe972431
16 changed files with 20 additions and 24 deletions
+132
View File
@@ -0,0 +1,132 @@
#!/usr/bin/env bashio
# The script assumes that basic authentication is configured
#
# DASHBOARDS_DIRECTORY represents the path to the directory
# where the JSON files corresponding to the dashboards exist.
# The default location is relative to the execution of the
# script.
#
# Source: https://github.com/cirocosta/sample-grafana/blob/master/update-dashboards.sh
set -o errexit
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
}
backup() {
local dashboard_json
for dashboard in $(list_dashboards); do
dashboard_json=$(get_dashboard "$dashboard")
if [[ -z "$dashboard_json" ]]; then
echo "ERROR: Couldn't retrieve dashboard $dashboard."
exit 1
fi
echo "$dashboard_json" > "$DASHBOARDS_DIRECTORY/$dashboard".json
echo "BACKED UP $(basename "$dashboard").json"
done
}
restore() {
bashio::log.info "Checking for Grafana folder: $FOLDER_NAME"
folders=$(curl --silent --show-error \
--user "$LOGIN" -H "Content-Type: application/json" \
"$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 \
--silent \
--show-error \
--user "$LOGIN" \
-X POST -H "Content-Type: application/json" \
-d "{\"title\":\"$FOLDER_NAME\"}" \
"$URL/api/folders")
fi
FOLDER_ID=$(echo "$FLD" | jq -r .id)
if [[ -z "$FOLDER_ID" ]]; then
bashio::log.error "Could not determine Grafana folder id"
exit 1
fi
find "$DASHBOARDS_DIRECTORY" -type f -name \*.json -print0 |
while IFS= read -r -d '' dashboard; do
curl \
--silent --show-error --output /dev/null \
--user "$LOGIN" \
-X POST -H "Content-Type: application/json" \
-d "{\"dashboard\":$(cat "$dashboard"),\"overwrite\":true,\"folderId\":$FOLDER_ID, \
\"inputs\":[{\"name\":\"DS_POSTGRES\",\"type\":\"datasource\", \
\"pluginId\":\"postgres\",\"value\":\"TeslaMate\"}]}" \
"$URL/api/dashboards/import"
bashio::log.info "... RESTORED $(basename "$dashboard")"
done
bashio::log.info "Finished Importing Grafana Dashboards"
}
get_dashboard() {
local dashboard=$1
if [[ -z "$dashboard" ]]; then
echo "ERROR:
A dashboard must be specified.
"
exit 1
fi
curl \
--silent \
--user "$LOGIN" \
"$URL/api/dashboards/db/$dashboard" |
jq '.dashboard | .id = null'
}
list_dashboards() {
curl \
--silent \
--user "$LOGIN" \
"$URL/api/search" |
jq -r '.[] | select(.type == "dash-db") | .uri' |
cut -d '/' -f2
}
main "$@"
+39
View File
@@ -0,0 +1,39 @@
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
server_name _;
listen 8099 default_server;
location / {
proxy_pass http://localhost:4000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Referer $http_referer;
# Disable encoding to allow the sub_filter to work
proxy_set_header Accept-Encoding "";
# Enable sub-filtering
sub_filter_types text/css text/xml application/javascript text/javascript;
sub_filter_once off;
# Home URLs in the HTML
sub_filter 'href="/"' 'href="$http_x_ingress_path/"';
# Fonts in the CSS
sub_filter 'url(/' 'url($http_x_ingress_path/';
# Update fixed paths in the .js files
sub_filter '"/live"' '"$http_x_ingress_path/live"';
sub_filter '"/js/"' '"$http_x_ingress_path/js/"';
}
}
+14
View File
@@ -0,0 +1,14 @@
#!/command/with-contenv bashio
# -*- bash -*-
# shellcheck shell=bash
if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then
case "${1}" in
1)
bashio::log.error "Nginx encountered an error, halting add-on"
;;
*)
bashio::log.error "Nginx crashed, halting add-on"
;;
esac
/run/s6/basedir/bin/halt
fi
+11
View File
@@ -0,0 +1,11 @@
#!/command/with-contenv bashio
# -*- bash -*-
# shellcheck shell=bash
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;'
+1
View File
@@ -0,0 +1 @@
longrun
+15
View File
@@ -0,0 +1,15 @@
#!/command/with-contenv bashio
# -*- bash -*-
# shellcheck shell=bash
if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then
case "${1}" in
1)
bashio::log.error "TeslaMate encountered an error, halting add-on"
;;
*)
bashio::log.error "TeslaMate crashed, halting add-on"
;;
esac
/run/s6/basedir/bin/halt
fi
+84
View File
@@ -0,0 +1,84 @@
#!/usr/bin/with-contenv bashio
# -*- bash -*-
# shellcheck shell=bash
set -e
# Database things
DATABASE_HOST=$(bashio::config 'database_host')
DATABASE_NAME=$(bashio::config 'database_name')
DATABASE_PASS=$(bashio::config 'database_pass')
DATABASE_PORT=$(bashio::config 'database_port')
DATABASE_USER=$(bashio::config 'database_user')
# MQTT things
DISABLE_MQTT=$(bashio::config 'disable_mqtt')
MQTT_HOST=$(bashio::config 'mqtt_host')
MQTT_NAMESPACE=$(bashio::config 'mqtt_namespace')
MQTT_PASSWORD=$(bashio::config 'mqtt_pass')
MQTT_TLS_ACCEPT_INVALID_CERTS=$(bashio::config 'mqtt_tls_accept_invalid_certs')
MQTT_TLS=$(bashio::config 'mqtt_tls')
MQTT_USERNAME=$(bashio::config 'mqtt_user')
# Other things
IMPORT_DIR=$(bashio::config 'import_dir')
PORT=4000
TZ=$(bashio::config 'timezone')
DEFAULT_GEOFENCE=$(bashio::config 'default_geofence')
encryption_key=$(bashio::config 'encryption_key')
if bashio::config.is_empty 'encryption_key'; then
encryption_key=$(openssl rand -hex 32)
bashio::addon.option 'encryption_key' "$encryption_key"
fi
ENCRYPTION_KEY=$(bashio::config 'encryption_key')
ingress_entry=$(curl -X GET \
-H "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
-H "Content-Type: application/json" \
-s http://supervisor/addons/self/info | \
jq -r '.data.ingress_entry')
export URL_PATH=$ingress_entry
export DATABASE_HOST DATABASE_NAME DATABASE_PASS DATABASE_PORT DATABASE_USER \
DISABLE_MQTT MQTT_HOST MQTT_NAMESPACE MQTT_PASSWORD MQTT_TLS_ACCEPT_INVALID_CERTS \
MQTT_TLS MQTT_USERNAME IMPORT_DIR PORT TZ DEFAULT_GEOFENCE ENCRYPTION_KEY URL_PATH
# These are optional and applied last so we can override those set above if needed.
for env_var in $(bashio::config 'env_vars|keys'); do
name=$(bashio::config "env_vars[${env_var}].name")
value=$(bashio::config "env_vars[${env_var}].value")
bashio::log.debug "Setting Env Variable ${name} to ${value}"
export "${name}=${value}"
done
# Import dashboards
if bashio::config.true 'grafana_import_dashboards'; then
/dashboards.sh restore || bashio::log.error "Failed to import dashboards"
fi
# Create the PostgreSQL database if it doesn't exist
if pg_isready -h "$DATABASE_HOST" -p "$DATABASE_PORT" > /dev/null 2>&1; then
bashio::log.info "Creating database '$DATABASE_NAME' on $DATABASE_HOST"
if [[ -n $(PGPASSWORD="$DATABASE_PASS" psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" postgres -Atqc "\list \"$DATABASE_NAME\"") ]]; then
bashio::log.info "Database $DATABASE_NAME already exists"
else
if PGPASSWORD="$DATABASE_PASS" psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" postgres -qc "CREATE DATABASE \"$DATABASE_NAME\""; then
bashio::log.info "Database $DATABASE_NAME created"
else
bashio::log.error "Failed to create database $DATABASE_NAME"
exit 1
fi
fi
else
bashio::log.error "PostgreSQL at '$DATABASE_HOST' is not ready or unreachable"
exit 1
fi
ulimit -n 1048576
bashio::log.info "Starting TeslaMate..."
cd /opt/app
exec $(/usr/bin/env sh) /entrypoint.sh bin/teslamate start
@@ -0,0 +1 @@
longrun