From ffe62bdd52f88ac85f54064e6eb02aa38f88f58d Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Wed, 23 Oct 2019 15:35:01 +0200 Subject: [PATCH] [FIX] Odoo 11.0-12.0: replace wait-for-it script As the wait-for-it script is started before the parsing of the command line, it is always started. It can be a problem if a user wants to start some other binary than odoo (for example a shell). Also, the wait-for-it script is only checking if a port is open on a host and then waits 5 seconds before starting Odoo. With this commit, the wait-for-it script is replaced by a more useful custom python script that checks if the postgresql server is able to handle a connection with the given parameters. Odoo then starts whenever it's ready or fails after a 30 sec timeout. --- 11.0/Dockerfile | 3 ++- 11.0/entrypoint.sh | 5 ++--- 11.0/wait-for-psql.py | 32 ++++++++++++++++++++++++++++++++ 12.0/Dockerfile | 3 ++- 12.0/entrypoint.sh | 5 ++--- 12.0/wait-for-psql.py | 32 ++++++++++++++++++++++++++++++++ 13.0/Dockerfile | 3 ++- 13.0/entrypoint.sh | 5 ++--- 13.0/wait-for-psql.py | 32 ++++++++++++++++++++++++++++++++ 9 files changed, 108 insertions(+), 12 deletions(-) create mode 100755 11.0/wait-for-psql.py create mode 100755 12.0/wait-for-psql.py create mode 100755 13.0/wait-for-psql.py diff --git a/11.0/Dockerfile b/11.0/Dockerfile index 361c76a..6f84e46 100644 --- a/11.0/Dockerfile +++ b/11.0/Dockerfile @@ -13,7 +13,6 @@ RUN set -x; \ && apt-get install -y --no-install-recommends \ ca-certificates \ curl \ - wait-for-it \ dirmngr \ fonts-noto-cjk \ gnupg \ @@ -76,6 +75,8 @@ EXPOSE 8069 8071 # Set the default config file ENV ODOO_RC /etc/odoo/odoo.conf +COPY wait-for-psql.py /usr/local/bin/wait-for-psql.py + # Set default user when running the container USER odoo diff --git a/11.0/entrypoint.sh b/11.0/entrypoint.sh index b6401f7..5638e6d 100755 --- a/11.0/entrypoint.sh +++ b/11.0/entrypoint.sh @@ -23,19 +23,18 @@ check_config "db_port" "$PORT" check_config "db_user" "$USER" check_config "db_password" "$PASSWORD" -# Wait for the database to be up -wait-for-it $HOST:$PORT --timeout=60 -- sleep 5s - case "$1" in -- | odoo) shift if [[ "$1" == "scaffold" ]] ; then exec odoo "$@" else + wait-for-psql.py ${DB_ARGS[@]} --timeout=30 exec odoo "$@" "${DB_ARGS[@]}" fi ;; -*) + wait-for-psql.py ${DB_ARGS[@]} --timeout=30 exec odoo "$@" "${DB_ARGS[@]}" ;; *) diff --git a/11.0/wait-for-psql.py b/11.0/wait-for-psql.py new file mode 100755 index 0000000..a55f440 --- /dev/null +++ b/11.0/wait-for-psql.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +import argparse +import psycopg2 +import sys +import time + + +if __name__ == '__main__': + arg_parser = argparse.ArgumentParser() + 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_password', required=True) + arg_parser.add_argument('--timeout', type=int, default=5) + + args = arg_parser.parse_args() + + start_time = time.time() + 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 = '' + break + except psycopg2.OperationalError as e: + error = e + else: + conn.close() + time.sleep(1) + + if error: + print("Database connection failure: %s" % error, file=sys.stderr) + sys.exit(1) diff --git a/12.0/Dockerfile b/12.0/Dockerfile index cfa69a6..a93334a 100644 --- a/12.0/Dockerfile +++ b/12.0/Dockerfile @@ -13,7 +13,6 @@ RUN set -x; \ && apt-get install -y --no-install-recommends \ ca-certificates \ curl \ - wait-for-it \ dirmngr \ fonts-noto-cjk \ gnupg \ @@ -90,6 +89,8 @@ EXPOSE 8069 8071 # Set the default config file ENV ODOO_RC /etc/odoo/odoo.conf +COPY wait-for-psql.py /usr/local/bin/wait-for-psql.py + # Set default user when running the container USER odoo diff --git a/12.0/entrypoint.sh b/12.0/entrypoint.sh index b6401f7..5638e6d 100755 --- a/12.0/entrypoint.sh +++ b/12.0/entrypoint.sh @@ -23,19 +23,18 @@ check_config "db_port" "$PORT" check_config "db_user" "$USER" check_config "db_password" "$PASSWORD" -# Wait for the database to be up -wait-for-it $HOST:$PORT --timeout=60 -- sleep 5s - case "$1" in -- | odoo) shift if [[ "$1" == "scaffold" ]] ; then exec odoo "$@" else + wait-for-psql.py ${DB_ARGS[@]} --timeout=30 exec odoo "$@" "${DB_ARGS[@]}" fi ;; -*) + wait-for-psql.py ${DB_ARGS[@]} --timeout=30 exec odoo "$@" "${DB_ARGS[@]}" ;; *) diff --git a/12.0/wait-for-psql.py b/12.0/wait-for-psql.py new file mode 100755 index 0000000..a55f440 --- /dev/null +++ b/12.0/wait-for-psql.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +import argparse +import psycopg2 +import sys +import time + + +if __name__ == '__main__': + arg_parser = argparse.ArgumentParser() + 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_password', required=True) + arg_parser.add_argument('--timeout', type=int, default=5) + + args = arg_parser.parse_args() + + start_time = time.time() + 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 = '' + break + except psycopg2.OperationalError as e: + error = e + else: + conn.close() + time.sleep(1) + + if error: + print("Database connection failure: %s" % error, file=sys.stderr) + sys.exit(1) diff --git a/13.0/Dockerfile b/13.0/Dockerfile index 7a1b3ff..d20372e 100644 --- a/13.0/Dockerfile +++ b/13.0/Dockerfile @@ -10,7 +10,6 @@ RUN set -x; \ && apt-get install -y --no-install-recommends \ ca-certificates \ curl \ - wait-for-it \ dirmngr \ fonts-noto-cjk \ gnupg \ @@ -78,6 +77,8 @@ EXPOSE 8069 8071 # Set the default config file ENV ODOO_RC /etc/odoo/odoo.conf +COPY wait-for-psql.py /usr/local/bin/wait-for-psql.py + # Set default user when running the container USER odoo diff --git a/13.0/entrypoint.sh b/13.0/entrypoint.sh index b6401f7..5638e6d 100755 --- a/13.0/entrypoint.sh +++ b/13.0/entrypoint.sh @@ -23,19 +23,18 @@ check_config "db_port" "$PORT" check_config "db_user" "$USER" check_config "db_password" "$PASSWORD" -# Wait for the database to be up -wait-for-it $HOST:$PORT --timeout=60 -- sleep 5s - case "$1" in -- | odoo) shift if [[ "$1" == "scaffold" ]] ; then exec odoo "$@" else + wait-for-psql.py ${DB_ARGS[@]} --timeout=30 exec odoo "$@" "${DB_ARGS[@]}" fi ;; -*) + wait-for-psql.py ${DB_ARGS[@]} --timeout=30 exec odoo "$@" "${DB_ARGS[@]}" ;; *) diff --git a/13.0/wait-for-psql.py b/13.0/wait-for-psql.py new file mode 100755 index 0000000..a55f440 --- /dev/null +++ b/13.0/wait-for-psql.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +import argparse +import psycopg2 +import sys +import time + + +if __name__ == '__main__': + arg_parser = argparse.ArgumentParser() + 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_password', required=True) + arg_parser.add_argument('--timeout', type=int, default=5) + + args = arg_parser.parse_args() + + start_time = time.time() + 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 = '' + break + except psycopg2.OperationalError as e: + error = e + else: + conn.close() + time.sleep(1) + + if error: + print("Database connection failure: %s" % error, file=sys.stderr) + sys.exit(1)