From c7157c1f1defae22774a98ad50025b9bfab24de7 Mon Sep 17 00:00:00 2001 From: chase mateusiak Date: Wed, 28 Feb 2024 11:18:04 -0600 Subject: [PATCH] reverting back to orig entrypoint script --- compose/production/django/entrypoint | 56 +++++++++------------ compose/production/django/entrypoint_new | 59 +++++++++++++++++++++++ compose/production/django/entrypoint_orig | 49 ------------------- 3 files changed, 82 insertions(+), 82 deletions(-) create mode 100644 compose/production/django/entrypoint_new delete mode 100644 compose/production/django/entrypoint_orig diff --git a/compose/production/django/entrypoint b/compose/production/django/entrypoint index 08222b1..f15d0b5 100644 --- a/compose/production/django/entrypoint +++ b/compose/production/django/entrypoint @@ -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}" @@ -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' diff --git a/compose/production/django/entrypoint_new b/compose/production/django/entrypoint_new new file mode 100644 index 0000000..08222b1 --- /dev/null +++ b/compose/production/django/entrypoint_new @@ -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 "$@" diff --git a/compose/production/django/entrypoint_orig b/compose/production/django/entrypoint_orig deleted file mode 100644 index f15d0b5..0000000 --- a/compose/production/django/entrypoint_orig +++ /dev/null @@ -1,49 +0,0 @@ -#!/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 "$@"