From 38b7cedfa7aa8b58aa69d155df2ba4235b22aafb Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Wed, 16 Jul 2025 15:47:38 +0200 Subject: [PATCH] Update 99-run.sh --- ente/rootfs/etc/cont-init.d/99-run.sh | 195 +++++++++++++++++++++++--- 1 file changed, 178 insertions(+), 17 deletions(-) diff --git a/ente/rootfs/etc/cont-init.d/99-run.sh b/ente/rootfs/etc/cont-init.d/99-run.sh index 5c841bdb9..cc1ae776e 100755 --- a/ente/rootfs/etc/cont-init.d/99-run.sh +++ b/ente/rootfs/etc/cont-init.d/99-run.sh @@ -1,26 +1,187 @@ #!/usr/bin/env bashio # shellcheck shell=bash -# shellcheck disable=SC2155,SC2016 -set -e +set -euo pipefail +############################################ +# Persistent dirs +############################################ mkdir -p /config/ente/custom-logs mkdir -p /config/data mkdir -p /config/minio-data -mkdir -p /config/postgres-data +mkdir -p /config/postgres mkdir -p /config/scripts/compose -################ -# Run services # -################ +############################################ +# Config / options +############################################ +USE_EXTERNAL_DB=false +if bashio::config.true 'USE_EXTERNAL_DB'; then + USE_EXTERNAL_DB=true +fi -bashio::log.info "Starting services" -for dir in /etc/services.d/*; do - # Check if the directory contains a 'run' file - if [ -f "$dir/run" ]; then - # Execute the 'run' file - bashio::log.info "Starting service $dir" - /."$dir/run" - else - bashio::log.fatal "No run file found in $dir" - fi -done +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_PORT_INTERNAL=5432 + +DB_HOST_EXT="$(bashio::config 'DB_HOSTNAME')" +DB_PORT_EXT="$(bashio::config 'DB_PORT')" + +MINIO_USER="$(bashio::config 'MINIO_ROOT_USER')" +MINIO_PASS="$(bashio::config 'MINIO_ROOT_PASSWORD')" +S3_BUCKET="$(bashio::config 'S3_BUCKET')" + +DISABLE_WEB_UI=false +if bashio::config.true 'DISABLE_WEB_UI'; then + DISABLE_WEB_UI=true +fi + +############################################ +# Paths to binaries +############################################ +INITDB="$(command -v initdb || echo /usr/bin/initdb)" +POSTGRES_BIN="$(command -v postgres || echo /usr/bin/postgres)" +MINIO_BIN="/usr/local/bin/minio" +MC_BIN="/usr/local/bin/mc" +MUSEUM_BIN="/usr/bin/museum" +WEB_BIN="/usr/bin/ente-web" + +PGDATA="/config/postgres" + +############################################ +# Functions +############################################ + +start_postgres() { + if $USE_EXTERNAL_DB; then + bashio::log.info "External DB enabled; skipping internal Postgres start." + return 0 + fi + + # runtime socket dir + mkdir -p /run/postgresql + chown postgres:postgres /run/postgresql + chmod 775 /run/postgresql + + # data dir + mkdir -p "$PGDATA" + chown -R postgres:postgres "$PGDATA" + chmod 0700 "$PGDATA" + + if [[ ! -s "$PGDATA/PG_VERSION" ]]; then + bashio::log.info "Initializing Postgres data directory..." + su - postgres -c "$INITDB -D $PGDATA" + fi + + 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'" & + PG_PID=$! +} + +wait_postgres_ready() { + local host port user + if $USE_EXTERNAL_DB; then + host="$DB_HOST_EXT" + port="$DB_PORT_EXT" + user="$DB_USER" + bashio::log.info "Waiting for EXTERNAL Postgres at ${host}:${port}..." + else + host="$DB_HOST_INTERNAL" + port="$DB_PORT_INTERNAL" + # Use superuser 'postgres' for readiness check because DB_USER may not yet exist. + user="postgres" + bashio::log.info "Waiting for internal Postgres..." + fi + until pg_isready -q -h "$host" -p "$port" -U "$user"; do + sleep 1 + done + bashio::log.info "Postgres reachable." +} + +bootstrap_internal_db() { + if $USE_EXTERNAL_DB; then + return 0 + fi + bashio::log.info "Creating role/database if needed..." + + # Use psql via local socket (faster, avoids password) + psql -v ON_ERROR_STOP=1 -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U postgres </dev/null; do + sleep 1 + done + bashio::log.info "Ensuring bucket ${S3_BUCKET}..." + "$MC_BIN" mb -p "h0/${S3_BUCKET}" || true + bashio::log.info "MinIO bucket ready." +} + +start_web() { + if $DISABLE_WEB_UI; then + bashio::log.info "Web UI disabled." + return 0 + fi + bashio::log.info "Starting Ente web (:3000)..." + "$WEB_BIN" & + WEB_PID=$! +} + +start_museum_foreground() { + local cfg=/config/museum.yaml + if ! bashio::fs.file_exists "$cfg"; then + bashio::log.error "$cfg missing; cannot start museum." + return 1 + fi + + # For internal DB: wait one more time as DB_USER (ensures role exists) + if ! $USE_EXTERNAL_DB; then + bashio::log.info "Verifying internal DB user '${DB_USER}'..." + until pg_isready -q -h "$DB_HOST_INTERNAL" -p "$DB_PORT_INTERNAL" -U "$DB_USER"; do + sleep 1 + done + fi + + bashio::log.info "Starting museum (foreground)..." + exec "$MUSEUM_BIN" --config "$cfg" +} + +############################################ +# Main orchestration +############################################ +bashio::log.info "=== Ente startup sequence ===" + +start_postgres +wait_postgres_ready +bootstrap_internal_db + +start_minio +wait_minio_ready_and_bucket + +start_web + +# Last: foreground museum keeps container alive +start_museum_foreground