feat: Implement Dockerized multi-tenant Django application with initial setup for database, migrations, and superuser creation.

This commit is contained in:
2025-12-12 18:04:36 +03:00
parent 0046b36e89
commit 4cbc5c07b9
7 changed files with 152 additions and 27 deletions

View File

@@ -4,9 +4,41 @@ set -e
# Ожидание готовности PostgreSQL
wait_for_postgres() {
echo "Waiting for PostgreSQL..."
while ! python -c "
python -c "
import psycopg2
import os
import sys
dbname = os.environ.get('DB_NAME', 'inventory_db')
user = os.environ.get('DB_USER', 'postgres')
password = os.environ.get('DB_PASSWORD', 'postgres')
host = os.environ.get('DB_HOST', 'db')
port = os.environ.get('DB_PORT', '5432')
print(f'Attempting connection to: host={host} port={port} dbname={dbname} user={user}')
try:
conn = psycopg2.connect(
dbname=dbname,
user=user,
password=password,
host=host,
port=port
)
conn.close()
print('Connection successful!')
exit(0)
except Exception as e:
print(f'Error connecting to PostgreSQL: {e}', file=sys.stderr)
exit(1)
"
while [ $? -ne 0 ]; do
echo "PostgreSQL is unavailable - sleeping"
sleep 2
python -c "
import psycopg2
import os
import sys
try:
conn = psycopg2.connect(
dbname=os.environ.get('DB_NAME', 'inventory_db'),
@@ -17,11 +49,10 @@ try:
)
conn.close()
exit(0)
except:
except Exception as e:
print(f'Retry error: {e}', file=sys.stderr)
exit(1)
" 2>/dev/null; do
echo "PostgreSQL is unavailable - sleeping"
sleep 2
"
done
echo "PostgreSQL is up!"
}
@@ -29,9 +60,33 @@ except:
# Ожидание готовности Redis
wait_for_redis() {
echo "Waiting for Redis..."
while ! python -c "
python -c "
import redis
import os
import sys
host = os.environ.get('REDIS_HOST', 'redis')
port = int(os.environ.get('REDIS_PORT', '6379'))
db = int(os.environ.get('REDIS_DB', '0'))
print(f'Attempting connection to Redis: host={host} port={port} db={db}')
try:
r = redis.Redis(host=host, port=port, db=db)
r.ping()
print('Redis connection successful!')
exit(0)
except Exception as e:
print(f'Error connecting to Redis: {e}', file=sys.stderr)
exit(1)
"
while [ $? -ne 0 ]; do
echo "Redis is unavailable - sleeping"
sleep 2
python -c "
import redis
import os
import sys
try:
r = redis.Redis(
host=os.environ.get('REDIS_HOST', 'redis'),
@@ -40,11 +95,10 @@ try:
)
r.ping()
exit(0)
except:
except Exception as e:
print(f'Redis retry error: {e}', file=sys.stderr)
exit(1)
" 2>/dev/null; do
echo "Redis is unavailable - sleeping"
sleep 2
"
done
echo "Redis is up!"
}
@@ -59,6 +113,9 @@ run_migrations() {
echo "Collecting static files..."
python manage.py collectstatic --noinput
echo "Ensuring public tenant exists..."
python /app/docker/create_public_tenant.py
}
# Создание суперпользователя если не существует
@@ -82,7 +139,7 @@ with schema_context('public'):
user = User.objects.create_superuser(
email=email,
password=password,
first_name=first_name
name=first_name
)
print(f'Superuser {email} created successfully!')
else: