From 4b2a853318cb7e7c7b2b5352da2bf03037de548a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Diot?= Date: Mon, 23 Dec 2024 18:07:39 +0100 Subject: [PATCH] Refactor database configuration update and migration logic in scheduler scripts --- src/linux/scripts/bunkerweb-scheduler.sh | 94 ++++++++++++------------ src/scheduler/entrypoint.sh | 82 ++++++++++----------- 2 files changed, 86 insertions(+), 90 deletions(-) diff --git a/src/linux/scripts/bunkerweb-scheduler.sh b/src/linux/scripts/bunkerweb-scheduler.sh index fb8441554..29a600678 100644 --- a/src/linux/scripts/bunkerweb-scheduler.sh +++ b/src/linux/scripts/bunkerweb-scheduler.sh @@ -73,21 +73,13 @@ function start() { esac # Update configuration files - if ! sed -i "s|^sqlalchemy\\.url =.*$|sqlalchemy.url = $DATABASE_URI|" alembic.ini; then - log "SYSTEMCTL" "❌" "Failed to update database URL in configuration" - exit 1 - fi - - if ! sed -i "s|^version_locations =.*$|version_locations = ${DATABASE}_versions|" alembic.ini; then - log "SYSTEMCTL" "❌" "Failed to update version locations in configuration" - exit 1 - fi - - # Check current version and stamp - log "SYSTEMCTL" "ℹ️" "Checking database version..." - installed_version=$(cat /usr/share/bunkerweb/VERSION) - # Create temporary Python script - cat > /tmp/version_check.py << EOL + if sed -i "s|^sqlalchemy\\.url =.*$|sqlalchemy.url = $DATABASE_URI|" alembic.ini; then + if sed -i "s|^version_locations =.*$|version_locations = ${DATABASE}_versions|" alembic.ini; then + # Check current version and stamp + log "SYSTEMCTL" "ℹ️" "Checking database version..." + installed_version=$(cat /usr/share/bunkerweb/VERSION) + # Create temporary Python script + cat > /tmp/version_check.py << EOL import sqlalchemy as sa from os import getenv @@ -102,45 +94,51 @@ with engine.connect() as conn: result = conn.execute(sa.text('SELECT version FROM bw_metadata WHERE id = 1')) print(next(result)[0]) except BaseException as e: - if 'doesn\'t exist' not in str(e) and 'no such table' not in str(e) and 'relation \"bw_metadata\" does not exist' not in str(e): - print('none') - else: - print('${installed_version}') + if 'doesn\'t exist' not in str(e) and 'no such table' not in str(e) and 'relation \"bw_metadata\" does not exist' not in str(e): + print('none') + else: + print('${installed_version}') EOL - current_version=$(sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 /tmp/version_check.py") - rm -f /tmp/version_check.py + current_version=$(sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 /tmp/version_check.py") + rm -f /tmp/version_check.py - if [ "$current_version" == "none" ]; then - log "SYSTEMCTL" "❌" "Failed to retrieve database version" - exit 1 - fi + if [ "$current_version" == "none" ]; then + log "SYSTEMCTL" "❌" "Failed to retrieve database version" + exit 1 + fi - if [ "$current_version" != "$installed_version" ]; then - # Find the corresponding Alembic revision by scanning migration files - MIGRATION_DIR="/usr/share/bunkerweb/db/alembic/${DATABASE}_versions" - NORMALIZED_VERSION=$(echo "$current_version" | tr '.' '_' | tr '-' '_') - REVISION=$(find "$MIGRATION_DIR" -maxdepth 1 -type f -name "*_upgrade_to_version_${NORMALIZED_VERSION}.py" -exec basename {} \; | awk -F_ '{print $1}') + if [ "$current_version" != "$installed_version" ]; then + # Find the corresponding Alembic revision by scanning migration files + MIGRATION_DIR="/usr/share/bunkerweb/db/alembic/${DATABASE}_versions" + NORMALIZED_VERSION=$(echo "$current_version" | tr '.' '_' | tr '-' '_') + REVISION=$(find "$MIGRATION_DIR" -maxdepth 1 -type f -name "*_upgrade_to_version_${NORMALIZED_VERSION}.py" -exec basename {} \; | awk -F_ '{print $1}') - if [ -z "$REVISION" ]; then - log "SYSTEMCTL" "❌" "No migration file found for database version: $current_version" - exit 1 + if [ -z "$REVISION" ]; then + log "SYSTEMCTL" "❌" "No migration file found for database version: $current_version" + exit 1 + fi + + # Stamp the database with the determined revision + if ! sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 -m alembic stamp \"$REVISION\""; then + log "SYSTEMCTL" "❌" "Failed to stamp database with revision: $REVISION" + exit 1 + fi + + # Run database migration + log "SYSTEMCTL" "ℹ️" "Running database migration..." + if ! sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 -m alembic upgrade head"; then + log "SYSTEMCTL" "❌" "Database migration failed" + exit 1 + fi + + log "SYSTEMCTL" "✅" "Database migration completed successfully" + fi + else + log "ENTRYPOINT" "❌" "Failed to update version locations in configuration, migration aborted" fi - - # Stamp the database with the determined revision - if ! sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 -m alembic stamp \"$REVISION\""; then - log "SYSTEMCTL" "❌" "Failed to stamp database with revision: $REVISION" - exit 1 - fi - - # Run database migration - log "SYSTEMCTL" "ℹ️" "Running database migration..." - if ! sudo -E -u nginx -g nginx /bin/bash -c "PYTHONPATH=$PYTHONPATH python3 -m alembic upgrade head"; then - log "SYSTEMCTL" "❌" "Database migration failed" - exit 1 - fi - - log "SYSTEMCTL" "✅" "Database migration completed successfully" + else + log "ENTRYPOINT" "❌" "Failed to update database URL in configuration, migration aborted" fi cd - > /dev/null || exit 1 diff --git a/src/scheduler/entrypoint.sh b/src/scheduler/entrypoint.sh index 175688715..770815ed8 100644 --- a/src/scheduler/entrypoint.sh +++ b/src/scheduler/entrypoint.sh @@ -59,20 +59,12 @@ case "$db_type" in esac # Update configuration files -if ! sed -i "s|^sqlalchemy\\.url =.*$|sqlalchemy.url = $DATABASE_URI|" alembic.ini; then - log "ENTRYPOINT" "❌" "Failed to update database URL in configuration" - exit 1 -fi - -if ! sed -i "s|^version_locations =.*$|version_locations = ${DATABASE}_versions|" alembic.ini; then - log "ENTRYPOINT" "❌" "Failed to update version locations in configuration" - exit 1 -fi - -# Check current version and stamp -log "ENTRYPOINT" "ℹ️" "Checking database version..." -installed_version=$(cat /usr/share/bunkerweb/VERSION) -current_version=$(python3 -c " +if sed -i "s|^sqlalchemy\\.url =.*$|sqlalchemy.url = $DATABASE_URI|" alembic.ini; then + if sed -i "s|^version_locations =.*$|version_locations = ${DATABASE}_versions|" alembic.ini; then + # Check current version and stamp + log "ENTRYPOINT" "ℹ️" "Checking database version..." + installed_version=$(cat /usr/share/bunkerweb/VERSION) + current_version=$(python3 -c " import sqlalchemy as sa from os import getenv @@ -91,38 +83,44 @@ with engine.connect() as conn: print('none') else: print('${installed_version}') -") + ") -if [ "$current_version" == "none" ]; then - log "ENTRYPOINT" "❌" "Failed to retrieve database version" - exit 1 -fi - -if [ "$current_version" != "$installed_version" ]; then - # Find the corresponding Alembic revision by scanning migration files - MIGRATION_DIR="/usr/share/bunkerweb/db/alembic/${DATABASE}_versions" - NORMALIZED_VERSION=$(echo "$current_version" | tr '.' '_' | tr '-' '_') - REVISION=$(find "$MIGRATION_DIR" -maxdepth 1 -type f -name "*_upgrade_to_version_${NORMALIZED_VERSION}.py" -exec basename {} \; | awk -F_ '{print $1}') - - if [ -z "$REVISION" ]; then - log "ENTRYPOINT" "❌" "No migration file found for database version: $current_version" - exit 1 - fi - - # Stamp the database with the determined revision - if ! python3 -m alembic stamp "$REVISION"; then - log "ENTRYPOINT" "❌" "Failed to stamp database with revision: $REVISION" + if [ "$current_version" == "none" ]; then + log "ENTRYPOINT" "❌" "Failed to retrieve database version" exit 1 - fi + fi - # Run database migration - log "ENTRYPOINT" "ℹ️" "Running database migration..." - if ! python3 -m alembic upgrade head; then - log "ENTRYPOINT" "❌" "Database migration failed" - exit 1 - fi + if [ "$current_version" != "$installed_version" ]; then + # Find the corresponding Alembic revision by scanning migration files + MIGRATION_DIR="/usr/share/bunkerweb/db/alembic/${DATABASE}_versions" + NORMALIZED_VERSION=$(echo "$current_version" | tr '.' '_' | tr '-' '_') + REVISION=$(find "$MIGRATION_DIR" -maxdepth 1 -type f -name "*_upgrade_to_version_${NORMALIZED_VERSION}.py" -exec basename {} \; | awk -F_ '{print $1}') - log "ENTRYPOINT" "✅" "Database migration completed successfully" + if [ -z "$REVISION" ]; then + log "ENTRYPOINT" "❌" "No migration file found for database version: $current_version" + exit 1 + fi + + # Stamp the database with the determined revision + if ! python3 -m alembic stamp "$REVISION"; then + log "ENTRYPOINT" "❌" "Failed to stamp database with revision: $REVISION" + exit 1 + fi + + # Run database migration + log "ENTRYPOINT" "ℹ️" "Running database migration..." + if ! python3 -m alembic upgrade head; then + log "ENTRYPOINT" "❌" "Database migration failed" + exit 1 + fi + + log "ENTRYPOINT" "✅" "Database migration completed successfully" + fi + else + log "ENTRYPOINT" "❌" "Failed to update version locations in configuration, migration aborted" + fi +else + log "ENTRYPOINT" "❌" "Failed to update database URL in configuration, migration aborted" fi cd - > /dev/null || exit 1