Skip to content

Commit

Permalink
reverting back to orig entrypoint script
Browse files Browse the repository at this point in the history
  • Loading branch information
cmatKhan committed Feb 28, 2024
1 parent 400b3a4 commit c7157c1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 82 deletions.
56 changes: 23 additions & 33 deletions compose/production/django/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ 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 @@ -19,39 +22,26 @@ import time
import psycopg
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')
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'
Expand Down
59 changes: 59 additions & 0 deletions compose/production/django/entrypoint_new
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/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
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'

exec "$@"
49 changes: 0 additions & 49 deletions compose/production/django/entrypoint_orig

This file was deleted.

0 comments on commit c7157c1

Please sign in to comment.