109 lines
2.7 KiB
Bash
109 lines
2.7 KiB
Bash
|
|
#!/bin/sh
|
||
|
|
set -e
|
||
|
|
|
||
|
|
# Database initialization entrypoint for PieFed
|
||
|
|
# This script runs as a Kubernetes Job before web/worker pods start
|
||
|
|
|
||
|
|
log() {
|
||
|
|
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
|
||
|
|
}
|
||
|
|
|
||
|
|
log "Starting PieFed database initialization..."
|
||
|
|
|
||
|
|
# Wait for database to be available
|
||
|
|
wait_for_db() {
|
||
|
|
log "Waiting for database connection..."
|
||
|
|
until python -c "
|
||
|
|
import psycopg2
|
||
|
|
import os
|
||
|
|
from urllib.parse import urlparse
|
||
|
|
|
||
|
|
try:
|
||
|
|
# Parse DATABASE_URL
|
||
|
|
database_url = os.environ.get('DATABASE_URL', '')
|
||
|
|
if not database_url:
|
||
|
|
raise Exception('DATABASE_URL not set')
|
||
|
|
|
||
|
|
# Parse the URL to extract connection details
|
||
|
|
parsed = urlparse(database_url)
|
||
|
|
conn = psycopg2.connect(
|
||
|
|
host=parsed.hostname,
|
||
|
|
port=parsed.port or 5432,
|
||
|
|
database=parsed.path[1:], # Remove leading slash
|
||
|
|
user=parsed.username,
|
||
|
|
password=parsed.password
|
||
|
|
)
|
||
|
|
conn.close()
|
||
|
|
print('Database connection successful')
|
||
|
|
except Exception as e:
|
||
|
|
print(f'Database connection failed: {e}')
|
||
|
|
exit(1)
|
||
|
|
" 2>/dev/null; do
|
||
|
|
log "Database not ready, waiting 2 seconds..."
|
||
|
|
sleep 2
|
||
|
|
done
|
||
|
|
log "Database connection established"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Wait for Redis to be available
|
||
|
|
wait_for_redis() {
|
||
|
|
log "Waiting for Redis connection..."
|
||
|
|
until python -c "
|
||
|
|
import redis
|
||
|
|
import os
|
||
|
|
|
||
|
|
try:
|
||
|
|
cache_redis_url = os.environ.get('CACHE_REDIS_URL', '')
|
||
|
|
if cache_redis_url:
|
||
|
|
r = redis.from_url(cache_redis_url)
|
||
|
|
else:
|
||
|
|
# Fallback to separate host/port for backwards compatibility
|
||
|
|
r = redis.Redis(host='redis', port=6379, password=os.environ.get('REDIS_PASSWORD', ''))
|
||
|
|
r.ping()
|
||
|
|
print('Redis connection successful')
|
||
|
|
except Exception as e:
|
||
|
|
print(f'Redis connection failed: {e}')
|
||
|
|
exit(1)
|
||
|
|
" 2>/dev/null; do
|
||
|
|
log "Redis not ready, waiting 2 seconds..."
|
||
|
|
sleep 2
|
||
|
|
done
|
||
|
|
log "Redis connection established"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Main initialization sequence
|
||
|
|
main() {
|
||
|
|
# Change to application directory
|
||
|
|
cd /app
|
||
|
|
|
||
|
|
# Wait for dependencies
|
||
|
|
wait_for_db
|
||
|
|
wait_for_redis
|
||
|
|
|
||
|
|
# Run database migrations
|
||
|
|
log "Running database migrations..."
|
||
|
|
export FLASK_APP=pyfedi.py
|
||
|
|
|
||
|
|
# Run Flask database migrations
|
||
|
|
flask db upgrade
|
||
|
|
log "Database migrations completed"
|
||
|
|
|
||
|
|
# Populate community search index
|
||
|
|
log "Populating community search..."
|
||
|
|
flask populate_community_search
|
||
|
|
log "Community search populated"
|
||
|
|
|
||
|
|
# Ensure log files have correct ownership for dual logging (file + stdout)
|
||
|
|
if [ -f /app/logs/pyfedi.log ]; then
|
||
|
|
chown piefed:piefed /app/logs/pyfedi.log
|
||
|
|
chmod 664 /app/logs/pyfedi.log
|
||
|
|
log "Fixed log file ownership for piefed user"
|
||
|
|
fi
|
||
|
|
|
||
|
|
log "Database initialization completed successfully!"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Run the main function
|
||
|
|
main
|
||
|
|
|