-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdhis2-core-start.sh
More file actions
executable file
·188 lines (159 loc) · 4.6 KB
/
dhis2-core-start.sh
File metadata and controls
executable file
·188 lines (159 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash -x
set -e -u -o pipefail
#
# Tasks:
#
# - Run seed SQL files.
# - Run pre-tomcat shell scripts.
# - Start Tomcat Catalina.
# - Run post-tomcat shell scripts
#
# Global: LOAD_FROM_DATA="yes" | "no"
# Global: DEPLOY_PATH=string
# Global: DHIS2_AUTH=string
export PGPASSWORD="dhis"
dhis2_url="http://localhost:8080/$DEPLOY_PATH"
dhis2_url_with_auth="http://$DHIS2_AUTH@localhost:8080/$DEPLOY_PATH"
psql_base_cmd="psql --quiet -h db -U dhis dhis2"
psql_cmd="$psql_base_cmd -v ON_ERROR_STOP=0"
psql_strict_cmd="$psql_base_cmd -v ON_ERROR_STOP=1"
pgrestore_cmd="pg_restore -h db -U dhis -d dhis2"
configdir="/config"
homedir="/dhis2-home-files"
scripts_dir="/data/scripts"
root_db_path="/data/db"
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"
debug() {
echo "[dhis2-core-start] $*" >&2
}
run_sql_files() {
base_db_path=$(test "${LOAD_FROM_DATA}" = "yes" && echo "$root_db_path" || echo "$post_db_path")
debug "Files in data path"
find "$base_db_path" >&2
find "$base_db_path" -type f \( -name '*.dump' \) |
sort | while read -r path; do
echo "Load SQL dump: $path"
$pgrestore_cmd "$path" || true
done
find "$base_db_path" -type f \( -name '*.sql.gz' \) |
sort | while read -r path; do
echo "Load SQL (compressed): $path"
zcat "$path" | $psql_cmd || true
done
find "$base_db_path" -type f \( -name '*.sql' \) |
sort | while read -r path; do
echo "Load SQL: $path"
run_psql_cmd "$path"
exit_code=$?
if [ "$exit_code" -gt 0 ]; then
echo "Exit code: $exit_code"
exit "$exit_code"
fi
done
}
run_psql_cmd() {
local path=$1
if [[ "$path" == *strict* ]]; then
echo "Strict mode: $path"
$psql_strict_cmd < "$path"
else
echo "Normal mode: $path"
$psql_cmd < "$path"
fi
}
run_pre_scripts() {
find "$scripts_dir" -type f -name '*.sh' ! \( -name 'post*' \) | sort | while read -r path; do
debug "Run pre-tomcat script: $path"
(cd "$(dirname "$path")" && bash -x "$path")
done
}
run_post_scripts() {
find "$scripts_dir" -type f -name '*.sh' -name 'post*' | sort | while read -r path; do
debug "Run post-tomcat script: $path"
(cd "$(dirname "$path")" && bash -x "$path" "$dhis2_url_with_auth")
done
}
copy_apps() {
debug "Copy Dhis2 apps: $source_apps_path -> $files_path"
mkdir -p "$files_path/apps"
if test -e "$source_apps_path"; then
cp -Rv "$source_apps_path" "$files_path"
fi
}
copy_documents() {
debug "Copy Dhis2 documents: $source_documents_path -> $files_path"
mkdir -p "$files_path/document"
if test -e "$source_documents_path"; then
cp -Rv "$source_documents_path" "$files_path"
fi
}
copy_datavalues() {
debug "Copy Dhis2 dataValues: $source_datavalues_path -> $files_path"
mkdir -p "$files_path/dataValue"
if test -e "$source_datavalues_path"; then
cp -Rv "$source_datavalues_path" "$files_path"
fi
}
copy_non_empty_files() {
local from=$1 to=$2
find "$from" -maxdepth 1 -type f -size +0 -exec cp -v {} "$to" \;
}
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/server.xml" "$tomcat_conf_dir/server.xml"
copy_non_empty_files "$configdir/override/tomcat/" "$tomcat_conf_dir/"
}
wait_for_postgres() {
debug "Waiting for postgres: ${host}:${psql_port}"
while ! echo "select 1;" | $psql_cmd; do
sleep 1
done
}
start_tomcat() {
debug "Start Tomcat catalina"
catalina.sh run
}
wait_for_tomcat() {
debug "Waiting for Tomcat to start: $dhis2_url"
while ! curl -sS -i "$dhis2_url" 2>/dev/null | grep "^Location"; do
sleep 1
done
}
INIT_DONE_FILE="/tmp/dhis2-core-start.done"
is_init_done() {
test -e "$INIT_DONE_FILE"
}
init_done() {
touch "$INIT_DONE_FILE"
}
run() {
local host=$1 psql_port=$2
setup_tomcat
copy_apps
copy_documents
copy_datavalues
if is_init_done; then
debug "Container: already configured. Skip DB load"
else
debug "Container: clean. Load DB"
wait_for_postgres
run_sql_files
run_pre_scripts || true
init_done
fi
start_tomcat &
wait_for_tomcat
run_post_scripts || true
debug "DHIS2 instance ready"
wait
}
env
run "db" "5432"