#!/usr/bin/env bashio # shellcheck shell=bash # shellcheck disable=SC2155,SC2016 set -e # Function to export options from JSON to env variables export_options() { local json_source="/data/options.json" bashio::log.info "Exporting addon options from ${json_source}" # Get all keys and export their raw values mapfile -t keys < <(jq -r 'keys[]' "${json_source}") for key in "${keys[@]}"; do local value value=$(jq -r ".${key}" "${json_source}") if bashio::config.false "verbose" || [[ "$key" == *"PASS"* ]]; then bashio::log.blue "${key}=******" else bashio::log.blue "${key}='${value}'" fi export "${key}=${value}" done } # Function to check and adjust DB_HOSTNAME if necessary check_db_hostname() { if [[ "$DB_HOSTNAME" == "homeassistant.local" ]]; then local host_ip host_ip=$(bashio::network.ipv4_address) host_ip=${host_ip%/*} export DB_HOSTNAME="$host_ip" bashio::log.warning "DB_HOSTNAME was set to homeassistant.local. Using detected IP: $DB_HOSTNAME" fi if ! ping -c 1 -W 3 "$DB_HOSTNAME" >/dev/null 2>&1; then bashio::log.warning "------------------------------------" bashio::log.warning "DB_HOSTNAME ($DB_HOSTNAME) is not reachable." bashio::log.warning "Please set it to the IP address of your database." bashio::log.warning "The addon will stop until this is fixed." bashio::log.warning "------------------------------------" sleep 30 bashio::addon.stop else echo "$DB_HOSTNAME is reachable." fi } # Function to migrate internal database to external storage if needed migrate_database() { if [ -f /share/postgresql_immich.tar.gz ]; then bashio::log.warning "Previous database export found at /share/postgresql_immich.tar.gz" elif [ -d /data/postgresql ]; then bashio::log.warning "Internal Postgres database detected. Migrating to /share/postgresql_immich.tar.gz" tar -zcvf /share/postgresql_immich.tar.gz /data/postgresql rm -rf /data/postgresql fi } # Function to validate required configuration values validate_config() { local missing=false for var in DB_USERNAME DB_HOSTNAME DB_PASSWORD DB_DATABASE_NAME DB_PORT JWT_SECRET; do if ! bashio::config.has_value "${var}"; then bashio::log.error "Missing required configuration: ${var}" missing=true fi done if [ "$missing" = true ]; then bashio::exit.nok "Please ensure all required options are set." fi } # Function to export DB variables to s6 environment if applicable export_db_env() { if [ -d /var/run/s6/container_environment ]; then for var in DB_USERNAME DB_PASSWORD DB_DATABASE_NAME DB_PORT DB_HOSTNAME JWT_SECRET; do printf "%s" "${!var}" > "/var/run/s6/container_environment/${var}" done fi } # Function to set up the root user with a secure password setup_root_user() { # Generate DB_ROOT_PASSWORD if not set (12-character alphanumeric). if bashio::config.has_value "DB_ROOT_PASSWORD"; then export DB_ROOT_PASSWORD="$(bashio::config 'DB_ROOT_PASSWORD')" else bashio::log.warning "DB_ROOT_PASSWORD not set. Generating a random 12-character alphanumeric password and storing it in the addon options." export DB_ROOT_PASSWORD="$(tr -dc 'A-Za-z0-9' "/var/run/s6/container_environment/DB_ROOT_PASSWORD" fi fi # Try to connect as root using the default insecure password. if psql "postgres://root:securepassword@${DB_HOSTNAME}:${DB_PORT}/postgres" -c '\q' 2>/dev/null; then bashio::log.info "Detected root user with default password. Updating to new DB_ROOT_PASSWORD..." psql "postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOSTNAME}:${DB_PORT}" <