Skip to content

Commit

Permalink
Use wait-for-it script instead of postgresql-client and mysql-client
Browse files Browse the repository at this point in the history
  • Loading branch information
am97 committed Oct 11, 2023
1 parent 02b8ed2 commit 98c2107
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 122 deletions.
3 changes: 3 additions & 0 deletions core/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ RUN --mount=type=cache,target=/root/.cache \

#Add startup script to container
COPY docker-entrypoint.sh /usr/local/bin/
# Add wait-for-it script (MIT license)
RUN wget -O /usr/local/bin/wait-for-it https://raw.githubusercontent.com/vishnubob/wait-for-it/81b1373f17855a4dc21156cfe1694c31d7d1792e/wait-for-it.sh \
&& chmod +x /usr/local/bin/wait-for-it

# Change the working directory.
WORKDIR /opt/mailman
Expand Down
48 changes: 12 additions & 36 deletions core/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
#! /bin/bash
set -e

function wait_for_postgres () {
# Check if the postgres database is up and accepting connections before
# moving forward.
# TODO: Use python3's psycopg2 module to do this in python3 instead of
# installing postgres-client in the image.
until psql -P pager=off $DATABASE_URL -c '\l'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - continuing"
}

function wait_for_mysql () {
# Check if MySQL is up and accepting connections.
readarray -d' ' -t ENDPOINT <<< $(python3 -c "from urllib.parse import urlparse; o = urlparse('$DATABASE_URL'); print('%s %s' % (o.hostname, o.port if o.port else '3306'));")
until mysqladmin ping --host ${ENDPOINT[0]} --port ${ENDPOINT[1]} --silent; do
>&2 echo "MySQL is unavailable - sleeping"
sleep 1
done
>&2 echo "MySQL is up - continuing"
function wait_for_db () {
# Check if the database is up and accepting connections before moving forward.
echo "Waiting for $DATABASE_TYPE"
declare -A DEFAULT_PORT=( ["postgres"]="5432" ["mysql"]="3306")
python3 -c "from urllib.parse import urlparse; o = urlparse('$DATABASE_URL'); print('%s %s' % (o.hostname, o.port if o.port else '${DEFAULT_PORT[$DATABASE_TYPE]}'));" | {
read HOST PORT
wait-for-it --timeout=60 "$HOST:$PORT" || exit 1
}
echo "$DATABASE_TYPE is up - continuing"
}

# Empty the config file.
Expand Down Expand Up @@ -92,27 +82,13 @@ EOF

# Check if $DATABASE_URL is defined, if not, use a standard sqlite database.
#
# If the $DATABASE_URL is defined and is postgres, check if it is available
# yet. Do not start the container before the postgresql boots up.
#
# TODO: If the $DATABASE_URL is defined and is mysql, check if the database is
# available before the container boots up.
#
# TODO: Check the database type and detect if it is up based on that. For now,
# assume that postgres is being used if DATABASE_URL is defined.
# If the $DATABASE_URL is defined, check if the database is available before the
# container boots up.
if [[ ! -v DATABASE_URL ]]; then
echo "DATABASE_URL is not defined. Using sqlite database..."
else
setup_database
fi


if [[ "$DATABASE_TYPE" = 'postgres' ]]
then
wait_for_postgres
elif [[ "$DATABASE_TYPE" = 'mysql' ]]
then
wait_for_mysql
wait_for_db
fi

# Generate a basic mailman.cfg.
Expand Down
8 changes: 5 additions & 3 deletions postorius/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN --mount=type=cache,target=/root/.cache \
&& apk add --no-cache --virtual .build-deps gcc libc-dev linux-headers \
postgresql-dev mariadb-dev mariadb-connector-c python3-dev libffi-dev openldap-dev cargo rust \
&& apk add --no-cache --virtual .mailman-rundeps bash sassc tzdata \
postgresql-client mysql-client py3-mysqlclient curl mailcap gettext \
libpq py3-mysqlclient curl mailcap gettext \
python3 py3-pip libffi libuuid pcre-dev py-cryptography \
&& python3 -m pip install -U 'Django<4.2' pip setuptools wheel \
&& python3 -m pip install postorius==1.3.7 \
Expand All @@ -28,8 +28,10 @@ RUN --mount=type=cache,target=/root/.cache \
COPY mailman-web /opt/mailman-web
# Add startup script to container
COPY docker-entrypoint.sh /usr/local/bin/

RUN chown -R mailman /opt/mailman-web/ \
# Add wait-for-it script (MIT license)
RUN wget -O /usr/local/bin/wait-for-it https://raw.githubusercontent.com/vishnubob/wait-for-it/81b1373f17855a4dc21156cfe1694c31d7d1792e/wait-for-it.sh \
&& chmod +x /usr/local/bin/wait-for-it \
&& chown -R mailman /opt/mailman-web/ \
&& chmod u+x /opt/mailman-web/manage.py

WORKDIR /opt/mailman-web
Expand Down
8 changes: 5 additions & 3 deletions postorius/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN --mount=type=cache,target=/root/.cache \
&& apk add --no-cache --virtual .build-deps gcc libc-dev linux-headers \
postgresql-dev mariadb-dev mariadb-connector-c python3-dev libffi-dev git cargo rust \
&& apk add --no-cache --virtual .mailman-rundeps bash sassc tzdata \
postgresql-client mysql-client py3-mysqlclient curl mailcap \
libpq py3-mysqlclient curl mailcap \
python3 py3-pip libffi gettext py-cryptography \
&& python3 -m pip install -U pip setuptools wheel \
&& python3 -m pip install -U \
Expand All @@ -36,8 +36,10 @@ RUN --mount=type=cache,target=/root/.cache \
COPY mailman-web /opt/mailman-web
# Add startup script to container
COPY docker-entrypoint.sh /usr/local/bin/

RUN chown -R mailman /opt/mailman-web/ \
# Add wait-for-it script (MIT license)
RUN wget -O /usr/local/bin/wait-for-it https://raw.githubusercontent.com/vishnubob/wait-for-it/81b1373f17855a4dc21156cfe1694c31d7d1792e/wait-for-it.sh \
&& chmod +x /usr/local/bin/wait-for-it \
&& chown -R mailman /opt/mailman-web/ \
&& chmod u+x /opt/mailman-web/manage.py

WORKDIR /opt/mailman-web
Expand Down
50 changes: 13 additions & 37 deletions postorius/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@
set -e


function wait_for_postgres () {
# Check if the postgres database is up and accepting connections before
# moving forward.
# TODO: Use python's psycopg2 module to do this in python instead of
# installing postgres-client in the image.
until psql -P pager=off $DATABASE_URL -c '\l'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - continuing"
}

function wait_for_mysql () {
# Check if MySQL is up and accepting connections.
readarray -d' ' -t ENDPOINT <<< $(python3 -c "from urllib.parse import urlparse; o = urlparse('$DATABASE_URL'); print('%s %s' % (o.hostname, o.port if o.port else '3306'));")
until mysqladmin ping --host ${ENDPOINT[0]} --port ${ENDPOINT[1]} --silent; do
>&2 echo "MySQL is unavailable - sleeping"
sleep 1
done
>&2 echo "MySQL is up - continuing"
function wait_for_db () {
# Check if the database is up and accepting connections before moving forward.
echo "Waiting for $DATABASE_TYPE"
declare -A DEFAULT_PORT=( ["postgres"]="5432" ["mysql"]="3306")
python3 -c "from urllib.parse import urlparse; o = urlparse('$DATABASE_URL'); print('%s %s' % (o.hostname, o.port if o.port else '${DEFAULT_PORT[$DATABASE_TYPE]}'));" | {
read HOST PORT
wait-for-it --timeout=60 "$HOST:$PORT" || exit 1
}
echo "$DATABASE_TYPE is up - continuing"
}

function check_or_create () {
Expand Down Expand Up @@ -57,27 +46,14 @@ fi

# Check if $DATABASE_URL is defined, if not, use a standard sqlite database.
#
# If the $DATABASE_URL is defined and is postgres, check if it is available
# yet. Do not start the container before the postgresql boots up.
#
# If the $DATABASE_URL is defined and is mysql, check if the database is
# available before the container boots up.
#
# TODO: Check the database type and detect if it is up based on that. For now,
# assume that postgres is being used if DATABASE_URL is defined.

# If the $DATABASE_URL is defined, check if the database is available before the
# container boots up.
if [[ ! -v DATABASE_URL ]]; then
echo "DATABASE_URL is not defined. Using sqlite database..."
export DATABASE_URL=sqlite://mailmanweb.db
export DATABASE_TYPE='sqlite'
fi

if [[ "$DATABASE_TYPE" = 'postgres' ]]
then
wait_for_postgres
elif [[ "$DATABASE_TYPE" = 'mysql' ]]
then
wait_for_mysql
else
wait_for_db
fi

# Check if we are in the correct directory before running commands.
Expand Down
8 changes: 5 additions & 3 deletions web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN --mount=type=cache,target=/root/.cache \
&& apk add --no-cache --virtual .build-deps gcc libc-dev linux-headers \
postgresql-dev mariadb-dev mariadb-connector-c python3-dev libffi-dev openldap-dev cargo rust \
&& apk add --no-cache --virtual .mailman-rundeps bash sassc tzdata \
postgresql-client mysql-client py3-mysqlclient curl mailcap gettext \
libpq py3-mysqlclient curl mailcap gettext \
python3 py3-pip xapian-core xapian-bindings-python3 libffi pcre-dev py-cryptography \
&& python3 -m pip install -U 'Django<4.2' pip setuptools wheel \
&& pip install -r /tmp/requirements.txt \
Expand All @@ -36,8 +36,10 @@ RUN --mount=type=cache,target=/root/.cache \
COPY mailman-web /opt/mailman-web
# Add startup script to container
COPY docker-entrypoint.sh /usr/local/bin/

RUN chown -R mailman /opt/mailman-web/ \
# Add wait-for-it script (MIT license)
RUN wget -O /usr/local/bin/wait-for-it https://raw.githubusercontent.com/vishnubob/wait-for-it/81b1373f17855a4dc21156cfe1694c31d7d1792e/wait-for-it.sh \
&& chmod +x /usr/local/bin/wait-for-it \
&& chown -R mailman /opt/mailman-web/ \
&& chmod u+x /opt/mailman-web/manage.py

WORKDIR /opt/mailman-web
Expand Down
8 changes: 5 additions & 3 deletions web/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN --mount=type=cache,target=/root/.cache \
&& apk add --no-cache --virtual .build-deps gcc libc-dev linux-headers git \
postgresql-dev mariadb-dev mariadb-connector-c python3-dev libffi-dev openldap-dev cargo rust \
&& apk add --no-cache --virtual .mailman-rundeps bash sassc pcre-dev tzdata \
python3 py3-pip postgresql-client mysql-client py3-mysqlclient \
python3 py3-pip libpq py3-mysqlclient \
curl mailcap xapian-core xapian-bindings-python3 libffi gettext py-cryptography \
&& python3 -m pip install -U pip setuptools wheel \
&& python3 -m pip install -U \
Expand Down Expand Up @@ -43,8 +43,10 @@ RUN --mount=type=cache,target=/root/.cache \
COPY mailman-web /opt/mailman-web
# Add startup script to container
COPY docker-entrypoint.sh /usr/local/bin/

RUN chown -R mailman /opt/mailman-web/ \
# Add wait-for-it script (MIT license)
RUN wget -O /usr/local/bin/wait-for-it https://raw.githubusercontent.com/vishnubob/wait-for-it/81b1373f17855a4dc21156cfe1694c31d7d1792e/wait-for-it.sh \
&& chmod +x /usr/local/bin/wait-for-it \
&& chown -R mailman /opt/mailman-web/ \
&& chmod u+x /opt/mailman-web/manage.py

WORKDIR /opt/mailman-web
Expand Down
50 changes: 13 additions & 37 deletions web/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@
set -e


function wait_for_postgres () {
# Check if the postgres database is up and accepting connections before
# moving forward.
# TODO: Use python's psycopg2 module to do this in python instead of
# installing postgres-client in the image.
until psql -P pager=off $DATABASE_URL -c '\l'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - continuing"
}

function wait_for_mysql () {
# Check if MySQL is up and accepting connections.
readarray -d' ' -t ENDPOINT <<< $(python3 -c "from urllib.parse import urlparse; o = urlparse('$DATABASE_URL'); print('%s %s' % (o.hostname, o.port if o.port else '3306'));")
until mysqladmin ping --host ${ENDPOINT[0]} --port ${ENDPOINT[1]} --silent; do
>&2 echo "MySQL is unavailable - sleeping"
sleep 1
done
>&2 echo "MySQL is up - continuing"
function wait_for_db () {
# Check if the database is up and accepting connections before moving forward.
echo "Waiting for $DATABASE_TYPE"
declare -A DEFAULT_PORT=( ["postgres"]="5432" ["mysql"]="3306")
python3 -c "from urllib.parse import urlparse; o = urlparse('$DATABASE_URL'); print('%s %s' % (o.hostname, o.port if o.port else '${DEFAULT_PORT[$DATABASE_TYPE]}'));" | {
read HOST PORT
wait-for-it --timeout=60 "$HOST:$PORT" || exit 1
}
echo "$DATABASE_TYPE is up - continuing"
}

function check_or_create () {
Expand Down Expand Up @@ -57,27 +46,14 @@ fi

# Check if $DATABASE_URL is defined, if not, use a standard sqlite database.
#
# If the $DATABASE_URL is defined and is postgres, check if it is available
# yet. Do not start the container before the postgresql boots up.
#
# If the $DATABASE_URL is defined and is mysql, check if the database is
# available before the container boots up.
#
# TODO: Check the database type and detect if it is up based on that. For now,
# assume that postgres is being used if DATABASE_URL is defined.

# If the $DATABASE_URL is defined, check if the database is available before the
# container boots up.
if [[ ! -v DATABASE_URL ]]; then
echo "DATABASE_URL is not defined. Using sqlite database..."
export DATABASE_URL=sqlite://mailmanweb.db
export DATABASE_TYPE='sqlite'
fi

if [[ "$DATABASE_TYPE" = 'postgres' ]]
then
wait_for_postgres
elif [[ "$DATABASE_TYPE" = 'mysql' ]]
then
wait_for_mysql
else
wait_for_db
fi

# Check if we are in the correct directory before running commands.
Expand Down

0 comments on commit 98c2107

Please sign in to comment.