Change shebang to use env for bashio

This commit is contained in:
Alexandre
2026-01-03 11:46:58 +01:00
committed by GitHub
parent f1ab5f46a1
commit ae34ee80cb

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bashio #!/usr/bin/env bashio
# shellcheck shell=bash # shellcheck shell=bash
set -e set -euo pipefail
# hadolint ignore=SC2155 # hadolint ignore=SC2155
################# #################
@@ -8,10 +8,11 @@ set -e
################# #################
CONFIGSOURCE="/config/addons_config/zoneminder" CONFIGSOURCE="/config/addons_config/zoneminder"
if [ ! -f "$CONFIGSOURCE"/zm.conf ]; then mkdir -p "$CONFIGSOURCE"
# Copy conf files if [ ! -f "$CONFIGSOURCE/zm.conf" ]; then
cp /etc/zm/zm.conf "$CONFIGSOURCE" # Copy conf file on first run
cp -f /etc/zm/zm.conf "$CONFIGSOURCE/zm.conf"
fi fi
################### ###################
@@ -19,95 +20,157 @@ fi
################### ###################
bashio::log.info "Defining database" bashio::log.info "Defining database"
case "$(bashio::config "DB_CONNECTION")" in
# Use MariaDB case "$(bashio::config "DB_CONNECTION")" in
mariadb_addon) mariadb_addon)
bashio::log.info "Using MariaDB addon. Requirements : running MariaDB addon. Detecting values..." bashio::log.info "Using MariaDB addon. Detecting values..."
if ! bashio::services.available 'mysql'; then if ! bashio::services.available 'mysql'; then
bashio::log.fatal \ bashio::log.fatal "Local database access should be provided by the MariaDB addon"
"Local database access should be provided by the MariaDB addon" bashio::exit.nok "Please ensure it is installed and started"
bashio::exit.nok \
"Please ensure it is installed and started"
fi fi
# Use values # Use values from MariaDB service
DB_CONNECTION=mysql DB_CONNECTION="mysql"
ZM_DB_HOST=$(bashio::services "mysql" "host") remoteDB="1"
ZM_DB_PORT=$(bashio::services "mysql" "port") ZM_DB_HOST="$(bashio::services "mysql" "host")"
ZM_DB_NAME=zm ZM_DB_PORT="$(bashio::services "mysql" "port")"
ZM_DB_USER=$(bashio::services "mysql" "username") ZM_DB_NAME="zm"
ZM_DB_PASS=$(bashio::services "mysql" "password") ZM_DB_USER="$(bashio::services "mysql" "username")"
export DB_CONNECTION ZM_DB_PASS="$(bashio::services "mysql" "password")"
export remoteDB=1 export DB_CONNECTION remoteDB ZM_DB_HOST ZM_DB_PORT ZM_DB_NAME ZM_DB_USER ZM_DB_PASS
export ZM_DB_HOST && bashio::log.blue "ZM_DB_HOST=$ZM_DB_HOST"
export ZM_DB_PORT && bashio::log.blue "ZM_DB_PORT=$ZM_DB_PORT"
export ZM_DB_NAME && bashio::log.blue "ZM_DB_NAME=$ZM_DB_NAME"
export ZM_DB_USER && bashio::log.blue "ZM_DB_USER=$ZM_DB_USER"
export ZM_DB_PASS && bashio::log.blue "ZM_DB_PASS=$ZM_DB_PASS"
bashio::log.warning "Zoneminder is using the MariaDB addon" # DO NOT log passwords
bashio::log.blue "ZM_DB_HOST=${ZM_DB_HOST}"
bashio::log.blue "ZM_DB_PORT=${ZM_DB_PORT}"
bashio::log.blue "ZM_DB_NAME=${ZM_DB_NAME}"
bashio::log.blue "ZM_DB_USER=${ZM_DB_USER}"
bashio::log.warning "ZoneMinder is using the MariaDB addon"
bashio::log.warning "Please ensure this is included in your backups" bashio::log.warning "Please ensure this is included in your backups"
bashio::log.warning "Uninstalling the MariaDB addon will remove any data" bashio::log.warning "Uninstalling the MariaDB addon will remove any data"
bashio::log.info "Creating database for Zoneminder if required" # Common mysql invocation (batch + no headers)
mysql \ mysql_base=(
-u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ mysql
-h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ -u "${ZM_DB_USER}"
-e "CREATE DATABASE IF NOT EXISTS \`${ZM_DB_NAME}\` ;" -p"${ZM_DB_PASS}"
-h "${ZM_DB_HOST}"
-P "${ZM_DB_PORT}"
--batch
--skip-column-names
)
legacy_db=$(mysql \ is_likely_zoneminder_db() {
-u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ # Returns 0 if DB looks like ZoneMinder, 1 otherwise
-h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ local db="$1"
--batch --skip-column-names \
-e "SHOW DATABASES LIKE 'firefly';" || true) # Strict requirement: these two tables are very characteristic for ZM
local required_count
required_count="$("${mysql_base[@]}" -e \
"SELECT COUNT(*) FROM information_schema.tables
WHERE table_schema='${db}'
AND LOWER(table_name) IN ('config','monitors');" \
2>/dev/null || echo 0)"
# Firefly III-ish signature tables (heuristic)
local firefly_count
firefly_count="$("${mysql_base[@]}" -e \
"SELECT COUNT(*) FROM information_schema.tables
WHERE table_schema='${db}'
AND LOWER(table_name) IN (
'accounts','transactions','transaction_journals','categories',
'budgets','bills','tags','piggy_banks','rules','rule_groups'
);" \
2>/dev/null || echo 0)"
# Must have BOTH required ZM tables and none of the Firefly signature tables
[ "${required_count:-0}" -ge 2 ] && [ "${firefly_count:-0}" -eq 0 ]
}
bashio::log.info "Creating database for ZoneMinder if required"
"${mysql_base[@]}" -e "CREATE DATABASE IF NOT EXISTS \`${ZM_DB_NAME}\`;"
# --- Legacy fix: previous buggy addon used DB name 'firefly' ---
LEGACY_DB_NAME="firefly"
legacy_db="$("${mysql_base[@]}" -e "SHOW DATABASES LIKE '${LEGACY_DB_NAME}';" || true)"
if [ -n "$legacy_db" ]; then if [ -n "$legacy_db" ]; then
target_db=$(mysql \ # First: verify legacy DB looks like ZoneMinder, not an actual Firefly DB
-u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ if ! is_likely_zoneminder_db "$LEGACY_DB_NAME"; then
-h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ bashio::log.warning "Legacy database '${LEGACY_DB_NAME}' exists but does NOT look like ZoneMinder. Skipping migration to avoid touching a real Firefly database."
--batch --skip-column-names \ else
-e "SHOW DATABASES LIKE '${ZM_DB_NAME}';" || true) # Second: migrate only if target appears empty and legacy has data
if [ -z "$target_db" ]; then target_tables="$("${mysql_base[@]}" -e \
bashio::log.warning "Detected legacy database 'firefly'. Attempting migration to '${ZM_DB_NAME}'." "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${ZM_DB_NAME}';" \
mysql \ 2>/dev/null || echo 0)"
-u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ legacy_tables="$("${mysql_base[@]}" -e \
-h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${LEGACY_DB_NAME}';" \
-e "CREATE DATABASE IF NOT EXISTS \`${ZM_DB_NAME}\` ;" 2>/dev/null || echo 0)"
if command -v mysqldump >/dev/null 2>&1; then
if mysqldump \ if [ "${target_tables:-0}" -eq 0 ] && [ "${legacy_tables:-0}" -gt 0 ]; then
-u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ bashio::log.warning "Detected legacy ZoneMinder DB named '${LEGACY_DB_NAME}'. Migrating to '${ZM_DB_NAME}'..."
-h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \
--routines --events --triggers \ dump_bin=""
firefly | mysql \ if command -v mysqldump >/dev/null 2>&1; then
dump_bin="mysqldump"
elif command -v mariadb-dump >/dev/null 2>&1; then
dump_bin="mariadb-dump"
fi
if [ -z "$dump_bin" ]; then
bashio::log.warning "mysqldump/mariadb-dump not available; please migrate manually."
else
if "$dump_bin" \
-u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \
-h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \
"${ZM_DB_NAME}"; then --routines --events --triggers \
bashio::log.info "Legacy database migration completed." "${LEGACY_DB_NAME}" | \
else mysql \
bashio::log.warning "Legacy database migration failed; please migrate manually." -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \
-h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \
"${ZM_DB_NAME}"; then
bashio::log.info "Legacy database migration completed."
bashio::log.warning "Optional: you may now drop '${LEGACY_DB_NAME}' manually if desired."
else
bashio::log.warning "Legacy database migration failed; please migrate manually."
fi
fi fi
else else
bashio::log.warning "mysqldump not available; please migrate manually." bashio::log.info "Legacy DB '${LEGACY_DB_NAME}' found but migration skipped (target not empty or legacy empty)."
fi fi
fi fi
fi fi
;; ;;
# Use remote
external) external)
bashio::log.info "Using remote database. Requirement : filling all addon options fields, and making sure the database already exists" bashio::log.info "Using remote database. Requirement: filling all addon options fields, and making sure the database already exists"
for conditions in "ZM_DB_HOST" "ZM_DB_PORT" "ZM_DB_NAME" "ZM_DB_USER" "ZM_DB_PASS"; do
if ! bashio::config.has_value "$conditions"; then for key in "ZM_DB_HOST" "ZM_DB_PORT" "ZM_DB_NAME" "ZM_DB_USER" "ZM_DB_PASS"; do
bashio::exit.nok "Remote database has been specified but $conditions is not defined in addon options" if ! bashio::config.has_value "$key"; then
bashio::exit.nok "Remote database has been specified but $key is not defined in addon options"
fi fi
done done
DB_CONNECTION="mysql"
remoteDB="1"
ZM_DB_HOST="$(bashio::config "ZM_DB_HOST")"
ZM_DB_PORT="$(bashio::config "ZM_DB_PORT")"
ZM_DB_NAME="$(bashio::config "ZM_DB_NAME")"
ZM_DB_USER="$(bashio::config "ZM_DB_USER")"
ZM_DB_PASS="$(bashio::config "ZM_DB_PASS")"
export DB_CONNECTION remoteDB ZM_DB_HOST ZM_DB_PORT ZM_DB_NAME ZM_DB_USER ZM_DB_PASS
bashio::log.blue "ZM_DB_HOST=${ZM_DB_HOST}"
bashio::log.blue "ZM_DB_PORT=${ZM_DB_PORT}"
bashio::log.blue "ZM_DB_NAME=${ZM_DB_NAME}"
bashio::log.blue "ZM_DB_USER=${ZM_DB_USER}"
# DO NOT log passwords
;; ;;
# Use remote
*) *)
bashio::log.info "Using internal database" bashio::log.info "Using internal database"
;; ;;
esac esac
############## ##############
@@ -115,5 +178,4 @@ esac
############## ##############
bashio::log.info "Please wait while the app is loading !" bashio::log.info "Please wait while the app is loading !"
/./usr/local/bin/entrypoint.sh
./usr/local/bin/entrypoint.sh