Добавлен entrypoint.sh с правами на выполнение для деплоя
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -24,7 +24,6 @@ staticfiles/
|
|||||||
.env.local
|
.env.local
|
||||||
*.env
|
*.env
|
||||||
docker/.env.docker
|
docker/.env.docker
|
||||||
docker/entrypoint.sh
|
|
||||||
|
|
||||||
# IDE
|
# IDE
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|||||||
135
docker/entrypoint.sh
Executable file
135
docker/entrypoint.sh
Executable file
@@ -0,0 +1,135 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Ожидание готовности PostgreSQL
|
||||||
|
wait_for_postgres() {
|
||||||
|
echo "Waiting for PostgreSQL..."
|
||||||
|
while ! python -c "
|
||||||
|
import psycopg2
|
||||||
|
import os
|
||||||
|
try:
|
||||||
|
conn = psycopg2.connect(
|
||||||
|
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')
|
||||||
|
)
|
||||||
|
conn.close()
|
||||||
|
exit(0)
|
||||||
|
except:
|
||||||
|
exit(1)
|
||||||
|
" 2>/dev/null; do
|
||||||
|
echo "PostgreSQL is unavailable - sleeping"
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
echo "PostgreSQL is up!"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Ожидание готовности Redis
|
||||||
|
wait_for_redis() {
|
||||||
|
echo "Waiting for Redis..."
|
||||||
|
while ! python -c "
|
||||||
|
import redis
|
||||||
|
import os
|
||||||
|
try:
|
||||||
|
r = redis.Redis(
|
||||||
|
host=os.environ.get('REDIS_HOST', 'redis'),
|
||||||
|
port=int(os.environ.get('REDIS_PORT', '6379')),
|
||||||
|
db=int(os.environ.get('REDIS_DB', '0'))
|
||||||
|
)
|
||||||
|
r.ping()
|
||||||
|
exit(0)
|
||||||
|
except:
|
||||||
|
exit(1)
|
||||||
|
" 2>/dev/null; do
|
||||||
|
echo "Redis is unavailable - sleeping"
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
echo "Redis is up!"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Применение миграций и создание суперпользователя
|
||||||
|
run_migrations() {
|
||||||
|
echo "Running migrations for shared apps..."
|
||||||
|
python manage.py migrate_schemas --shared
|
||||||
|
|
||||||
|
echo "Running migrations for tenant schemas..."
|
||||||
|
python manage.py migrate_schemas --tenant
|
||||||
|
|
||||||
|
echo "Collecting static files..."
|
||||||
|
python manage.py collectstatic --noinput
|
||||||
|
}
|
||||||
|
|
||||||
|
# Создание суперпользователя если не существует
|
||||||
|
create_superuser() {
|
||||||
|
echo "Creating superuser if not exists..."
|
||||||
|
python manage.py shell << EOF
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.db import connection
|
||||||
|
from django_tenants.utils import schema_context
|
||||||
|
import os
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
# Создаём суперпользователя в public схеме из переменных окружения
|
||||||
|
with schema_context('public'):
|
||||||
|
email = os.environ.get('TENANT_ADMIN_EMAIL', 'admin@example.com')
|
||||||
|
password = os.environ.get('TENANT_ADMIN_PASSWORD', 'changeme')
|
||||||
|
first_name = os.environ.get('TENANT_ADMIN_NAME', 'Admin')
|
||||||
|
|
||||||
|
if not User.objects.filter(email=email).exists():
|
||||||
|
user = User.objects.create_superuser(
|
||||||
|
email=email,
|
||||||
|
password=password,
|
||||||
|
first_name=first_name
|
||||||
|
)
|
||||||
|
print(f'Superuser {email} created successfully!')
|
||||||
|
else:
|
||||||
|
print(f'Superuser {email} already exists.')
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
web)
|
||||||
|
wait_for_postgres
|
||||||
|
wait_for_redis
|
||||||
|
run_migrations
|
||||||
|
create_superuser
|
||||||
|
echo "Starting Gunicorn..."
|
||||||
|
exec gunicorn myproject.wsgi:application \
|
||||||
|
--bind 0.0.0.0:8000 \
|
||||||
|
--workers 3 \
|
||||||
|
--threads 2 \
|
||||||
|
--timeout 120 \
|
||||||
|
--access-logfile - \
|
||||||
|
--error-logfile - \
|
||||||
|
--capture-output
|
||||||
|
;;
|
||||||
|
celery-worker)
|
||||||
|
wait_for_postgres
|
||||||
|
wait_for_redis
|
||||||
|
echo "Starting Celery Worker..."
|
||||||
|
exec celery -A myproject worker \
|
||||||
|
-l info \
|
||||||
|
-Q celery,photo_processing \
|
||||||
|
--concurrency=2
|
||||||
|
;;
|
||||||
|
celery-beat)
|
||||||
|
wait_for_postgres
|
||||||
|
wait_for_redis
|
||||||
|
echo "Starting Celery Beat..."
|
||||||
|
exec celery -A myproject beat -l info
|
||||||
|
;;
|
||||||
|
migrate)
|
||||||
|
wait_for_postgres
|
||||||
|
run_migrations
|
||||||
|
create_superuser
|
||||||
|
;;
|
||||||
|
shell)
|
||||||
|
exec python manage.py shell
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
exec "$@"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
44
ГИД ПО ЗАПУСКУ
Normal file
44
ГИД ПО ЗАПУСКУ
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
docker run -d `
|
||||||
|
--name postgres17 `
|
||||||
|
-e POSTGRES_PASSWORD=postgres `
|
||||||
|
-e POSTGRES_USER=postgres `
|
||||||
|
-e POSTGRES_DB=inventory_db `
|
||||||
|
-p 5432:5432 `
|
||||||
|
-v postgres17-data:/var/lib/postgresql/data `
|
||||||
|
postgres:17
|
||||||
|
|
||||||
|
# 2. Создаем миграции с нуля
|
||||||
|
python manage.py makemigrations
|
||||||
|
|
||||||
|
# 3. Применяем все миграции
|
||||||
|
python manage.py migrate
|
||||||
|
|
||||||
|
# 4. Создаем главного тенанта (если нужно)
|
||||||
|
python manage.py shell
|
||||||
|
# Внутри shell:
|
||||||
|
from tenants.models import Client, Domain
|
||||||
|
client = Client.objects.create(
|
||||||
|
name='Main',
|
||||||
|
schema_name='public'
|
||||||
|
)
|
||||||
|
Domain.objects.create(
|
||||||
|
domain='localhost',
|
||||||
|
tenant=client,
|
||||||
|
is_primary=True
|
||||||
|
)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# 5. Создаем суперпользователя для public схемы
|
||||||
|
python manage.py createsuperuser
|
||||||
|
|
||||||
|
# 6. Создаем суперпользователя для конкретного тенанта (опционально)
|
||||||
|
python manage.py shell
|
||||||
|
# Внутри:
|
||||||
|
from tenants.models import Client
|
||||||
|
from django.core.management import call_command
|
||||||
|
from django_tenants.utils import schema_context
|
||||||
|
|
||||||
|
client = Client.objects.get(schema_name='public')
|
||||||
|
with schema_context(client):
|
||||||
|
call_command('createsuperuser')
|
||||||
|
exit()
|
||||||
Reference in New Issue
Block a user