Skip to content
This repository was archived by the owner on Oct 1, 2025. It is now read-only.

Commit f92c90f

Browse files
Merge branch '5-prepare-user-and-db-for-monitoring' into 'main'
Resolve "Prepare user and db for monitoring" Closes #5 See merge request cm/rnd/cytomine/tools/envs/postgis-ce!6
2 parents 2b64590 + 7360398 commit f92c90f

12 files changed

+98
-41
lines changed

Dockerfile

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,34 @@ ARG IMAGE_VERSION
2929
ARG IMAGE_REVISION
3030
ARG POSTGIS_VERSION
3131

32-
#set default user (and default DB name) to docker by default
33-
ENV POSTGRES_USER=docker
32+
# set default superadmin user postgres + set defaults for component specific databass sdb and user
3433
ENV APPENGINE_DB=appengine
3534
ENV APPENGINE_USER=appengine
36-
ENV APPENGINE_PASSWORD=password
3735

38-
# database init
39-
RUN mkdir -p /etc/postgres/conf.d /docker-entrypoint-cytomine.d/ /docker-entrypoint-initdb.d/
40-
COPY files/initdb-cytomine-extensions.sql /docker-entrypoint-initdb.d/11_cytomine-extensions.sql
41-
COPY files/initdb-cytomine-user-appengine.sh /docker-entrypoint-initdb.d/14_cytomine_user_appengine.sh
36+
# database init. Warning: those are only run if data volume is empty
37+
RUN mkdir -p /etc/postgres/conf.d /docker-entrypoint-cytomine.d/ /docker-entrypoint-initdb.d/ /checks/
38+
COPY files/initdb/initdb-cytomine-extensions.sql /docker-entrypoint-initdb.d/11_cytomine-extensions.sql
4239

4340
# default configuration
44-
COPY files/postgres.conf /etc/postgres/postgres.conf
45-
COPY files/postgres.default.conf /etc/postgres/00-default.conf
46-
COPY files/check-backup-folder.sh /docker-entrypoint-cytomine.d/550-check-backup-folder.sh
47-
COPY files/start-crond.sh /docker-entrypoint-cytomine.d/600-start-crond.sh
41+
COPY files/conf/postgres.conf /etc/postgres/postgres.conf
42+
COPY files/conf/postgres.default.conf /etc/postgres/00-default.conf
43+
44+
# entry points. Triggered at every container starts
45+
COPY files/scripts/check-backup-folder.sh /docker-entrypoint-cytomine.d/550-check-backup-folder.sh
46+
COPY files/scripts/start-crond.sh /docker-entrypoint-cytomine.d/600-start-crond.sh
47+
COPY files/scripts/check_dbs_users.sh /docker-entrypoint-cytomine.d/700-check_dbs_users.sh
48+
COPY files/checks /checks
4849

4950
# backup and restore scripts
50-
COPY files/backup-cron-job /backup-cron-job
51-
COPY files/cytomine-postgis-backup.sh /usr/local/bin/backup
52-
COPY files/cytomine-postgis-restore.sh /usr/local/bin/restore
51+
COPY files/scripts/backup-cron-job /backup-cron-job
52+
COPY files/scripts/cytomine-postgis-backup.sh /usr/local/bin/backup
53+
COPY files/scripts/cytomine-postgis-restore.sh /usr/local/bin/restore
5354

54-
RUN chmod +x /usr/local/bin/backup /usr/local/bin/restore /docker-entrypoint-cytomine.d/*.sh && \
55+
RUN chmod +x /usr/local/bin/backup /usr/local/bin/restore /docker-entrypoint-cytomine.d/*.sh /checks/*.sh && \
5556
chmod 0644 /backup-cron-job && \
5657
chmod u+s /usr/bin/crontab && \
57-
crontab /backup-cron-job
58+
crontab /backup-cron-job && \
59+
chmod 0700 /checks -R
5860

5961
COPY --from=entrypoint-scripts --chmod=774 /cytomine-entrypoint.sh /usr/local/bin/
6062
COPY --from=entrypoint-scripts --chmod=774 /envsubst-on-templates-and-move.sh /docker-entrypoint-cytomine.d/500-envsubst-on-templates-and-move.sh

files/checks/check-user.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
3+
CHECK_NAME="$1"
4+
USER="$2"
5+
PASSWORD="$3"
6+
DB="$4"
7+
ADD_DB_EXTENSIONS="$5"
8+
9+
echo "Starting '${CHECK_NAME}'."
10+
11+
if [ -z "$USER" ] || [ -z "$PASSWORD" ] || [ -z "$DB" ]; then
12+
echo "$0" "Skipping '${CHECK_NAME}' pre-configuration because * env variables are empty or undefined"
13+
exit
14+
fi
15+
16+
echo "$0" "Creating database '$DB' using user '$POSTGRES_USER'. An error is expected if it already exists.";
17+
18+
# DB
19+
psql -U "$POSTGRES_USER" -c "CREATE DATABASE $DB" >/dev/null
20+
21+
if [ "$ADD_DB_EXTENSIONS" -ne "0" ]; then
22+
echo "Loading PostGIS extensions into $DB"
23+
# extracted from postgis initdb script
24+
psql -U "$POSTGRES_USER" --dbname="$DB" <<-'EOSQL'
25+
CREATE EXTENSION IF NOT EXISTS postgis;
26+
CREATE EXTENSION IF NOT EXISTS postgis_topology;
27+
-- Reconnect to update pg_setting.resetval
28+
-- See https://github.com/postgis/docker-postgis/issues/288
29+
\c
30+
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
31+
CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder;
32+
EOSQL
33+
echo "Loading ltree into $DB"
34+
psql -U "$POSTGRES_USER" --dbname="$DB" <<-'EOSQL'
35+
CREATE EXTENSION IF NOT EXISTS ltree;
36+
EOSQL
37+
fi
38+
39+
# Grants
40+
echo "$0" "Grant roles to '$USER' for database '$DB'. A notice is expected if they already exist.";
41+
42+
psql -U "$POSTGRES_USER" >/dev/null <<- EOSQL
43+
DO
44+
\$do\$
45+
BEGIN
46+
IF EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '$USER') THEN
47+
RAISE NOTICE 'Role "$USER" already exists. Skipping.';
48+
ELSE
49+
CREATE ROLE $USER LOGIN PASSWORD '$PASSWORD';
50+
ALTER ROLE $USER SET client_encoding TO 'utf8';
51+
ALTER ROLE $USER SET timezone TO 'UTC';
52+
GRANT ALL PRIVILEGES ON DATABASE $DB TO $USER;
53+
ALTER DATABASE $DB OWNER TO $USER;
54+
RAISE NOTICE 'Database "$DB" and user "$USER" created.';
55+
END IF;
56+
END
57+
\$do\$;
58+
EOSQL
File renamed without changes.
File renamed without changes.

files/initdb-cytomine-user-appengine.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

files/scripts/check_dbs_users.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
echo $0 "Databases check. Wainting for pg_isready -U $POSTGRES_USER"
4+
5+
wait_for_db() {
6+
sleep 2
7+
until pg_isready -U "$POSTGRES_USER"; do
8+
>&2 echo $0 "Postgres is unavailable - sleeping for 3 seconds."
9+
sleep 2
10+
done
11+
12+
echo $0 "Postgres is up - executing command"
13+
14+
sh /checks/check-user.sh "check-cytomine" "$POSTGRES_USER" "$POSTGRES_PASSWORD" "$POSTGRES_DB" "1"
15+
sh /checks/check-user.sh "check-appengine" "$APPENGINE_USER" "$APPENGINE_PASSWORD" "$APPENGINE_DB" "0"
16+
}
17+
18+
# This will be executed in background as we need the database server to be ready.
19+
wait_for_db &
20+
21+
# executing next entrypoint, even if there are running background tasks. It will allow Postgres to start.
22+
exec "$@"
File renamed without changes.

0 commit comments

Comments
 (0)