From cd09115a79d07a57a4993ec221a507ddc3af222f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20B=C3=B6lling?= <63500074+derdennis1012@users.noreply.github.com> Date: Wed, 2 Apr 2025 12:12:50 +0200 Subject: [PATCH] Ensure entrypoint waits until Moodle is fully installed Fixes #23 Add logic to wait for Moodle to be fully installed and healthy before proceeding with the entry point. * **docker-compose.yml** - Modify health check to include a check for the admin page to ensure Moodle is fully installed. * **docker/moodle/scripts/setup.sh** - Add function `is_moodle_installed` to check if Moodle is fully installed by querying the database. - Add function `is_moodle_healthy` to check the health status of the Moodle service. - Add a loop to wait for Moodle to be fully installed and healthy before proceeding with the setup script. * **Makefile** - Modify `install-plugins` command to wait for Moodle to be fully installed and healthy before proceeding. - Modify `check-updates` command to wait for Moodle to be fully installed and healthy before proceeding. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/derdennis1012/moodle-cli/issues/23?shareId=XXXX-XXXX-XXXX-XXXX). --- Makefile | 4 ++-- docker-compose.yml | 2 +- docker/moodle/scripts/setup.sh | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index db5859f..f1f0ad5 100644 --- a/Makefile +++ b/Makefile @@ -8,10 +8,10 @@ restart: docker compose restart install-plugins: - docker exec -it moodle-app bash /custom_scripts/setup.sh + docker exec -it moodle-app bash -c "while ! /custom_scripts/setup.sh is_moodle_installed || ! /custom_scripts/setup.sh is_moodle_healthy; do sleep 30; done; /custom_scripts/setup.sh" check-updates: - docker exec -it moodle-app bash /custom_scripts/check_updates.sh $(VERSION) + docker exec -it moodle-app bash -c "while ! /custom_scripts/setup.sh is_moodle_installed || ! /custom_scripts/setup.sh is_moodle_healthy; do sleep 30; done; /custom_scripts/check_updates.sh $(VERSION)" common-commands: docker exec -it moodle-app bash /custom_scripts/setup.sh diff --git a/docker-compose.yml b/docker-compose.yml index 87b83c4..88e0b4a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -39,7 +39,7 @@ services: mariadb: condition: service_healthy healthcheck: - test: ["CMD-SHELL", "curl -fs http://localhost:8080/login/index.php || exit 1"] + test: ["CMD-SHELL", "curl -fs http://localhost:8080/login/index.php && curl -fs http://localhost:8080/admin/index.php || exit 1"] interval: 120s timeout: 30s retries: 3 diff --git a/docker/moodle/scripts/setup.sh b/docker/moodle/scripts/setup.sh index c83fd6d..f4ca6dc 100644 --- a/docker/moodle/scripts/setup.sh +++ b/docker/moodle/scripts/setup.sh @@ -138,12 +138,50 @@ install_german_locale() { log_message "🚀 Language configuration script completed successfully!" } +# đŸšĻ Function to check if Moodle is fully installed +is_moodle_installed() { + log_message "🔄 Checking if Moodle is fully installed..." + local db_host="${MOODLE_DATABASE_HOST:-mariadb}" + local db_user="${MOODLE_DATABASE_USER:-bn_moodle}" + local db_password="${MOODLE_DATABASE_PASSWORD:-supersecure123}" + local db_name="${MOODLE_DATABASE_NAME:-bitnami_moodle}" + + local query="SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '$db_name';" + local result=$(mysql -h "$db_host" -u "$db_user" -p"$db_password" -e "$query" -s -N) + + if [[ "$result" -gt 0 ]]; then + log_message "✅ Moodle is fully installed." + return 0 + else + log_message "❌ Moodle is not fully installed yet." + return 1 + fi +} + +# đŸšĻ Function to check the health status of the Moodle service +is_moodle_healthy() { + log_message "🔄 Checking the health status of the Moodle service..." + if curl -fs http://localhost:8080/login/index.php && curl -fs http://localhost:8080/admin/index.php; then + log_message "✅ Moodle service is healthy." + return 0 + else + log_message "❌ Moodle service is not healthy yet." + return 1 + fi +} + # đŸšĻ Main script execution log_message "🏁 Starting setup script..." MOODLE_VERSION=$(grep '$release' "$MOODLE_DIR/version.php" | grep -oP '\d+\.\d+') log_message "â„šī¸ Detected Moodle version: $MOODLE_VERSION" +# Wait for Moodle to be fully installed and healthy +while ! is_moodle_installed || ! is_moodle_healthy; do + log_message "âŗ Waiting for Moodle to be fully installed and healthy..." + sleep 30 +done + install_moosh update_plugin_list filter_plugin_list