Skip to content

Commit

Permalink
Manage between sqlite3 and postgresql databases.
Browse files Browse the repository at this point in the history
  • Loading branch information
romer8 committed Mar 6, 2024
1 parent d49498f commit c461380
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
45 changes: 27 additions & 18 deletions docker/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ db_max_count=24;
no_daemon=true;
skip_perm=false;
test=false;
db_engine=${TETHYS_DB_ENGINE} # Get the DB engine from environment variable
USAGE="USAGE: . run.sh [options]
OPTIONS:
--background \t run supervisord in background.
Expand Down Expand Up @@ -44,32 +45,40 @@ done

echo_status "Starting up..."

# Create Salt Config
echo "file_client: local" > /etc/salt/minion
echo "postgres.host: '${TETHYS_DB_HOST}'" >> /etc/salt/minion
echo "postgres.port: '${TETHYS_DB_PORT}'" >> /etc/salt/minion
echo "postgres.user: '${TETHYS_DB_USERNAME}'" >> /etc/salt/minion
echo "postgres.pass: '${TETHYS_DB_PASSWORD}'" >> /etc/salt/minion
echo "postgres.bins_dir: '${CONDA_HOME}/envs/${CONDA_ENV_NAME}/bin'" >> /etc/salt/minion

if [[ $test = false ]]; then
# Set extra ENVs
export NGINX_USER=$(grep 'user .*;' /etc/nginx/nginx.conf | awk '{print $2}' | awk -F';' '{print $1}')

# Apply States
echo_status "Checking if DB is ready"
if [[ $db_engine == "django.db.backends.postgresql" ]]; then
# Create Salt Config for PostgreSQL
echo "postgres.host: '${TETHYS_DB_HOST}'" >> /etc/salt/minion
echo "postgres.port: '${TETHYS_DB_PORT}'" >> /etc/salt/minion
echo "postgres.user: '${TETHYS_DB_USERNAME}'" >> /etc/salt/minion
echo "postgres.pass: '${TETHYS_DB_PASSWORD}'" >> /etc/salt/minion
echo "postgres.bins_dir: '${CONDA_HOME}/envs/${CONDA_ENV_NAME}/bin'" >> /etc/salt/minion

db_check_count=0

until ${CONDA_HOME}/envs/${CONDA_ENV_NAME}/bin/pg_isready -h ${TETHYS_DB_HOST} -p ${TETHYS_DB_PORT} -U postgres; do
if [[ $db_check_count -gt $db_max_count ]]; then
>&2 echo "DB was not available in time - exiting"
exit 1
fi
>&2 echo "DB is unavailable - sleeping"
db_check_count=`expr $db_check_count + 1`
sleep 5
done
elif [[ $db_engine == "django.db.backends.sqlite3" ]]; then
echo_status "Using SQLite3 as the database"
# Add any SQLite-specific setup here if necessary
else
echo_status "Unsupported database engine: $db_engine"
fi


db_check_count=0

until ${CONDA_HOME}/envs/${CONDA_ENV_NAME}/bin/pg_isready -h ${TETHYS_DB_HOST} -p ${TETHYS_DB_PORT} -U postgres; do
if [[ $db_check_count -gt $db_max_count ]]; then
>&2 echo "DB was not available in time - exiting"
exit 1
fi
>&2 echo "DB is unavailable - sleeping"
db_check_count=`expr $db_check_count + 1`
sleep 5
done
fi

echo_status "Enforcing start state... (This might take a bit)"
Expand Down
6 changes: 5 additions & 1 deletion docker/salt/tethyscore.sls
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Generate_ASGI_Service_TethysCore:
- replace: False
- makedirs: True

{% if TETHYS_DB_ENGINE == 'django.db.backends.postgresql' %}
Create_Database_User_and_SuperUser_TethysCore:
cmd.run:
- name: >
Expand All @@ -149,7 +150,8 @@ Create_Database_User_and_SuperUser_TethysCore:
-N "{{ TETHYS_DB_SUPERUSER }}"
-P "{{ TETHYS_DB_SUPERUSER_PASS }}"
- shell: /bin/bash
- unless: /bin/bash -c "[ -f "{{ TETHYS_PERSIST }}/setup_complete" ];"
- unless: /bin/bash -c "[ -f '{{ TETHYS_PERSIST }}/setup_complete' ];"
{% endif %}
Migrate_Database_TethysCore:
cmd.run:
Expand All @@ -158,6 +160,7 @@ Migrate_Database_TethysCore:
- shell: /bin/bash
- unless: /bin/bash -c "[ -f "{{ TETHYS_PERSIST }}/setup_complete" ];"
{% if TETHYS_DB_ENGINE == 'django.db.backends.postgresql' %}
Create_Database_Portal_SuperUser_TethysCore:
cmd.run:
- name: >
Expand All @@ -168,6 +171,7 @@ Create_Database_Portal_SuperUser_TethysCore:
{%- if PORTAL_SUPERUSER_EMAIL %}--pe "{{ PORTAL_SUPERUSER_EMAIL }}"{% endif %}
- shell: /bin/bash
- unless: /bin/bash -c "[ -f "{{ TETHYS_PERSIST }}/setup_complete" ];"
{% endif %}
{% if TETHYS_SITE_CONTENT %}
Modify_Tethys_Site_TethysCore:
Expand Down

0 comments on commit c461380

Please sign in to comment.