Skip to content

Commit

Permalink
deployment fix
Browse files Browse the repository at this point in the history
  • Loading branch information
QuitoTactico committed Nov 13, 2024
1 parent 2762ed0 commit 4e5b906
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
*pycache*
db_backup
*.log
*.dot
/scripts/data.json
*.dot
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ RUN pip install -r requirements.txt
COPY . .

# Copia los scripts y cambia el archivo de settings
COPY scripts /app/scripts
RUN chmod +x /app/scripts/init.sh
COPY scripts /scripts
RUN chmod +x /scripts/init.sh

# Configura variables de entorno
ENV PYTHONDONTWRITEBYTECODE 1
Expand All @@ -26,4 +26,4 @@ ENV PYTHONUNBUFFERED 1
EXPOSE 80

# Ejecuta el script de inicialización
CMD ["sh", "/app/scripts/init.sh"]
CMD ["sh", "/scripts/init.sh"]
2 changes: 1 addition & 1 deletion docker-compose-swarm.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
web:
image: quitotactico/softserve-academy
command: ["sh", "/app/scripts/init.sh"]
command: ["sh", "/scripts/init.sh"]
environment:
- DJANGO_SETTINGS_MODULE=SoftServeAcademy.settings_deployment
depends_on:
Expand Down
Binary file added scripts/data.json
Binary file not shown.
15 changes: 9 additions & 6 deletions scripts/init.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# Cambia al directorio del script
cd "$(dirname "$0")"

# Espera a que la base de datos esté lista antes de aplicar migraciones y cargar datos
echo " "

Expand All @@ -17,10 +20,10 @@ done
echo "MySQL está listo."

# Verifica si el archivo data.json no existe ya en el contenedor para evitar múltiples cargas
if [ ! -f "/app/scripts/data.json" ]; then
if [ ! -f "data.json" ]; then
echo "Generando data.json desde la base de datos SQLite."
# Genera el archivo data.json usando settings.py (configurado con SQLite)
python manage.py dumpdata --settings=SoftServeAcademy.settings > /app/scripts/data.json
python manage.py dumpdata --settings=SoftServeAcademy.settings > data.json
echo "Archivo data.json generado con éxito desde SQLite."
else
echo "Archivo data.json ya existe, omitiendo generación."
Expand All @@ -34,18 +37,18 @@ echo "Migraciones aplicadas con éxito."
# Carga los datos en MySQL solo si la tabla de usuarios no existe
if ! python manage.py inspectdb --settings=SoftServeAcademy.settings_deployment | grep -q "auth_user"; then
echo "Base de datos vacía, cargando datos desde data.json."
python manage.py loaddata /app/scripts/data.json --settings=SoftServeAcademy.settings_deployment
python manage.py loaddata data.json --settings=SoftServeAcademy.settings_deployment
echo "Datos cargados con éxito desde data.json."
else
echo "Base de datos ya inicializada, no se cargan datos."
fi

# Elimina el archivo data.json si fue cargado
if [ -f "/app/scripts/data.json" ]; then
rm /app/scripts/data.json
if [ -f "data.json" ]; then
rm data.json
echo "Archivo data.json eliminado del contenedor."
fi

# Inicia el servidor de Django usando settings_deployment (con MySQL)
echo "Iniciando el servidor de Django..."
exec python manage.py runserver 0.0.0.0:80 --settings=SoftServeAcademy.settings_deployment
exec python manage.py runserver 0.0.0.0:80 --settings=SoftServeAcademy.settings_deployment

0 comments on commit 4e5b906

Please sign in to comment.