-
Notifications
You must be signed in to change notification settings - Fork 8
/
docker-entrypoint.sh
executable file
·309 lines (244 loc) · 7.66 KB
/
docker-entrypoint.sh
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/bash
# wait for a given host:port to become available
#
# $1 host
# $2 port
function dockerwait {
while ! exec 6<>/dev/tcp/"$1"/"$2"; do
warn "$(date) - waiting to connect $1 $2"
sleep 5
done
success "$(date) - connected to $1 $2"
exec 6>&-
exec 6<&-
}
function info () {
printf "\r [\033[00;34mINFO\033[0m] %s\n" "$1"
}
function warn () {
printf "\r [\033[00;33mWARN\033[0m] %s\n" "$1"
}
function success () {
printf "\r\033[2K [\033[00;32m OK \033[0m] %s\n" "$1"
}
function fail () {
printf "\r\033[2K [\033[0;31mFAIL\033[0m] %s\n" "$1"
echo ''
exit 1
}
# wait for services to become available
# this prevents race conditions using fig
function wait_for_services {
if [[ "$WAIT_FOR_DB" ]] ; then
dockerwait "$DBSERVER" "$DBPORT"
fi
if [[ "$WAIT_FOR_CLINICAL_DB" ]] ; then
dockerwait "$CLINICAL_DBSERVER" "$CLINICAL_DBPORT"
fi
if [[ "$WAIT_FOR_REPORTING_DB" ]] ; then
dockerwait "$REPORTING_DBSERVER" "$REPORTING_DBPORT"
fi
if [[ "$WAIT_FOR_CACHE" ]] ; then
dockerwait "$CACHESERVER" "$CACHEPORT"
fi
if [[ "$WAIT_FOR_REDIS_CACHE" ]] ; then
dockerwait "$REDISCACHESERVER" "$REDISCACHEPORT"
fi
if [[ "$WAIT_FOR_RUNSERVER" ]] ; then
dockerwait "$RUNSERVER" "$RUNSERVERPORT"
fi
if [[ "$WAIT_FOR_HOST_PORT" ]]; then
dockerwait "$DOCKER_ROUTE" "$WAIT_FOR_HOST_PORT"
fi
if [[ "$WAIT_FOR_UWSGI" ]] ; then
dockerwait "$UWSGISERVER" "$UWSGIPORT"
fi
}
function defaults {
: "${DBSERVER:=db}"
: "${DBPORT:=5432}"
: "${DBUSER:=webapp}"
: "${DBNAME:=${DBUSER}}"
: "${DBPASS:=${DBUSER}}"
: "${CLINICAL_DBSERVER:=clinicaldb}"
: "${CLINICAL_DBPORT:=5432}"
: "${CLINICAL_DBUSER:=webapp}"
: "${CLINICAL_DBNAME:=${CLINICAL_DBUSER}}"
: "${CLINICAL_DBPASS:=${CLINICAL_DBUSER}}"
: "${REPORTING_DBSERVER:=reportingdb}"
: "${REPORTING_DBPORT:=5432}"
: "${REPORTING_DBUSER:=webapp}"
: "${REPORTING_DBNAME:=${REPORTING_DBUSER}}"
: "${REPORTING_DBPASS:=${REPORTING_DBUSER}}"
: "${DOCKER_ROUTE:=$(/sbin/ip route|awk '/default/ { print $3 }')}"
: "${UWSGISERVER:=uwsgi}"
: "${UWSGIPORT:=9000}"
: "${UWSGI_OPTS:=/app/uwsgi/docker.ini}"
: "${RUNSERVER:=runserver}"
: "${RUNSERVERPORT:=8000}"
: "${RUNSERVER_CMD:=runserver}"
: "${CACHESERVER:=cache}"
: "${CACHEPORT:=11211}"
: "${MEMCACHE:=${CACHESERVER}:${CACHEPORT}}"
: "${REDISCACHESERVER:=rediscache}"
: "${REDISCACHEPORT:=6379}"
: "${REDISCACHE:=${REDISCACHESERVER}:${REDISCACHEPORT}}"
# variables to control where tests will look for the app (aloe via selenium hub)
: "${TEST_APP_SCHEME:=http}"
: "${TEST_APP_HOST:=runservertest}"
: "${TEST_APP_PORT:=8000}"
: "${TEST_APP_PATH:=/}"
: "${TEST_APP_URL:=${TEST_APP_SCHEME}://${TEST_APP_HOST}:${TEST_APP_PORT}${TEST_APP_PATH}}"
#: "${TEST_BROWSER:=chrome}"
: "${TEST_BROWSER:=firefox}"
: "${TEST_WAIT:=30}"
: "${TEST_SELENIUM_HUB:=http://hub:4444/wd/hub}"
: "${DJANGO_FIXTURES:=none}"
export DBSERVER DBPORT DBUSER DBNAME DBPASS MEMCACHE REDISCACHE DOCKER_ROUTE
export CLINICAL_DBSERVER CLINICAL_DBPORT CLINICAL_DBUSER CLINICAL_DBNAME CLINICAL_DBPASS
export REPORTING_DBSERVER REPORTING_DBPORT REPORTING_DBUSER REPORTING_DBNAME REPORTING_DBPASS
export TEST_APP_URL TEST_APP_SCHEME TEST_APP_HOST TEST_APP_PORT TEST_APP_PATH TEST_BROWSER TEST_WAIT TEST_SELENIUM_HUB
export DJANGO_FIXTURES
}
function _django_check_deploy {
info "running check --deploy"
set -x
django-admin.py check --deploy --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-check.log
set +x
}
function _django_migrate {
info "running migrate"
set -x
django-admin.py migrate --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-migrate.log
django-admin.py migrate --database=clinical --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-migrate-clinical.log
django-admin.py update_permissions --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-permissions.log
set +x
}
function _django_collectstatic {
info "running collectstatic"
set -x
django-admin.py collectstatic --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-collectstatic.log
set +x
}
function _django_test_fixtures {
info 'loading test (iprestrict permissive) fixture'
set -x
django-admin.py init iprestrict_permissive
django-admin.py reload_rules
set +x
}
function _django_dev_fixtures {
info "loading DEV fixture"
set -x
django-admin.py init DEV
django-admin.py reload_rules
set +x
}
function _rdrf_import_grdr {
info "importing grdr registry"
set -x
django-admin.py import_registry --file=/app/grdr.yaml
set +x
}
function _django_fixtures {
if [ "${DJANGO_FIXTURES}" = 'test' ]; then
_django_test_fixtures
fi
if [ "${DJANGO_FIXTURES}" = 'dev' ]; then
_django_dev_fixtures
fi
}
function _runserver() {
: "${RUNSERVER_OPTS=${RUNSERVER_CMD} 0.0.0.0:${RUNSERVERPORT} --settings=${DJANGO_SETTINGS_MODULE}}"
_django_collectstatic
_django_migrate
_django_fixtures
info "RUNSERVER_OPTS is ${RUNSERVER_OPTS}"
set -x
# shellcheck disable=SC2086
exec django-admin.py ${RUNSERVER_OPTS}
}
function _aloe() {
export DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE}"_test
shift
set -x
exec django-admin.py harvest --with-xunit --xunit-file="${WRITABLE_DIRECTORY}"/tests.xml --verbosity=3 "$@"
}
trap exit SIGHUP SIGINT SIGTERM
defaults
env | grep -iv PASS | sort
wait_for_services
# prod uwsgi entrypoint
if [ "$1" = 'uwsgi' ]; then
info "[Run] Starting prod uwsgi"
_django_collectstatic
_django_migrate
_django_check_deploy
set -x
exec uwsgi --die-on-term --ini "${UWSGI_OPTS}"
fi
# local and test uwsgi entrypoint
if [ "$1" = 'uwsgi_local' ]; then
info "[Run] Starting local uwsgi"
_django_collectstatic
_django_migrate
_django_fixtures
_django_check_deploy
set -x
exec uwsgi --die-on-term --ini "${UWSGI_OPTS}"
fi
# runserver entrypoint
if [ "$1" = 'runserver' ]; then
info "[Run] Starting runserver"
_runserver
fi
# runserver_plus entrypoint
if [ "$1" = 'runserver_plus' ]; then
info "[Run] Starting runserver_plus"
RUNSERVER_CMD=runserver_plus
_runserver
fi
# celery_worker entrypoint
if [ "$1" = 'celery_worker' ]; then
info "[Run] Starting celery_worker"
set -x
exec celery -A rdrf worker -l INFO
fi
# grdr entrypoint
if [ "$1" = 'grdr' ]; then
info "[Run] Starting runserver_plus with GRDR data elements"
_django_collectstatic
_django_migrate
_django_fixtures
_rdrf_import_grdr
RUNSERVER_CMD=runserver_plus
_runserver
fi
# runtests entrypoint
if [ "$1" = 'runtests' ]; then
info "[Run] Starting tests"
export DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE}"_test
set -x
single_test="$2"
if [ "$single_test" != "" ]; then
exec django-admin.py test --noinput -v 3 "$single_test"
else
exec django-admin.py test --noinput -v 3 dashboards rdrf
fi
fi
# aloe entrypoint
if [ "$1" = 'aloe' ]; then
info "[Run] Starting aloe"
cd /app/rdrf/rdrf/testing/behaviour || exit
_aloe "$@"
fi
# allow execution of a management command
if [ "$1" = "management-command" ]; then
command_line="django-admin.py $2"
exec $command_line
fi
warn "[RUN]: Builtin command not provided [tarball|aloe|runtests|runserver|runserver_plus|uwsgi|uwsgi_local]"
info "[RUN]: $*"
set -x
# shellcheck disable=SC2086 disable=SC2048
exec "$@"