-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocker-entrypoint.sh
executable file
·290 lines (227 loc) · 7.87 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
#!/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_CACHE" ]] ; then
dockerwait "$CACHESERVER" "$CACHEPORT"
fi
if [[ "$WAIT_FOR_RUNSERVER" ]] ; then
dockerwait "$RUNSERVER" "$RUNSERVERPORT"
fi
if [[ "$WAIT_FOR_UWSGI" ]] ; then
dockerwait "$UWSGISERVER" "$UWSGIPORT"
fi
}
function defaults {
: "${DBSERVER:=db}"
: "${DBPORT:=5432}"
: "${DBUSER:=webapp}"
: "${DBNAME:=${DBUSER}}"
: "${DBPASS:=${DBUSER}}"
: "${CLINICAL_DBSERVER:=${DBSERVER}}"
: "${CLINICAL_DBPORT:=5432}"
: "${CLINICAL_DBUSER:=${DBUSER}}"
: "${CLINICAL_DBNAME:=${CLINICAL_DBUSER}}"
: "${CLINICAL_DBPASS:=${DBPASS}}"
: "${UWSGISERVER:=uwsgi}"
: "${UWSGIPORT:=9000}"
: "${UWSGI_OPTS:=/app/uwsgi/docker.ini}"
: "${RUNSERVER:=runserver}"
: "${RUNSERVERPORT:=8000}"
: "${RUNSERVER_CMD:=runserver}"
: "${CACHESERVER:=cache}"
: "${CACHEPORT:=11211}"
MEMCACHE=""
if [[ "$WAIT_FOR_CACHE" ]] ; then
: "${MEMCACHE:=${CACHESERVER}:${CACHEPORT}}"
fi
# 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:=""}"
export DBSERVER DBPORT DBUSER DBNAME DBPASS MEMCACHE
export CLINICAL_DBSERVER CLINICAL_DBPORT CLINICAL_DBUSER CLINICAL_DBNAME CLINICAL_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 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 migrate --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-migrate.log
django-admin migrate --database=clinical --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-migrate-clinical.log
django-admin 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 collectstatic --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1 | tee "${LOG_DIRECTORY}"/uwsgi-collectstatic.log
set +x
}
function _django_fixtures {
info "loading fixtures ${DJANGO_FIXTURES}"
set -x
django-admin init ${DJANGO_FIXTURES}
set +x
}
function _runserver() {
: "${RUNSERVER_OPTS=${RUNSERVER_CMD} 0.0.0.0:${RUNSERVERPORT} --settings=${DJANGO_SETTINGS_MODULE}}"
_django_migrate
_django_fixtures
mkdir -p /data/python_profiling
info "RUNSERVER_OPTS is ${RUNSERVER_OPTS}"
set -x
# shellcheck disable=SC2086
exec django-admin ${RUNSERVER_OPTS}
}
function _aloe() {
export DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE}"_test
shift
set -x
exec django-admin test --testrunner=rdrf.testing.behaviour.features.runner.GherkinNoDjangoTestDBTestRunner --with-xunit --xunit-file="${WRITABLE_DIRECTORY}"/tests.xml --verbosity=3 "$@"
}
trap exit SIGHUP SIGINT SIGTERM
defaults
# Display all env vars, but mask the values of sensitive ones (containing pass, key or secret in their name)
env | sort | awk -F "=" '{ if($1 ~ /(KEY|PASS|SECRET)/) print $1 "=xxxxx"; else print }'
wait_for_services
# prod uwsgi entrypoint
if [ "$1" = 'uwsgi' ]; then
info "[Run] Starting prod uwsgi"
_django_check_deploy
set -x
# exec uwsgi --die-on-term --ini "${UWSGI_OPTS}"
exec uwsgi --http :9000 --wsgi-file /app/uwsgi/django.wsgi --static-map /static=/data/static --ini "${UWSGI_OPTS}"
fi
# prod uwsgi HTTPS entrypoint
if [ "$1" = 'uwsgi_ssl' ]; then
info "[Run] Starting prod uwsgi on HTTPS"
_django_check_deploy
set -x
# exec uwsgi --die-on-term --ini "${UWSGI_OPTS}"
exec uwsgi --master --https 0.0.0.0:9443,/etc/ssl/certs/ssl-cert-snakeoil.pem,/etc/ssl/private/ssl-cert-snakeoil.key --wsgi-file /app/uwsgi/django.wsgi --static-map /static=/data/static
fi
# prod uwsgi HTTPS entrypoint
if [ "$1" = 'uwsgi_ssl_fargate' ]; then
info "[Run] Starting prod uwsgi on HTTPS"
_django_check_deploy
info "running collectstatic"
set -x
django-admin collectstatic --noinput --settings="${DJANGO_SETTINGS_MODULE}" 2>&1
set -x
# exec uwsgi --die-on-term --ini "${UWSGI_OPTS}"
exec uwsgi --master --enable-threads --processes 4 --https 0.0.0.0:9443,/etc/ssl/certs/ssl-cert-snakeoil.pem,/etc/ssl/private/ssl-cert-snakeoil.key --wsgi-file /app/uwsgi/django.wsgi --static-map /static=/data/static --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
# local lambda entrypoint
if [ "$1" = 'lambda_local' ]; then
exec /aws-lambda-rie /usr/local/bin/python -m awslambdaric "${@:2}"
fi
# lambda entrypoint
if [ "$1" = 'awslambdaric' ]; then
exec /usr/local/bin/python -m awslambdaric "${@:2}"
fi
# runtests entrypoint
if [ "$1" = 'runtests' ]; then
info "[Run] Starting tests"
export DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE}"_test
set -x
args="rdrf/rdrf/testing/unit rdrf/report/tests rdrf/useraudit/tests"
if [ "$2" != "" ]; then
# pass through any arguments (if provided) to pytest
args="${@:2}"
fi
cd /app
exec pytest $args
fi
# runtests with coverage entrypoint
if [ "$1" = 'runtests_coverage' ]; then
info "[Run] Starting tests"
export DJANGO_SETTINGS_MODULE="${DJANGO_SETTINGS_MODULE}"_test
set -ex
args="rdrf/rdrf/testing/unit rdrf/report/tests rdrf/useraudit/tests"
if [ "$2" != "" ]; then
# pass through any arguments (if provided) to pytest
args="${@:2}"
fi
cd /app
coverage run -m pytest $args
coverage report -m
exec coverage html
fi
# aloe entrypoint
if [ "$1" = 'aloe' ]; then
info "[Run] Starting aloe"
# cd /app/rdrf/rdrf/testing/behaviour || exit
# Find the aloe tests directory dynamically, in order to also find them in projects
# including TRRF as a git submodule
cd `find /app -path '*/testing/behaviour'` || exit 1
_aloe "$@"
fi
# db_init entrypoint
if [ "$1" = 'db_init' ]; then
info "[Run] Initialising the DB with data"
_django_fixtures
exit
fi
warn "[RUN]: Builtin command not provided [tarball|aloe|runtests|runtests_coverage|runserver|runserver_plus|uwsgi|uwsgi_local|db_init]"
info "[RUN]: $*"
set -x
# shellcheck disable=SC2086 disable=SC2048
exec "$@"