Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@ venv.bak/
*.war

.flaskenv.secret
bom.json
49 changes: 43 additions & 6 deletions src/d2_docker/config/dhis2-core-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ post_db_path="/data/db/post"
source_apps_path="/data/apps"
source_documents_path="/data/document"
source_datavalues_path="/data/dataValue"
files_path="/DHIS2_home/files/"
tomcat_conf_dir="/usr/local/tomcat/conf"
home_path="/DHIS2_home"
files_path="$home_path/files/"
tomcatdir=/usr/local/tomcat
tomcat_conf_dir="$tomcatdir/conf"
approot="$tomcatdir/webapps/ROOT"
flag_sql_error="$home_path/flag-sql-error"


debug() {
echo "[dhis2-core-start] $*" >&2
Expand All @@ -39,6 +44,10 @@ debug() {
run_sql_files() {
base_db_path=$(test "${LOAD_FROM_DATA}" = "yes" && echo "$root_db_path" || echo "$post_db_path")
debug "Files in data path"
if [[ ! -d "$base_db_path" ]] ; then
debug " -- NO FILES -- "
return 0
fi
find "$base_db_path" >&2

find "$base_db_path" -type f \( -name '*.dump' \) |
Expand All @@ -60,6 +69,12 @@ run_sql_files() {
run_psql_cmd "$path" || exit_code=$?
if [ "$exit_code" -gt 0 ]; then
echo "Exit code: $exit_code"
touch "$flag_sql_error"
rf -rvf $approot
mkdir -p -m 750 $approot
chown tomcat:tomcat $approot
echo '<!DOCTYPE html><title>Error</title>
Error during preparation of the service' > $approot/index.html
exit "$exit_code"
fi
done
Expand Down Expand Up @@ -122,9 +137,9 @@ copy_non_empty_files() {
setup_tomcat() {
debug "Setup tomcat"

cp -v $configdir/DHIS2_home/* "/DHIS2_home/"
cp -v $homedir/* /DHIS2_home/ || true
copy_non_empty_files "$configdir/override/dhis2/" "/DHIS2_home/"
cp -v $configdir/DHIS2_home/* "$home_path/"
cp -v $homedir/* $home_path/ || true
copy_non_empty_files "$configdir/override/dhis2/" "$home_path/"

cp -v "$configdir/server.xml" "$tomcat_conf_dir/server.xml"
copy_non_empty_files "$configdir/override/tomcat/" "$tomcat_conf_dir/"
Expand All @@ -149,6 +164,27 @@ wait_for_tomcat() {
done
}

cleanup() {
debug "--- [SIGNAL RECEIVED] ---"
debug "Stopping tomcat"
catalina.sh stop &
STOP_PID=$!
count=0
# if need more than 10 seconds (default), remember to configure stop_grace_period in docker-compose.yml accordingly
while [ $count -lt 10 ]; do
Comment thread
cgbautista marked this conversation as resolved.
Outdated
if ! kill -0 $STOP_PID 2>/dev/null; then
debug "Tomcat has stopped."
exit 0
fi
sleep 1
count=$((count + 1))
done
exit 0
}

trap cleanup SIGTERM SIGINT


INIT_DONE_FILE="/tmp/dhis2-core-start.done"

is_init_done() {
Expand Down Expand Up @@ -178,10 +214,11 @@ run() {
fi

start_tomcat &
LAST_PID=$!
wait_for_tomcat
run_post_scripts || true
debug "DHIS2 instance ready"
wait
wait $LAST_PID || true
}

env
Expand Down
3 changes: 2 additions & 1 deletion src/d2_docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ services:
DHIS2_AUTH: "${DHIS2_AUTH}"
entrypoint: bash /config/dhis2-core-entrypoint.sh
command: bash /config/dhis2-core-start.sh
restart: "no"
stop_grace_period: 10s
restart: unless-stopped
depends_on:
- "db"
- "data"
Expand Down
12 changes: 10 additions & 2 deletions src/d2_docker/images/dhis2-core/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ WARFILE=/usr/local/tomcat/webapps/ROOT.war
TOMCATDIR=/usr/local/tomcat
DHIS2HOME=/DHIS2_home
DATA_DIR=/data
FLAG_SQL_ERROR=$DHIS2HOME/flag_sql_error
APPROOT=$TOMCATDIR/webapps/ROOT

if [ "$(id -u)" = "0" ]; then
if [ -f $WARFILE ]; then
unzip -q $WARFILE -d $TOMCATDIR/webapps/ROOT
if [ -f $FLAG_SQL_ERROR ]; then
rf -rvf $APPROOT
Comment thread
cgbautista marked this conversation as resolved.
Outdated
mkdir -p -m 750 $APPROOT
chown tomcat:tomcat $APPROOT
echo '<!DOCTYPE html><title>Error</title>
Error during preparation of the service' > $APPROOT/index.html
elif [ -f $WARFILE ]; then
unzip -q $WARFILE -d $APPROOT
rm -v $WARFILE # just to save space
fi

Expand Down
2 changes: 2 additions & 0 deletions src/d2_docker/images/dhis2-data/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ main() { local volume=$1
chmod -R u+rwX,go+rX,go-w $volume
else
rm -rf /$volume/*
# To avoid leaving dhis2-core without a path to /data/db/post after clearing up all /data
mkdir /$volume/db
fi
}

Expand Down
Loading