Update 99-run.sh

This commit is contained in:
Alexandre
2025-07-17 16:43:06 +02:00
committed by GitHub
parent 8e3bf41a2d
commit 6ee4175ae6

View File

@@ -3,42 +3,65 @@
set -euo pipefail set -euo pipefail
############################################ ############################################
# Persistent dirs # Paths & constants
############################################ ############################################
mkdir -p /config/ente/custom-logs CFG=/config/museum.yaml
mkdir -p /config/data PGDATA=/config/postgres
mkdir -p /config/minio-data
mkdir -p /config/postgres
mkdir -p /config/scripts/compose
############################################ # Internal Postgres always bound here
# Config / options
############################################
USE_EXTERNAL_DB=false
if bashio::config.true 'USE_EXTERNAL_DB'; then
USE_EXTERNAL_DB=true
fi
DB_NAME="$(bashio::config 'DB_DATABASE_NAME')"
DB_USER="$(bashio::config 'DB_USERNAME')"
DB_PASS="$(bashio::config 'DB_PASSWORD')"
DB_HOST_INTERNAL=127.0.0.1 DB_HOST_INTERNAL=127.0.0.1
DB_PORT_INTERNAL=5432 DB_PORT_INTERNAL=5432
DB_HOST_EXT="$(bashio::config 'DB_HOSTNAME')" ############################################
DB_PORT_EXT="$(bashio::config 'DB_PORT')" # Read addon options
############################################
DB_NAME="$(bashio::config 'DB_DATABASE_NAME' || echo ente_db)"
DB_USER="$(bashio::config 'DB_USERNAME' || echo pguser)"
DB_PASS="$(bashio::config 'DB_PASSWORD' || echo ente)"
# External DB opts (may be blank)
DB_HOST_EXT="$(bashio::config 'DB_HOSTNAME' || echo '')"
DB_PORT_EXT="$(bashio::config 'DB_PORT' || echo '')"
MINIO_USER="$(bashio::config 'MINIO_ROOT_USER')" MINIO_USER="$(bashio::config 'MINIO_ROOT_USER')"
MINIO_PASS="$(bashio::config 'MINIO_ROOT_PASSWORD')" MINIO_PASS="$(bashio::config 'MINIO_ROOT_PASSWORD')"
# Which bucket name well autocreate in MinIO
S3_BUCKET="b2-eu-cen" S3_BUCKET="b2-eu-cen"
USE_EXTERNAL_DB=false
if bashio::config.true 'USE_EXTERNAL_DB'; then
USE_EXTERNAL_DB=true
bashio::log.warning "USE_EXTERNAL_DB enabled: will connect to external Postgres."
else
bashio::log.info "Using internal Postgres."
fi
DISABLE_WEB_UI=false DISABLE_WEB_UI=false
if bashio::config.true 'DISABLE_WEB_UI'; then if bashio::config.true 'DISABLE_WEB_UI'; then
DISABLE_WEB_UI=true DISABLE_WEB_UI=true
fi
# Active DB connection target (may be overridden below)
if $USE_EXTERNAL_DB; then
DB_HOST="$DB_HOST_EXT"
DB_PORT="$DB_PORT_EXT"
else
DB_HOST="$DB_HOST_INTERNAL"
DB_PORT="$DB_PORT_INTERNAL"
fi fi
############################################ ############################################
# Paths to binaries (discover; DO NOT mv) # Ensure persistent dirs
############################################
mkdir -p /config/ente/custom-logs \
/config/data \
/config/minio-data \
"$PGDATA" \
/config/scripts/compose
############################################
# Locate binaries
############################################ ############################################
INITDB="$(command -v initdb || echo /usr/bin/initdb)" INITDB="$(command -v initdb || echo /usr/bin/initdb)"
POSTGRES_BIN="$(command -v postgres || echo /usr/bin/postgres)" POSTGRES_BIN="$(command -v postgres || echo /usr/bin/postgres)"
@@ -48,160 +71,186 @@ MC_BIN="/usr/local/bin/mc"
MUSEUM_BIN="$(command -v museum || true)" MUSEUM_BIN="$(command -v museum || true)"
[ -z "$MUSEUM_BIN" ] && [ -x /app/museum ] && MUSEUM_BIN=/app/museum [ -z "$MUSEUM_BIN" ] && [ -x /app/museum ] && MUSEUM_BIN=/app/museum
[ -z "$MUSEUM_BIN" ] && [ -x /museum ] && MUSEUM_BIN=/museum [ -z "$MUSEUM_BIN" ] && [ -x /museum ] && MUSEUM_BIN=/museum
[ -z "$MUSEUM_BIN" ] && MUSEUM_BIN=museum # last resort: PATH [ -z "$MUSEUM_BIN" ] && MUSEUM_BIN=museum
WEB_BIN="$(command -v ente-web || true)" WEB_PREP_BIN=/usr/local/bin/ente-web-prepare
[ -z "$WEB_BIN" ] && [ -x /app/ente-web ] && WEB_BIN=/app/ente-web WEB_NGINX_CONF=/etc/ente-web/nginx.conf
[ -z "$WEB_BIN" ] && [ -x /ente-web ] && WEB_BIN=/ente-web
PGDATA="/config/postgres"
############################################ ############################################
# Functions # Config generation
############################################ ############################################
create_config() {
bashio::log.info "Generating new museum config at $CFG"
# small helpers
_rand_b64() { head -c "$1" /dev/urandom | base64 | tr -d '\n'; }
_rand_b64url() { head -c "$1" /dev/urandom | base64 | tr '+/' '-_' | tr -d '\n'; }
cat >"$CFG" <<EOF
key:
encryption: $(_rand_b64 32)
hash: $(_rand_b64 64)
jwt:
secret: $(_rand_b64url 32)
db:
host: ${DB_HOST_INTERNAL}
port: ${DB_PORT_INTERNAL}
name: ${DB_NAME}
user: ${DB_USER}
password: ${DB_PASS}
EOF
}
############################################
# Postgres
############################################
start_postgres() { start_postgres() {
if $USE_EXTERNAL_DB; then if $USE_EXTERNAL_DB; then
bashio::log.info "External DB enabled; skipping internal Postgres start." bashio::log.info "External DB in use; not starting internal Postgres."
return 0 return 0
fi fi
mkdir -p /run/postgresql mkdir -p /run/postgresql
chown postgres:postgres /run/postgresql chown postgres:postgres /run/postgresql
chmod 775 /run/postgresql chmod 775 /run/postgresql
mkdir -p "$PGDATA" chown -R postgres:postgres "$PGDATA"
chown -R postgres:postgres "$PGDATA" chmod 0700 "$PGDATA"
chmod 0700 "$PGDATA"
if [[ ! -s "$PGDATA/PG_VERSION" ]]; then if [[ ! -s "$PGDATA/PG_VERSION" ]]; then
bashio::log.info "Initializing Postgres data directory..." bashio::log.info "Initializing Postgres data directory..."
su - postgres -c "$INITDB -D $PGDATA" su - postgres -c "$INITDB -D $PGDATA"
fi fi
bashio::log.info "Starting Postgres (127.0.0.1:5432)..." bashio::log.info "Starting Postgres (${DB_HOST_INTERNAL}:${DB_PORT_INTERNAL})..."
su - postgres -c "$POSTGRES_BIN -D $PGDATA -c listen_addresses='127.0.0.1'" & su - postgres -c "$POSTGRES_BIN -D $PGDATA -c listen_addresses='127.0.0.1'" &
PG_PID=$! PG_PID=$!
} }
wait_postgres_ready() { wait_postgres_ready() {
local host port local host port
if $USE_EXTERNAL_DB; then if $USE_EXTERNAL_DB; then
host="$DB_HOST_EXT" host="$DB_HOST_EXT"; port="$DB_PORT_EXT"
port="$DB_PORT_EXT" bashio::log.info "Waiting for EXTERNAL Postgres at ${host}:${port}..."
bashio::log.info "Waiting for EXTERNAL Postgres at ${host}:${port}..." else
else host="$DB_HOST_INTERNAL"; port="$DB_PORT_INTERNAL"
host="$DB_HOST_INTERNAL" bashio::log.info "Waiting for internal Postgres..."
port="$DB_PORT_INTERNAL" fi
bashio::log.info "Waiting for internal Postgres..." until pg_isready -q -h "$host" -p "$port"; do sleep 1; done
fi bashio::log.info "Postgres reachable."
until pg_isready -q -h "$host" -p "$port"; do
sleep 1
done
bashio::log.info "Postgres reachable."
} }
bootstrap_internal_db() { bootstrap_internal_db() {
if $USE_EXTERNAL_DB; then if $USE_EXTERNAL_DB; then
return 0 return 0
fi fi
bashio::log.info "Creating role/database if needed..." bashio::log.info "Ensuring role & database exist..."
local esc_pass="${DB_PASS//\'/\'\'}" local esc_pass="${DB_PASS//\'/\'\'}"
# role # role
if ! psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres -tAc \ if ! psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres -tAc \
"SELECT 1 FROM pg_roles WHERE rolname = '${DB_USER}'" | grep -q 1; then "SELECT 1 FROM pg_roles WHERE rolname = '${DB_USER}'" | grep -q 1; then
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \ psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
-c "CREATE ROLE \"${DB_USER}\" LOGIN PASSWORD '${esc_pass}';" -c "CREATE ROLE \"${DB_USER}\" LOGIN PASSWORD '${esc_pass}';"
else else
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \ psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
-c "ALTER ROLE \"${DB_USER}\" PASSWORD '${esc_pass}';" -c "ALTER ROLE \"${DB_USER}\" PASSWORD '${esc_pass}';"
fi fi
# db # db
if ! psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres -tAc \ if ! psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres -tAc \
"SELECT 1 FROM pg_database WHERE datname = '${DB_NAME}'" | grep -q 1; then "SELECT 1 FROM pg_database WHERE datname = '${DB_NAME}'" | grep -q 1; then
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \ psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
-c "CREATE DATABASE \"${DB_NAME}\" OWNER \"${DB_USER}\";" -c "CREATE DATABASE \"${DB_NAME}\" OWNER \"${DB_USER}\";"
else else
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \ psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
-c "ALTER DATABASE \"${DB_NAME}\" OWNER TO \"${DB_USER}\";" -c "ALTER DATABASE \"${DB_NAME}\" OWNER TO \"${DB_USER}\";"
fi fi
bashio::log.info "Internal Postgres ready."
} }
############################################
# MinIO
############################################
start_minio() { start_minio() {
bashio::log.info "Starting MinIO (:3200)..." bashio::log.info "Starting MinIO (:3200)..."
mkdir -p /config/minio-data mkdir -p /config/minio-data
"$MINIO_BIN" server /config/minio-data --address ":3200" & "$MINIO_BIN" server /config/minio-data --address ":3200" &
MINIO_PID=$! MINIO_PID=$!
} }
wait_minio_ready_and_bucket() { wait_minio_ready_and_bucket() {
bashio::log.info "Waiting for MinIO API..." bashio::log.info "Waiting for MinIO API..."
until "$MC_BIN" alias set h0 http://127.0.0.1:3200 "$MINIO_USER" "$MINIO_PASS" 2>/dev/null; do until "$MC_BIN" alias set h0 http://127.0.0.1:3200 "$MINIO_USER" "$MINIO_PASS" 2>/dev/null; do
sleep 1 sleep 1
done done
bashio::log.info "Ensuring bucket..." bashio::log.info "Ensuring buckets..."
"$MC_BIN" mb -p h0/b2-eu-cen || true "$MC_BIN" mb -p "h0/${S3_BUCKET}" || true
"$MC_BIN" mb -p h0/wasabi-eu-central-2-v3 || true "$MC_BIN" mb -p "h0/wasabi-eu-central-2-v3" || true
"$MC_BIN" mb -p h0/scw-eu-fr-v3 || true "$MC_BIN" mb -p "h0/scw-eu-fr-v3" || true
bashio::log.info "MinIO bucket ready." bashio::log.info "MinIO buckets ready."
} }
############################################
# Web (static nginx bundle)
############################################
start_web() { start_web() {
if $DISABLE_WEB_UI; then if $DISABLE_WEB_UI; then
bashio::log.info "Web UI disabled." bashio::log.info "Web UI disabled."
return 0 return 0
fi fi
# Prepare static assets with actual origins (does safe sed replacements). ENTE_API_ORIGIN="${ENTE_API_ORIGIN:-http://[HOST]:[PORT:8080]}"
ENTE_API_ORIGIN="${ENTE_API_ORIGIN:-http://[HOST]:[PORT:8080]}" ENTE_ALBUMS_ORIGIN="${ENTE_ALBUMS_ORIGIN:-${ENTE_API_ORIGIN}}"
ENTE_ALBUMS_ORIGIN="${ENTE_ALBUMS_ORIGIN:-${ENTE_API_ORIGIN}}" export ENTE_API_ORIGIN ENTE_ALBUMS_ORIGIN
export ENTE_API_ORIGIN ENTE_ALBUMS_ORIGIN
/usr/local/bin/ente-web-prepare || bashio::log.warning "Web env substitution step returned nonzero; continuing."
# nginx expects runtime dirs if [ -x "$WEB_PREP_BIN" ]; then
mkdir -p /run/nginx "$WEB_PREP_BIN" || bashio::log.warning "Web env substitution step failed (nonfatal)."
# log dir else
mkdir -p /var/log/nginx bashio::log.warning "Web prep helper not found ($WEB_PREP_BIN); skipping substitution."
fi
bashio::log.info "Starting Ente web (nginx, ports 30003004)..." mkdir -p /run/nginx /var/log/nginx
nginx -c /etc/ente-web/nginx.conf -g 'daemon off;' & if [ ! -f "$WEB_NGINX_CONF" ]; then
WEB_PID=$! bashio::log.error "Missing nginx conf at $WEB_NGINX_CONF; cannot start web."
return 1
fi
bashio::log.info "Starting Ente web (nginx, ports 30003004)..."
nginx -c "$WEB_NGINX_CONF" -g 'daemon off;' &
WEB_PID=$!
} }
############################################
# Museum (API)
############################################
start_museum_foreground() { start_museum_foreground() {
local cfg=/config/museum.yaml if [ ! -f "$CFG" ]; then
if ! bashio::fs.file_exists "$cfg"; then bashio::log.error "$CFG missing; cannot start museum."
bashio::log.error "$cfg missing; cannot start museum." return 1
return 1 fi
fi if [ ! -x "$MUSEUM_BIN" ] && ! command -v "$MUSEUM_BIN" >/dev/null 2>&1; then
if [ ! -x "$MUSEUM_BIN" ] && ! command -v "$MUSEUM_BIN" >/dev/null 2>&1; then bashio::log.error "Museum binary not found; cannot launch Ente API."
bashio::log.error "Museum binary not found; cannot launch Ente API." return 1
return 1 fi
fi
# Export ENTE_* overrides to guarantee correct credentials parsing. # Force env overrides (museum merges env > yaml)
# (Museum merges env vars over YAML.) if $USE_EXTERNAL_DB; then
# Ref: environment override mechanism in museum docs. :contentReference[oaicite:2]{index=2} export ENTE_DB_HOST="$DB_HOST_EXT"
if $USE_EXTERNAL_DB; then export ENTE_DB_PORT="$DB_PORT_EXT"
export ENTE_DB_HOST="$DB_HOST_EXT" else
export ENTE_DB_PORT="$DB_PORT_EXT" export ENTE_DB_HOST="$DB_HOST_INTERNAL"
else export ENTE_DB_PORT="$DB_PORT_INTERNAL"
export ENTE_DB_HOST="$DB_HOST_INTERNAL" fi
export ENTE_DB_PORT="$DB_PORT_INTERNAL" export ENTE_DB_USER="$DB_USER"
fi export ENTE_DB_PASSWORD="$DB_PASS"
export ENTE_DB_USER="$DB_USER" export ENTE_DB_NAME="$DB_NAME"
export ENTE_DB_PASSWORD="$DB_PASS" export ENTE_DB_SSLMODE=disable
export ENTE_DB_NAME="$DB_NAME"
export ENTE_DB_SSLMODE=disable
bashio::log.info "Starting museum (foreground)..." bashio::log.info "Starting museum (foreground)..."
exec "$MUSEUM_BIN" --config "$cfg" exec "$MUSEUM_BIN" --config "$CFG"
} }
############################################ ############################################
@@ -209,6 +258,12 @@ start_museum_foreground() {
############################################ ############################################
bashio::log.info "=== Ente startup sequence ===" bashio::log.info "=== Ente startup sequence ==="
if [ ! -f "$CFG" ]; then
create_config
else
bashio::log.info "Using existing $CFG."
fi
start_postgres start_postgres
wait_postgres_ready wait_postgres_ready
bootstrap_internal_db bootstrap_internal_db
@@ -218,4 +273,5 @@ wait_minio_ready_and_bucket
start_web start_web
# Foreground (keeps container alive)
start_museum_foreground start_museum_foreground