diff --git a/.env b/.env index be036aa..91b5a8d 100644 --- a/.env +++ b/.env @@ -1,11 +1,11 @@ #General settings LC_ALL=C.UTF-8 -SIRM_SITE=10.10.10.30 -SIRM_PORT=8085 -SIRM_MAX_UPLOAD_SIZE_MB=200 +SIRM_SITE= +SIRM_PORT= +SIRM_MAX_UPLOAD_SIZE_MB=20 #Postgres settings -POSTGRES_PORT_5432_TCP_ADDR=radarsys-postgres +POSTGRES_PORT_5432_TCP_ADDR=sirm-postgres POSTGRES_PORT_5432_TCP_PORT=5432 DB_NAME=radarsys DB_USER=docker diff --git a/.gitignore b/.gitignore index 483345a..0108638 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,4 @@ volumes/sirm/static_files/* migrations *.pyc *initial.py -.DS_Store -*.sqlite -.env \ No newline at end of file +.DS_Store \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 5f8f443..9eb2e06 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,9 @@ FROM python:3.8-slim RUN mkdir -p /workspace/sirm WORKDIR /workspace -# Copy "start_realtime" file -ADD ./setup/start_realtime.sh ./start_realtime.sh -RUN chmod +x ./start_realtime.sh +# Copy "setup" files +COPY ./setup/ ./setup/ +RUN chmod +x ./setup/start_realtime.sh # Set working directory WORKDIR /workspace/sirm @@ -17,4 +17,4 @@ RUN pip install -v --timeout 120 -r requirements.txt --no-cache-dir EXPOSE 8080 -ENTRYPOINT ["/workspace/start_realtime.sh"] \ No newline at end of file +ENTRYPOINT ["/workspace/setup/start_realtime.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 82bc000..f949d85 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,11 @@ Download the application *radarsys_met* to your workspace ### 2. Config .env file Update enviroment vars (/path/to/radarsys_met/.env) + #General settings - SIRM_SITE=localhost - SIRM_PORT=8080 - SIRM_MAX_UPLOAD_SIZE_MB=200 + SIRM_SITE= + SIRM_PORT= + SIRM_MAX_UPLOAD_SIZE_MB=20 #Postgres settings DB_NAME=radarsys diff --git a/requirements.txt b/requirements.txt index bb39cbb..273d194 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,15 @@ -Django -django-bootstrap4 -psycopg2-binary -django-polymorphic -gunicorn -requests - +Django==4.0.3 +django-bootstrap4==22.1 +psycopg2-binary==2.9.3 +django-polymorphic==3.1.0 +gunicorn==20.1.0 +requests==2.27.1 +backports.zoneinfo==0.2.1 +asgiref==3.5.0 +sqlparse==0.4.2 +beautifulsoup4==4.10.0 +idna==3.3 +urllib3==1.26.9 +charset-normalizer==2.0.12 +certifi==2021.10.8 +soupsieve==2.3.1 \ No newline at end of file diff --git a/setup/prerun.py b/setup/prerun.py new file mode 100644 index 0000000..85d4ec2 --- /dev/null +++ b/setup/prerun.py @@ -0,0 +1,34 @@ +import psycopg2 +import sys +import os + +RETRY = 5 + +def check_db_connection(retry=None): + + print('[prerun] Start check_db_connection...') + + if retry is None: + retry = RETRY + elif retry == 0: + print('[prerun] Giving up after 5 tries...') + sys.exit(1) + + try: + connection = psycopg2.connect(user=os.environ.get('DB_USER', 'localhost'), + host=os.environ.get('POSTGRES_PORT_5432_TCP_ADDR', '5432'), + password=os.environ.get('DB_PASSWORD', 'docker'), + database=os.environ.get('DB_NAME', 'radarsys')) + print('[prerun] Database is ready to accept connections') + + except psycopg2.Error as e: + print((str(e))) + print('[prerun] Unable to connect to the database...try again in a while.') + import time + time.sleep(10) + check_db_connection(retry = retry - 1) + else: + connection.close() + +if __name__ == '__main__': + check_db_connection() \ No newline at end of file diff --git a/setup/start_realtime.sh b/setup/start_realtime.sh index 48cccb8..493de72 100755 --- a/setup/start_realtime.sh +++ b/setup/start_realtime.sh @@ -1,8 +1,10 @@ #!/bin/bash -if [ -f /sirm/radarsys.sqlite ]; +python ../setup/prerun.py || { echo '[DJANGO prerun] FAILED. Exiting...' ; exit 1; } + +if [ -f ../setup/.gitkeep ]; then - echo "The commands 'makemigrations', 'migrate' and 'collectstatic' were not exectued" + echo "The commands 'makemigrations', 'migrate' and '.gitkeep' were not exectued" else # Apply database makemigrations echo "Apply makemigrations" @@ -13,15 +15,18 @@ if [ -f /sirm/radarsys.sqlite ]; python manage.py migrate python manage.py loaddata main python manage.py loaddata tx + # Collect static files - echo "Collect static files" - python manage.py collectstatic --noinput - + #echo "Collect static files" + #python manage.py collectstatic --noinput + + # Create ".gitkeep" file in "setup" folder + echo "Create .gitkeep" + touch ../setup/.gitkeep fi -# Create superuser -python manage.py createsuperuser_if_none_exists --user=${SIRM_USER} --email=${SIRM_EMAIL} --password=${SIRM_PASSWORD} +echo "Create Superuser" +python manage.py createsuperuser_if_none_exists -# Runserver -echo "Collect static files" +echo "Run server" python manage.py runserver 0.0.0.0:8080 \ No newline at end of file diff --git a/volumes/sirm/apps/accounts/management/commands/createsuperuser_if_none_exists.py b/volumes/sirm/apps/accounts/management/commands/createsuperuser_if_none_exists.py index 3db12a1..71d4585 100644 --- a/volumes/sirm/apps/accounts/management/commands/createsuperuser_if_none_exists.py +++ b/volumes/sirm/apps/accounts/management/commands/createsuperuser_if_none_exists.py @@ -1,3 +1,5 @@ +import os + from django.core.management.base import BaseCommand from django.contrib.auth import get_user_model @@ -8,17 +10,12 @@ class Command(BaseCommand): manage.py createsuperuser_if_none_exists --user=admin123 --password=admin123 --email=admin123@igp.gob.pe """ - def add_arguments(self, parser): - parser.add_argument("--user", required=True) - parser.add_argument("--password", required=True) - parser.add_argument("--email", default="admin123@igp.gob.pe") - def handle(self, *args, **options): User = get_user_model() - username = options["user"] - password = options["password"] - email = options["email"] + username = os.environ.get('SIRM_USER', 'admin') + password = os.environ.get('SIRM_PASSWORD', 'soporte') + email = os.environ.get('SIRM_EMAIL', 'admin@igp.gob.pe') if User.objects.exists(): self.stdout.write(f'Local user "{username}" currently exists')