From 4035fbef62c227738bae2570a9e076f43fecba54 Mon Sep 17 00:00:00 2001 From: Josh Santos Date: Sun, 31 Mar 2024 17:52:18 -0400 Subject: [PATCH] Improvements for psql wait script. * support: Move to shared directory to avoid code drift. * feat: Make DB name configurable --- 15.0/Dockerfile | 2 +- 15.0/wait-for-psql.py | 11 +++++++---- 16.0/Dockerfile | 2 +- 16.0/wait-for-psql.py | 11 +++++++---- 17.0/wait-for-psql.py | 11 +++++++---- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/15.0/Dockerfile b/15.0/Dockerfile index 9c0192757..b0e3b9a59 100644 --- a/15.0/Dockerfile +++ b/15.0/Dockerfile @@ -79,7 +79,7 @@ EXPOSE 8069 8071 8072 # Set the default config file ENV ODOO_RC /etc/odoo/odoo.conf -COPY wait-for-psql.py /usr/local/bin/wait-for-psql.py +COPY ../shared/wait-for-psql.py /usr/local/bin/wait-for-psql.py # Set default user when running the container USER odoo diff --git a/15.0/wait-for-psql.py b/15.0/wait-for-psql.py index a55f44098..d0a66eaf3 100755 --- a/15.0/wait-for-psql.py +++ b/15.0/wait-for-psql.py @@ -10,21 +10,24 @@ arg_parser.add_argument('--db_host', required=True) arg_parser.add_argument('--db_port', required=True) arg_parser.add_argument('--db_user', required=True) + arg_parser.add_argument('--db_name', required=True) arg_parser.add_argument('--db_password', required=True) arg_parser.add_argument('--timeout', type=int, default=5) args = arg_parser.parse_args() + conn = None start_time = time.time() + error = '' + while (time.time() - start_time) < args.timeout: try: - conn = psycopg2.connect(user=args.db_user, host=args.db_host, port=args.db_port, password=args.db_password, dbname='postgres') - error = '' + conn = psycopg2.connect(user=args.db_user, host=args.db_host, port=args.db_port, password=args.db_password, dbname=args.db_name) + conn.close() break except psycopg2.OperationalError as e: error = e - else: - conn.close() + time.sleep(1) if error: diff --git a/16.0/Dockerfile b/16.0/Dockerfile index f39900e7a..7b1f3caf1 100644 --- a/16.0/Dockerfile +++ b/16.0/Dockerfile @@ -93,7 +93,7 @@ EXPOSE 8069 8071 8072 # Set the default config file ENV ODOO_RC /etc/odoo/odoo.conf -COPY wait-for-psql.py /usr/local/bin/wait-for-psql.py +COPY ../shared/wait-for-psql.py /usr/local/bin/wait-for-psql.py # Set default user when running the container USER odoo diff --git a/16.0/wait-for-psql.py b/16.0/wait-for-psql.py index a55f44098..d0a66eaf3 100755 --- a/16.0/wait-for-psql.py +++ b/16.0/wait-for-psql.py @@ -10,21 +10,24 @@ arg_parser.add_argument('--db_host', required=True) arg_parser.add_argument('--db_port', required=True) arg_parser.add_argument('--db_user', required=True) + arg_parser.add_argument('--db_name', required=True) arg_parser.add_argument('--db_password', required=True) arg_parser.add_argument('--timeout', type=int, default=5) args = arg_parser.parse_args() + conn = None start_time = time.time() + error = '' + while (time.time() - start_time) < args.timeout: try: - conn = psycopg2.connect(user=args.db_user, host=args.db_host, port=args.db_port, password=args.db_password, dbname='postgres') - error = '' + conn = psycopg2.connect(user=args.db_user, host=args.db_host, port=args.db_port, password=args.db_password, dbname=args.db_name) + conn.close() break except psycopg2.OperationalError as e: error = e - else: - conn.close() + time.sleep(1) if error: diff --git a/17.0/wait-for-psql.py b/17.0/wait-for-psql.py index a55f44098..d0a66eaf3 100755 --- a/17.0/wait-for-psql.py +++ b/17.0/wait-for-psql.py @@ -10,21 +10,24 @@ arg_parser.add_argument('--db_host', required=True) arg_parser.add_argument('--db_port', required=True) arg_parser.add_argument('--db_user', required=True) + arg_parser.add_argument('--db_name', required=True) arg_parser.add_argument('--db_password', required=True) arg_parser.add_argument('--timeout', type=int, default=5) args = arg_parser.parse_args() + conn = None start_time = time.time() + error = '' + while (time.time() - start_time) < args.timeout: try: - conn = psycopg2.connect(user=args.db_user, host=args.db_host, port=args.db_port, password=args.db_password, dbname='postgres') - error = '' + conn = psycopg2.connect(user=args.db_user, host=args.db_host, port=args.db_port, password=args.db_password, dbname=args.db_name) + conn.close() break except psycopg2.OperationalError as e: error = e - else: - conn.close() + time.sleep(1) if error: