diff --git a/mikroman/Dockerfile b/mikroman/Dockerfile index 961419e..04ff6d2 100644 --- a/mikroman/Dockerfile +++ b/mikroman/Dockerfile @@ -31,6 +31,8 @@ COPY server-conf.json /conf/ ARG AM_I_IN_A_DOCKER_CONTAINER=Yes COPY init.sh /app/ COPY initpy.py /app/ +COPY testdb.py /app/ + RUN chmod +x /app/init.sh # background spooler dir diff --git a/mikroman/init.sh b/mikroman/init.sh index d9a4d77..e3ebbe3 100644 --- a/mikroman/init.sh +++ b/mikroman/init.sh @@ -8,9 +8,22 @@ else fi +# First Check if db is ready and accepting connections + +python3 /app/testdb.py + +# Check if the Python script ran successfully +if [ $? -ne 0 ]; then + echo "An error occurred while executing the SQL commands." + exit 1 +else + echo "Good News! Database is Running. :)" +fi + CONTAINER_ALREADY_STARTED="CONTAINER_ALREADY_STARTED_PLACEHOLDER" if [ ! -e $CONTAINER_ALREADY_STARTED ]; then echo "-- Initializing the mikroman for first run --" + # YOUR_JUST_ONCE_LOGIC_HERE cd /app && export PYTHONPATH=/app/py && export PYSRV_CONFIG_PATH=/conf/server-conf.json && python3 scripts/dbmigrate.py @@ -49,18 +62,18 @@ cat << EOF1 | tee init.sql >/dev/null INSERT INTO public.permissions(id, name, perms) VALUES (3, 'write', '{"api": true, "dude": false, "ftp": false, "local": true, "password": true, "policy": false, "read": true, "reboot": true, "rest-api": true, "romon": true, "sensitive": true, "sniff": true, "ssh": true, "telnet": true, "test": true, "tikapp": true, "web": true, "winbox": true, "write": true}'); INSERT INTO public.user_group_perm_rel(group_id, user_id, perm_id) VALUES ( 1, '37cc36e0-afec-4545-9219-94655805868b', 1); EOF1 -# Run the Python script -python3 /app/initpy.py + # Run the Python script + python3 /app/initpy.py -# Check if the Python script ran successfully -if [ $? -ne 0 ]; then - echo "An error occurred while executing the SQL commands." -else - touch $CONTAINER_ALREADY_STARTED - echo "SQL commands executed successfully." -fi - cron - uwsgi --ini /app/conf/uwsgi.ini:uwsgi-production --touch-reload=/app/reload + # Check if the Python script ran successfully + if [ $? -ne 0 ]; then + echo "An error occurred while executing the SQL commands." + else + touch $CONTAINER_ALREADY_STARTED + echo "SQL commands executed successfully." + fi + cron + uwsgi --ini /app/conf/uwsgi.ini:uwsgi-production --touch-reload=/app/reload else cron uwsgi --ini /app/conf/uwsgi.ini:uwsgi-production --touch-reload=/app/reload diff --git a/mikroman/testdb.py b/mikroman/testdb.py new file mode 100644 index 0000000..8a7aaa4 --- /dev/null +++ b/mikroman/testdb.py @@ -0,0 +1,38 @@ +import psycopg2 +import time +import os +import json + +# Step 1: Read connection details from server.json + +def wait_for_postgres(): + try: + with open('/conf/server-conf.json') as f: + config = json.load(f) + db_name = config['PYSRV_DATABASE_NAME'] + db_user = config['PYSRV_DATABASE_USER'] + db_password = config['PYSRV_DATABASE_PASSWORD'] + db_host = config['PYSRV_DATABASE_HOST_POSTGRESQL'] + db_port = config['PYSRV_DATABASE_PORT'] + + except Exception as e: + print(f"An error occurred: {e}") + exit(1) + print(f"Waiting for PostgreSQL database {db_name} to become available...") + while True: + try: + conn = psycopg2.connect( + dbname=db_name, + user=db_user, + password=db_password, + host=db_host, + port=db_port + ) + conn.close() + print("PostgreSQL is ready!") + break + except psycopg2.OperationalError as e: + print(f"Database not ready yet. Retrying in 5 seconds...\nError: {e}") + time.sleep(5) + +wait_for_postgres()