diff --git a/glpi/files/opt/glpi/cron-worker.sh b/glpi/files/opt/glpi/cron-worker.sh index d4a87be..439215f 100755 --- a/glpi/files/opt/glpi/cron-worker.sh +++ b/glpi/files/opt/glpi/cron-worker.sh @@ -1,6 +1,9 @@ #!/bin/bash set -e -u -o pipefail +# Wait for database to be ready before starting cron tasks +/opt/glpi/entrypoint/wait-for-db.sh cron-worker + # Infinite loop to run GLPI cron tasks every minute while true; do php /var/www/glpi/front/cron.php || echo "Cron task failed" diff --git a/glpi/files/opt/glpi/entrypoint.sh b/glpi/files/opt/glpi/entrypoint.sh index e8d4afa..b7170fc 100644 --- a/glpi/files/opt/glpi/entrypoint.sh +++ b/glpi/files/opt/glpi/entrypoint.sh @@ -3,6 +3,7 @@ set -e -u -o pipefail /opt/glpi/entrypoint/init-volumes-directories.sh /opt/glpi/entrypoint/forward-logs.sh +/opt/glpi/entrypoint/wait-for-db.sh entrypoint /opt/glpi/entrypoint/install.sh exec "$@" diff --git a/glpi/files/opt/glpi/entrypoint/install.sh b/glpi/files/opt/glpi/entrypoint/install.sh index 45c60cd..4bdf4fa 100644 --- a/glpi/files/opt/glpi/entrypoint/install.sh +++ b/glpi/files/opt/glpi/entrypoint/install.sh @@ -60,6 +60,15 @@ GLPI_Installed() { return 1 } +# Skipping auto stuff, database configuration is not fully provided +if [ -z "${GLPI_DB_HOST:-}" ] || [ -z "${GLPI_DB_PORT:-}" ] || [ -z "${GLPI_DB_NAME:-}" ] || [ -z "${GLPI_DB_USER:-}" ] || [ -z "${GLPI_DB_PASSWORD:-}" ]; then + if ! $GLPI_SKIP_AUTOINSTALL || ! $GLPI_SKIP_AUTOUPDATE; then + echo "Database configuration incomplete, forcing GLPI_SKIP_AUTOINSTALL and GLPI_SKIP_AUTOUPDATE to true." + fi + GLPI_SKIP_AUTOINSTALL=true + GLPI_SKIP_AUTOUPDATE=true +fi + if ! GLPI_Installed; then if ! $GLPI_SKIP_AUTOINSTALL; then echo "GLPI is not installed. but auto-install is enabled. Starting installation." diff --git a/glpi/files/opt/glpi/entrypoint/wait-for-db.sh b/glpi/files/opt/glpi/entrypoint/wait-for-db.sh new file mode 100644 index 0000000..6abd375 --- /dev/null +++ b/glpi/files/opt/glpi/entrypoint/wait-for-db.sh @@ -0,0 +1,35 @@ +#!/bin/bash +set -e -u -o pipefail + +# Wait for database to be ready before proceeding +# Uses PHP mysqli to check database connectivity +# Usage: wait-for-db.sh [caller_name] + +caller="${1:-unknown}" + +# Skip if database configuration is not fully provided +if [ -z "${GLPI_DB_HOST:-}" ] || [ -z "${GLPI_DB_PORT:-}" ] || [ -z "${GLPI_DB_NAME:-}" ] || [ -z "${GLPI_DB_USER:-}" ] || [ -z "${GLPI_DB_PASSWORD:-}" ]; then + echo "[$caller] Database ENV configuration incomplete, skipping database state checks." + exit 0 +fi + +echo "[$caller] Waiting for database to be ready..." +attempts_left=120 + +until [ $attempts_left -eq 0 ]; do + # Try a simple database connection check using PHP mysqli + if php -r " + \$conn = @new mysqli('$GLPI_DB_HOST', '$GLPI_DB_USER', '$GLPI_DB_PASSWORD', '', (int) '$GLPI_DB_PORT'); + exit(\$conn->connect_error ? 1 : 0); + " 2>/dev/null; then + echo "[$caller] The database is now ready and reachable." + exit 0 + fi + + sleep 1 + attempts_left=$((attempts_left - 1)) + echo "[$caller] Still waiting for database to be ready... $attempts_left attempts left." +done + +echo "[$caller] The database is not up or not reachable." +exit 1