107 lines
3.3 KiB
Bash
107 lines
3.3 KiB
Bash
#!/bin/bash
|
|
|
|
# Print environment variables for debugging
|
|
echo "Environment variables:"
|
|
echo "POSTGRES_USER: $POSTGRES_USER"
|
|
echo "POSTGRES_PASSWORD: $POSTGRES_PASSWORD"
|
|
echo "POSTGRES_DB: $POSTGRES_DB"
|
|
echo "DATABASE_URL: $DATABASE_URL"
|
|
|
|
# Function to wait for database
|
|
wait_for_db() {
|
|
echo "Waiting for database..."
|
|
while ! nc -z db 5432; do
|
|
sleep 1
|
|
done
|
|
echo "Database is ready!"
|
|
}
|
|
|
|
# Function to create database if it doesn't exist
|
|
create_database() {
|
|
echo "Creating database if it doesn't exist..."
|
|
PGPASSWORD=$POSTGRES_PASSWORD psql -h db -U $POSTGRES_USER -tc "SELECT 1 FROM pg_database WHERE datname = '$POSTGRES_DB'" | grep -q 1 || \
|
|
PGPASSWORD=$POSTGRES_PASSWORD psql -h db -U $POSTGRES_USER -c "CREATE DATABASE $POSTGRES_DB"
|
|
echo "Database check/creation complete!"
|
|
}
|
|
|
|
# Wait for database to be ready
|
|
wait_for_db
|
|
|
|
# Create database if it doesn't exist
|
|
create_database
|
|
|
|
# Wait for PostgreSQL to be ready to accept connections
|
|
echo "Waiting for PostgreSQL to accept connections..."
|
|
until PGPASSWORD=$POSTGRES_PASSWORD psql -h db -U $POSTGRES_USER -d $POSTGRES_DB -c '\q'; do
|
|
echo "PostgreSQL is unavailable - sleeping"
|
|
sleep 1
|
|
done
|
|
echo "PostgreSQL is up - executing command"
|
|
|
|
# Run all initialization in a single Python script to avoid multiple Flask instances
|
|
echo "Running initialization..."
|
|
python3 -c "
|
|
from app import create_app
|
|
from models import SiteSettings, db, User
|
|
from migrations.add_events_table import upgrade as upgrade_events
|
|
from migrations.add_notifs_table import upgrade as upgrade_notifs
|
|
from utils.email_templates import create_default_templates
|
|
|
|
app = create_app()
|
|
with app.app_context():
|
|
# Run migrations
|
|
print('Running database migrations...')
|
|
from flask_migrate import upgrade
|
|
upgrade()
|
|
|
|
# Create events table
|
|
print('Creating events table...')
|
|
try:
|
|
upgrade_events()
|
|
print('Events table created successfully')
|
|
except Exception as e:
|
|
print(f'Error creating events table: {e}')
|
|
|
|
# Create notifs table
|
|
print('Creating notifs table...')
|
|
try:
|
|
upgrade_notifs()
|
|
print('Notifs table created successfully')
|
|
except Exception as e:
|
|
print(f'Error creating notifs table: {e}')
|
|
|
|
# Create default site settings
|
|
print('Creating default site settings...')
|
|
try:
|
|
settings = SiteSettings.get_settings()
|
|
print('Default site settings created successfully')
|
|
except Exception as e:
|
|
print(f'Error creating site settings: {e}')
|
|
|
|
# Create admin user if it doesn't exist
|
|
print('Creating admin user...')
|
|
admin = User.query.filter_by(email='administrator@docupulse.com').first()
|
|
if not admin:
|
|
admin = User(
|
|
username='administrator',
|
|
email='administrator@docupulse.com',
|
|
last_name='None',
|
|
company='docupulse',
|
|
is_admin=True,
|
|
is_active=True
|
|
)
|
|
admin.set_password('changeme')
|
|
db.session.add(admin)
|
|
db.session.commit()
|
|
print('Default administrator user created successfully.')
|
|
else:
|
|
print('Admin user already exists.')
|
|
|
|
# Create default templates
|
|
print('Creating default templates...')
|
|
create_default_templates()
|
|
"
|
|
|
|
# Start the application
|
|
echo "Starting application..."
|
|
exec gunicorn --bind 0.0.0.0:5000 app:app |