Files
hassio-addons/fireflyiii/rootfs/etc/cont-init.d/99-run.sh
Claude f19d668d07 fix: force IPv4 host resolution for MariaDB addon connections (#2688)
On HAOS >=17.3 the Supervisor Docker network gained IPv6, so
core-mariadb resolves to an IPv6 address first. The official MariaDB
addon only grants its service user from the IPv4 supervisor subnet, so
connections from IPv6 fail with "Access denied".

Resolve the hostname to its IPv4 address before connecting in every
addon that consumes bashio::services 'mysql' 'host': photoprism,
monica, fireflyiii, seafile, zoneminder. Fall back to the raw hostname
if resolution fails so IPv4-only setups keep working unchanged.
2026-05-10 04:01:52 +00:00

226 lines
8.0 KiB
Bash
Executable File

#!/usr/bin/env bashio
# shellcheck shell=bash
set -e
# hadolint ignore=SC2155
########
# Init #
########
# APP_KEY
APP_KEY="$(bashio::config 'APP_KEY')"
# If not base64
if [[ ! "$APP_KEY" == *"base64"* ]]; then
# Check APP_KEY format
if [ ! "${#APP_KEY}" = 32 ]; then bashio::exit.nok "Your APP_KEY has ${#APP_KEY} instead of 32 characters"; fi
fi
# Backup APP_KEY file
bashio::log.info "Backuping APP_KEY to /config/addons_config/fireflyiii/APP_KEY_BACKUP.txt"
bashio::log.warning "Changing this value will require to reset your database"
# Get current app_key
mkdir -p /config/addons_config/fireflyiii
touch /config/addons_config/fireflyiii/APP_KEY_BACKUP.txt
CURRENT=$(sed -e '/^[<blank><tab>]*$/d' /config/addons_config/fireflyiii/APP_KEY_BACKUP.txt | sed -n -e '$p')
# Save if new
if [ "$CURRENT" != "$APP_KEY" ]; then
echo "$APP_KEY" >> /config/addons_config/fireflyiii/APP_KEY_BACKUP.txt
fi
# Update permissions
mkdir -p /config/addons_config/fireflyiii
chown -R www-data:www-data /config/addons_config/fireflyiii
chown -R www-data:www-data /var/www/html/storage
chmod -R 775 /config/addons_config/fireflyiii
###################
# Define database #
###################
bashio::log.info "Defining database"
case $(bashio::config 'DB_CONNECTION') in
# Use sqlite
sqlite_internal)
bashio::log.info "Using built in sqlite"
# Set variable
export DB_CONNECTION=sqlite
export DB_DATABASE=/config/addons_config/fireflyiii/database/database.sqlite
# Creating folders
mkdir -p /config/addons_config/fireflyiii/database
chown -R www-data:www-data /config/addons_config/fireflyiii/database
# Creating database
if [ ! -f /config/addons_config/fireflyiii/database/database.sqlite ]; then
# Create database
touch /config/addons_config/fireflyiii/database/database.sqlite
# Install database
echo "updating database"
php artisan migrate:refresh --seed --quiet
php artisan firefly-iii:upgrade-database --quiet
php artisan passport:install --quiet
fi
# Creating symlink
rm -r /var/www/html/storage/database
ln -s /config/addons_config/fireflyiii/database /var/www/html/storage
# Updating permissions
chmod 775 /config/addons_config/fireflyiii/database/database.sqlite
chown -R www-data:www-data /config/addons_config/fireflyiii
chown -R www-data:www-data /var/www/html/storage
;;
# Use MariaDB
mariadb_addon)
bashio::log.info "Using MariaDB addon. Requirements : running MariaDB addon. Detecting values..."
if ! bashio::services.available 'mysql'; then
bashio::log.fatal \
"Local database access should be provided by the MariaDB addon"
bashio::exit.nok \
"Please ensure it is installed and started"
fi
# Resolve MariaDB hostname to IPv4: on HAOS >=17.3 the Supervisor network
# gained IPv6, but the MariaDB addon only grants its user from the IPv4
# subnet (issue #2688). Fall back to the raw hostname if resolution fails.
mariadb_host_raw="$(bashio::services "mysql" "host")"
mariadb_host_ipv4="$(getent ahostsv4 "$mariadb_host_raw" 2> /dev/null | awk '{print $1; exit}')"
DB_HOST="${mariadb_host_ipv4:-$mariadb_host_raw}"
if [ "$DB_HOST" != "$mariadb_host_raw" ]; then
bashio::log.info "Resolved ${mariadb_host_raw} -> ${DB_HOST} (forcing IPv4)"
fi
# Use values
DB_CONNECTION=mysql
DB_PORT=$(bashio::services "mysql" "port")
# Always fetch service discovery credentials for bootstrap operations (CREATE DATABASE)
BOOTSTRAP_USERNAME=$(bashio::services "mysql" "username")
BOOTSTRAP_PASSWORD=$(bashio::services "mysql" "password")
# Use user-configured database name if provided, otherwise default to 'firefly'
if bashio::config.has_value "DB_DATABASE"; then
DB_DATABASE=$(bashio::config "DB_DATABASE")
# Validate: only allow alphanumeric, underscore, and dash
if [[ ! "$DB_DATABASE" =~ ^[a-zA-Z0-9_-]+$ ]]; then
bashio::exit.nok "DB_DATABASE contains invalid characters. Only alphanumeric, underscore, and dash are allowed."
fi
else
DB_DATABASE=firefly
fi
# Use user-configured credentials if provided, otherwise use service discovery
if bashio::config.has_value "DB_USERNAME"; then
DB_USERNAME=$(bashio::config "DB_USERNAME")
else
DB_USERNAME=${BOOTSTRAP_USERNAME}
fi
if bashio::config.has_value "DB_PASSWORD"; then
DB_PASSWORD=$(bashio::config "DB_PASSWORD")
else
DB_PASSWORD=${BOOTSTRAP_PASSWORD}
fi
export DB_CONNECTION
export DB_HOST && bashio::log.blue "DB_HOST=$DB_HOST"
export DB_PORT && bashio::log.blue "DB_PORT=$DB_PORT"
export DB_DATABASE && bashio::log.blue "DB_DATABASE=$DB_DATABASE"
export DB_USERNAME && bashio::log.blue "DB_USERNAME=$DB_USERNAME"
export DB_PASSWORD # do not log password
bashio::log.warning "Firefly-iii is using the Maria DB addon"
bashio::log.warning "Please ensure this is included in your backups"
bashio::log.warning "Uninstalling the MariaDB addon will remove any data"
bashio::log.info "Creating database for Firefly-iii if required"
# Create database using service discovery credentials which have CREATE privilege
mysql \
--skip-ssl \
-u "${BOOTSTRAP_USERNAME}" -p"${BOOTSTRAP_PASSWORD}" \
-h "${DB_HOST}" -P "${DB_PORT}" \
-e "CREATE DATABASE IF NOT EXISTS \`${DB_DATABASE}\`;"
;;
# Use remote
*)
bashio::log.info "Using remote database. Requirement : filling all addon options fields, and making sure the database already exists"
for conditions in "DB_HOST" "DB_PORT" "DB_DATABASE" "DB_USERNAME" "DB_PASSWORD"; do
if ! bashio::config.has_value "$conditions"; then
bashio::exit.nok "Remote database has been specified but $conditions is not defined in addon options"
fi
done
;;
esac
########################
# Define upload folder #
########################
bashio::log.info "Defining upload folder"
# Creating folder
if [ ! -d /config/addons_config/fireflyiii/upload ]; then
mkdir -p /config/addons_config/fireflyiii/upload
chown -R www-data:www-data /config/addons_config/fireflyiii/upload
fi
# Creating symlink
if [ -d /var/www/html/storage/ha_upload ]; then
rm -r /var/www/html/storage/ha_upload
fi
ln -s /config/addons_config/fireflyiii/upload /var/www/html/storage/ha_upload
# Updating permissions
chown -R www-data:www-data /config/addons_config/fireflyiii
chown -R www-data:www-data /var/www/html/storage
chmod -R 775 /config/addons_config/fireflyiii
# Test
f=/config/addons_config/fireflyiii
while [[ $f != / ]]; do
chmod 755 "$f"
f=$(dirname "$f")
done
################
# CRON OPTIONS #
################
if bashio::config.has_value 'Updates'; then
# Align update with options
echo ""
FREQUENCY=$(bashio::config 'Updates')
bashio::log.info "$FREQUENCY updates"
echo ""
# Sets cron // do not delete this message
cp /templates/cronupdate /etc/cron."${FREQUENCY}"/
chmod 755 /etc/cron."${FREQUENCY}"/cronupdate
# Sets cron to run with www-data user
# sed -i 's|root|www-data|g' /etc/crontab
# Starts cron
service cron start
fi
##############
# LAUNCH APP #
##############
bashio::log.info "Please wait while the app is loading !"
if bashio::config.true 'silent'; then
bashio::log.warning "Silent mode activated. Only errors will be shown. Please disable in addon options if you need to debug"
sudo -Eu www-data bash -c 'cd /var/www/html && /scripts/11-execute-things.sh >/dev/null'
else
sudo -Eu www-data bash -c 'cd /var/www/html && /scripts/11-execute-things.sh'
fi