Update 99-run.sh

This commit is contained in:
Alexandre
2025-07-16 22:58:16 +02:00
committed by GitHub
parent ba41c5f1b5
commit 9152d9941d

View File

@@ -11,15 +11,12 @@ mkdir -p /config/minio-data
mkdir -p /config/postgres mkdir -p /config/postgres
mkdir -p /config/scripts/compose mkdir -p /config/scripts/compose
# Symlink
mv /museum /usr/bin/museum
############################################ ############################################
# Config / options # Config / options
############################################ ############################################
USE_EXTERNAL_DB=false USE_EXTERNAL_DB=false
if bashio::config.true 'USE_EXTERNAL_DB'; then if bashio::config.true 'USE_EXTERNAL_DB'; then
USE_EXTERNAL_DB=true USE_EXTERNAL_DB=true
fi fi
DB_NAME="$(bashio::config 'DB_DATABASE_NAME')" DB_NAME="$(bashio::config 'DB_DATABASE_NAME')"
@@ -37,18 +34,25 @@ S3_BUCKET="$(bashio::config 'S3_BUCKET')"
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 fi
############################################ ############################################
# Paths to binaries # Paths to binaries (discover; DO NOT mv)
############################################ ############################################
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)"
MINIO_BIN="/usr/local/bin/minio" MINIO_BIN="/usr/local/bin/minio"
MC_BIN="/usr/local/bin/mc" MC_BIN="/usr/local/bin/mc"
MUSEUM_BIN="/usr/bin/museum"
WEB_BIN="/usr/bin/ente-web" MUSEUM_BIN="$(command -v museum || true)"
[ -z "$MUSEUM_BIN" ] && [ -x /app/museum ] && MUSEUM_BIN=/app/museum
[ -z "$MUSEUM_BIN" ] && [ -x /museum ] && MUSEUM_BIN=/museum
[ -z "$MUSEUM_BIN" ] && MUSEUM_BIN=museum # last resort: PATH
WEB_BIN="$(command -v ente-web || true)"
[ -z "$WEB_BIN" ] && [ -x /app/ente-web ] && WEB_BIN=/app/ente-web
[ -z "$WEB_BIN" ] && [ -x /ente-web ] && WEB_BIN=/ente-web
PGDATA="/config/postgres" PGDATA="/config/postgres"
@@ -57,126 +61,135 @@ PGDATA="/config/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 enabled; skipping internal Postgres start."
return 0 return 0
fi fi
# runtime socket dir 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
# data dir mkdir -p "$PGDATA"
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 (127.0.0.1:5432)..."
# background so startup can continue 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 user 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}..."
user="$DB_USER" else
bashio::log.info "Waiting for EXTERNAL Postgres at ${host}:${port}..." host="$DB_HOST_INTERNAL"; port="$DB_PORT_INTERNAL"
else bashio::log.info "Waiting for internal Postgres..."
host="$DB_HOST_INTERNAL" fi
port="$DB_PORT_INTERNAL" until pg_isready -q -h "$host" -p "$port"; do
# Use superuser 'postgres' for readiness check because DB_USER may not yet exist. sleep 1
user="postgres" done
bashio::log.info "Waiting for internal Postgres..." bashio::log.info "Postgres reachable."
fi
until pg_isready -q -h "$host" -p "$port" -U "$user"; 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 "Creating role/database if needed..."
# Create role if it doesn't exist, else update password to match config local esc_pass="${DB_PASS//\'/\'\'}"
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
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
-c "CREATE ROLE \"${DB_USER}\" LOGIN PASSWORD '${DB_PASS//\'/\'\'}';"
else
# update password in case it changed
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
-c "ALTER ROLE \"${DB_USER}\" PASSWORD '${DB_PASS//\'/\'\'}';"
fi
# Check and create database if it doesn't exist # role
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 if ! psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres -tAc \
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \ "SELECT 1 FROM pg_roles WHERE rolname = '${DB_USER}'" | grep -q 1; then
-c "CREATE DATABASE \"${DB_NAME}\" OWNER \"${DB_USER}\";" psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
else -c "CREATE ROLE \"${DB_USER}\" LOGIN PASSWORD '${esc_pass}';"
# Optional: ensure DB ownership 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 ROLE \"${DB_USER}\" PASSWORD '${esc_pass}';"
fi fi
bashio::log.info "Internal Postgres ready." # db
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
psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres \
-c "CREATE DATABASE \"${DB_NAME}\" OWNER \"${DB_USER}\";"
else
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}\";"
fi
bashio::log.info "Internal Postgres ready."
} }
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 ${S3_BUCKET}..." bashio::log.info "Ensuring bucket ${S3_BUCKET}..."
"$MC_BIN" mb -p "h0/${S3_BUCKET}" || true "$MC_BIN" mb -p "h0/${S3_BUCKET}" || true
bashio::log.info "MinIO bucket ready." bashio::log.info "MinIO bucket ready."
} }
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
bashio::log.info "Starting Ente web (:3000)..." if [ -z "$WEB_BIN" ] || [ ! -x "$WEB_BIN" ]; then
"$WEB_BIN" & bashio::log.warning "Web UI binary not found; skipping."
WEB_PID=$! return 0
fi
bashio::log.info "Starting Ente web (:3000)..."
"$WEB_BIN" &
WEB_PID=$!
} }
start_museum_foreground() { start_museum_foreground() {
local cfg=/config/museum.yaml local cfg=/config/museum.yaml
if ! bashio::fs.file_exists "$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
bashio::log.error "Museum binary not found; cannot launch Ente API."
return 1
fi
# For internal DB: wait one more time as DB_USER (ensures role exists) # Export ENTE_* overrides to guarantee correct credentials parsing.
if ! $USE_EXTERNAL_DB; then # (Museum merges env vars over YAML.)
bashio::log.info "Verifying internal DB user '${DB_USER}'..." # Ref: environment override mechanism in museum docs. :contentReference[oaicite:2]{index=2}
until pg_isready -q -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U "$DB_USER"; do if $USE_EXTERNAL_DB; then
sleep 1 export ENTE_DB_HOST="$DB_HOST_EXT"
done export ENTE_DB_PORT="$DB_PORT_EXT"
fi else
export ENTE_DB_HOST="$DB_HOST_INTERNAL"
export ENTE_DB_PORT="$DB_PORT_INTERNAL"
fi
export ENTE_DB_USER="$DB_USER"
export ENTE_DB_PASSWORD="$DB_PASS"
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"
} }
############################################ ############################################
@@ -193,5 +206,4 @@ wait_minio_ready_and_bucket
start_web start_web
# Last: foreground museum keeps container alive
start_museum_foreground start_museum_foreground