[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.
This commit is contained in:
Christophe Monniez 2019-10-23 15:35:01 +02:00
parent 19c6725881
commit ffe62bdd52
9 changed files with 108 additions and 12 deletions

View File

@ -13,7 +13,6 @@ RUN set -x; \
&& apt-get install -y --no-install-recommends \ && apt-get install -y --no-install-recommends \
ca-certificates \ ca-certificates \
curl \ curl \
wait-for-it \
dirmngr \ dirmngr \
fonts-noto-cjk \ fonts-noto-cjk \
gnupg \ gnupg \
@ -76,6 +75,8 @@ EXPOSE 8069 8071
# Set the default config file # Set the default config file
ENV ODOO_RC /etc/odoo/odoo.conf 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 # Set default user when running the container
USER odoo USER odoo

View File

@ -23,19 +23,18 @@ check_config "db_port" "$PORT"
check_config "db_user" "$USER" check_config "db_user" "$USER"
check_config "db_password" "$PASSWORD" check_config "db_password" "$PASSWORD"
# Wait for the database to be up
wait-for-it $HOST:$PORT --timeout=60 -- sleep 5s
case "$1" in case "$1" in
-- | odoo) -- | odoo)
shift shift
if [[ "$1" == "scaffold" ]] ; then if [[ "$1" == "scaffold" ]] ; then
exec odoo "$@" exec odoo "$@"
else else
wait-for-psql.py ${DB_ARGS[@]} --timeout=30
exec odoo "$@" "${DB_ARGS[@]}" exec odoo "$@" "${DB_ARGS[@]}"
fi fi
;; ;;
-*) -*)
wait-for-psql.py ${DB_ARGS[@]} --timeout=30
exec odoo "$@" "${DB_ARGS[@]}" exec odoo "$@" "${DB_ARGS[@]}"
;; ;;
*) *)

32
11.0/wait-for-psql.py Executable file
View File

@ -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)

View File

@ -13,7 +13,6 @@ RUN set -x; \
&& apt-get install -y --no-install-recommends \ && apt-get install -y --no-install-recommends \
ca-certificates \ ca-certificates \
curl \ curl \
wait-for-it \
dirmngr \ dirmngr \
fonts-noto-cjk \ fonts-noto-cjk \
gnupg \ gnupg \
@ -90,6 +89,8 @@ EXPOSE 8069 8071
# Set the default config file # Set the default config file
ENV ODOO_RC /etc/odoo/odoo.conf 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 # Set default user when running the container
USER odoo USER odoo

View File

@ -23,19 +23,18 @@ check_config "db_port" "$PORT"
check_config "db_user" "$USER" check_config "db_user" "$USER"
check_config "db_password" "$PASSWORD" check_config "db_password" "$PASSWORD"
# Wait for the database to be up
wait-for-it $HOST:$PORT --timeout=60 -- sleep 5s
case "$1" in case "$1" in
-- | odoo) -- | odoo)
shift shift
if [[ "$1" == "scaffold" ]] ; then if [[ "$1" == "scaffold" ]] ; then
exec odoo "$@" exec odoo "$@"
else else
wait-for-psql.py ${DB_ARGS[@]} --timeout=30
exec odoo "$@" "${DB_ARGS[@]}" exec odoo "$@" "${DB_ARGS[@]}"
fi fi
;; ;;
-*) -*)
wait-for-psql.py ${DB_ARGS[@]} --timeout=30
exec odoo "$@" "${DB_ARGS[@]}" exec odoo "$@" "${DB_ARGS[@]}"
;; ;;
*) *)

32
12.0/wait-for-psql.py Executable file
View File

@ -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)

View File

@ -10,7 +10,6 @@ RUN set -x; \
&& apt-get install -y --no-install-recommends \ && apt-get install -y --no-install-recommends \
ca-certificates \ ca-certificates \
curl \ curl \
wait-for-it \
dirmngr \ dirmngr \
fonts-noto-cjk \ fonts-noto-cjk \
gnupg \ gnupg \
@ -78,6 +77,8 @@ EXPOSE 8069 8071
# Set the default config file # Set the default config file
ENV ODOO_RC /etc/odoo/odoo.conf 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 # Set default user when running the container
USER odoo USER odoo

View File

@ -23,19 +23,18 @@ check_config "db_port" "$PORT"
check_config "db_user" "$USER" check_config "db_user" "$USER"
check_config "db_password" "$PASSWORD" check_config "db_password" "$PASSWORD"
# Wait for the database to be up
wait-for-it $HOST:$PORT --timeout=60 -- sleep 5s
case "$1" in case "$1" in
-- | odoo) -- | odoo)
shift shift
if [[ "$1" == "scaffold" ]] ; then if [[ "$1" == "scaffold" ]] ; then
exec odoo "$@" exec odoo "$@"
else else
wait-for-psql.py ${DB_ARGS[@]} --timeout=30
exec odoo "$@" "${DB_ARGS[@]}" exec odoo "$@" "${DB_ARGS[@]}"
fi fi
;; ;;
-*) -*)
wait-for-psql.py ${DB_ARGS[@]} --timeout=30
exec odoo "$@" "${DB_ARGS[@]}" exec odoo "$@" "${DB_ARGS[@]}"
;; ;;
*) *)

32
13.0/wait-for-psql.py Executable file
View File

@ -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)