mirror of
https://github.com/lildude/ha-addon-teslamate.git
synced 2026-10-13 21:19:16 -04:00
Fix database check and creation (#23)
* Refactor db check and create * Make exit more accurate
This commit is contained in:
@@ -2,8 +2,13 @@
|
|||||||
# -*- bash -*-
|
# -*- bash -*-
|
||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then
|
if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then
|
||||||
bashio::log.warning "Nginx crashed, halting add-on"
|
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
|
/run/s6/basedir/bin/halt
|
||||||
fi
|
fi
|
||||||
|
|
||||||
bashio::log.info "Nginx stopped, restarting..."
|
|
||||||
@@ -2,8 +2,14 @@
|
|||||||
# -*- bash -*-
|
# -*- bash -*-
|
||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then
|
if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then
|
||||||
bashio::log.warning "TeslaMate crashed, halting add-on"
|
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
|
/run/s6/basedir/bin/halt
|
||||||
fi
|
fi
|
||||||
|
|
||||||
bashio::log.info "TeslaMate stopped, restarting..."
|
|
||||||
+10
-13
@@ -51,23 +51,20 @@ fi
|
|||||||
|
|
||||||
# Create the PostgreSQL database if it doesn't exist
|
# Create the PostgreSQL database if it doesn't exist
|
||||||
if pg_isready -h "$DATABASE_HOST" -p "$DATABASE_PORT" > /dev/null 2>&1; then
|
if pg_isready -h "$DATABASE_HOST" -p "$DATABASE_PORT" > /dev/null 2>&1; then
|
||||||
bashio::log.info "Creating database $DATABASE_NAME on $DATABASE_HOST"
|
bashio::log.info "Creating database '$DATABASE_NAME' on $DATABASE_HOST"
|
||||||
|
|
||||||
res=$(PGPASSWORD="$DATABASE_PASS" psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" postgres -c "CREATE DATABASE $DATABASE_NAME" 2>&1) || true
|
if [[ -n $(PGPASSWORD="$DATABASE_PASS" psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USER" postgres -Atqc "\list $DATABASE_NAME") ]]; then
|
||||||
case "$res" in
|
bashio::log.info "Database $DATABASE_NAME already exists"
|
||||||
"CREATE DATABASE")
|
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"
|
bashio::log.info "Database $DATABASE_NAME created"
|
||||||
;;
|
else
|
||||||
*"already exists"*)
|
bashio::log.error "Failed to create database $DATABASE_NAME"
|
||||||
bashio::log.info "Database $DATABASE_NAME already exists"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
bashio::log.error "Failed to create database $DATABASE_NAME: $res"
|
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
fi
|
||||||
esac
|
fi
|
||||||
else
|
else
|
||||||
bashio::log.error "PostgreSQL is not ready"
|
bashio::log.error "PostgreSQL at '$DATABASE_HOST' is not ready or unreachable"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user