Skip to content

Commit

Permalink
replacing django entrypoint to check whether the database exists
Browse files Browse the repository at this point in the history
  • Loading branch information
cmatKhan committed Feb 28, 2024
1 parent a5aca23 commit 400b3a4
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 678 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude: '^docs/|/migrations/|devcontainer.json|/aws_cloudform.json|^rds_redis_ec2_config.yml'
exclude: '^docs/|/migrations/|devcontainer.json|/aws_cloudform.json|^rds_*.yml'
default_stages: [commit]

default_language_version:
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ cd yeastregulatorydb
celery -A config.celery_app worker -B -l info
```

## Github CI

To run the CI that is in this repo, you need to transform the `.envs` directory
into a binary string and save it as a secret. This can be done like so:

```bash
tar -czvf envs.tar.gz .envs
base64 envs.tar.gz > .tmp.txt
```

Then, go to the settings of the repo, and add a secret called `ENV_FILE` with
the string in .tmp.txt. Make sure that and the .tar.gz does not get pushed to
git.

## Deployment

The following details how to deploy this application.
Expand Down
56 changes: 33 additions & 23 deletions compose/production/django/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ set -o errexit
set -o pipefail
set -o nounset



# N.B. If only .env files supported variable expansion...
export CELERY_BROKER_URL="redis://${REDIS_HOST}:${REDIS_PORT}/0"


if [ -z "${POSTGRES_USER}" ]; then
base_postgres_image_default_user='postgres'
export POSTGRES_USER="${base_postgres_image_default_user}"
Expand All @@ -22,26 +19,39 @@ import time
import psycopg
suggest_unrecoverable_after = 30
start = time.time()
while True:
try:
psycopg.connect(
dbname="${POSTGRES_DB}",
user="${POSTGRES_USER}",
password="${POSTGRES_PASSWORD}",
host="${POSTGRES_HOST}",
port="${POSTGRES_PORT}",
)
break
except psycopg.OperationalError as error:
sys.stderr.write("Waiting for PostgreSQL to become available...\n")
if time.time() - start > suggest_unrecoverable_after:
sys.stderr.write(" This is taking longer than expected. The following exception may be indicative of an unrecoverable error: '{}'\n".format(error))
time.sleep(1)
def database_exists(conn_params, dbname):
with psycopg.connect(**conn_params) as conn:
with conn.cursor() as cur:
cur.execute("SELECT 1 FROM pg_database WHERE datname = %s", (dbname,))
return cur.fetchone() is not None
def create_database(conn_params, dbname):
with psycopg.connect(**conn_params) as conn:
conn.autocommit = True
with conn.cursor() as cur:
cur.execute(f"CREATE DATABASE \"{dbname}\"")
conn_params = {
"dbname": "postgres", # connect to the default database to check/create
"user": "${POSTGRES_USER}",
"password": "${POSTGRES_PASSWORD}",
"host": "${POSTGRES_HOST}",
"port": "${POSTGRES_PORT}"
}
dbname = "${POSTGRES_DB}"
if not database_exists(conn_params, dbname):
print("Database does not exist. Creating database: {}".format(dbname))
create_database(conn_params, dbname)
else:
print("Database {} already exists.".format(dbname))
# Now connect to the target database
conn_params["dbname"] = dbname
with psycopg.connect(**conn_params) as conn:
print('Connected to the database successfully')
END

>&2 echo 'PostgreSQL is available'
Expand Down
59 changes: 0 additions & 59 deletions compose/production/django/entrypoint_modified

This file was deleted.

49 changes: 49 additions & 0 deletions compose/production/django/entrypoint_orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset



# N.B. If only .env files supported variable expansion...
export CELERY_BROKER_URL="redis://${REDIS_HOST}:${REDIS_PORT}/0"


if [ -z "${POSTGRES_USER}" ]; then
base_postgres_image_default_user='postgres'
export POSTGRES_USER="${base_postgres_image_default_user}"
fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"

python << END
import sys
import time
import psycopg
suggest_unrecoverable_after = 30
start = time.time()
while True:
try:
psycopg.connect(
dbname="${POSTGRES_DB}",
user="${POSTGRES_USER}",
password="${POSTGRES_PASSWORD}",
host="${POSTGRES_HOST}",
port="${POSTGRES_PORT}",
)
break
except psycopg.OperationalError as error:
sys.stderr.write("Waiting for PostgreSQL to become available...\n")
if time.time() - start > suggest_unrecoverable_after:
sys.stderr.write(" This is taking longer than expected. The following exception may be indicative of an unrecoverable error: '{}'\n".format(error))
time.sleep(1)
END

>&2 echo 'PostgreSQL is available'

exec "$@"
Loading

0 comments on commit 400b3a4

Please sign in to comment.