-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·578 lines (478 loc) · 12.7 KB
/
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#!/bin/bash
HELP_TEXT="
Arguments:
run_arches: Default. Run the Arches server
run_tests: Run unit tests
setup_arches: Delete any existing Arches database and set up a fresh one
-h or help: Display help text
"
display_help() {
echo "${HELP_TEXT}"
}
CUSTOM_SCRIPT_FOLDER=${CUSTOM_SCRIPT_FOLDER:-/docker/entrypoint}
if [[ -z ${ARCHES_PROJECT} ]]; then
APP_FOLDER=${ARCHES_ROOT}
PACKAGE_JSON_FOLDER=${ARCHES_ROOT}/arches/install
else
APP_FOLDER=${WEB_ROOT}/${ARCHES_PROJECT}
# due to https://github.com/archesproject/arches/issues/4841, changes were made to yarn install
# and module deployment. Using the arches install directory for yarn.
# PTW PACKAGE_JSON_FOLDER=${ARCHES_ROOT}/arches/install
PACKAGE_JSON_FOLDER=${WEB_ROOT}/${ARCHES_PROJECT}/${ARCHES_PROJECT}
fi
# Read modules folder from yarn config file
# Get string after '--install.modules-folder' -> get first word of the result
# -> remove line endlings -> trim quotes -> trim leading ./
YARN_MODULES_FOLDER=${PACKAGE_JSON_FOLDER}/$(awk \
-F '--install.modules-folder' '{print $2}' ${PACKAGE_JSON_FOLDER}/.yarnrc \
| awk '{print $1}' \
| tr -d $'\r' \
| tr -d '"' \
| sed -e "s/^\.\///g")
export DJANGO_PORT=${DJANGO_PORT:-8000}
#COUCHDB_URL="http://$COUCHDB_USER:$COUCHDB_PASS@$COUCHDB_HOST:$COUCHDB_PORT"
STATIC_ROOT=${STATIC_ROOT:-/static_root}
export ALLOW_BOOTSTRAP=${ALLOW_BOOTSTRAP:-}
cd_web_root() {
cd ${WEB_ROOT}
echo "Current work directory: ${WEB_ROOT}"
}
cd_arches_root() {
cd ${ARCHES_ROOT}
echo "Current work directory: ${ARCHES_ROOT}"
}
cd_app_folder() {
cd ${APP_FOLDER}
echo "Current work directory: ${APP_FOLDER}"
}
cd_yarn_folder() {
cd ${PACKAGE_JSON_FOLDER}
echo "Current work directory: ${PACKAGE_JSON_FOLDER}"
}
activate_virtualenv() {
. ${WEB_ROOT}/ENV/bin/activate
}
#### Install
init_arches() {
if db_exists; then
echo "Database ${PGDBNAME} already exists, skipping initialization."
echo ""
else
if [[ "${ALLOW_BOOTSTRAP}" == "True" ]]; then
setup_arches
else
echo "Database ${PGDBNAME} does not exist yet, exiting until you 'entrypoint.sh bootstrap'..."
sleep 1;
exit 1;
fi
fi
}
bootstrap() {
init_arches_project
init_yarn_components
setup_arches
run_yarn_build_development
}
# Setup Postgresql and Elasticsearch
setup_arches() {
cd_arches_root
echo "" && echo ""
echo "*** Initializing database ***"
echo ""
echo "*** Any existing Arches database will be deleted ***"
echo "" && echo ""
echo "5" && sleep 10 && echo "4" && sleep 1 && echo "3" && sleep 1 && echo "2" && sleep 1 && echo "1" && sleep 1 && echo "0" && echo ""
echo "Running: python manage.py setup_db --force"
python ${APP_FOLDER}/manage.py setup_db --force
#echo "Running: Creating couchdb system databases"
#curl -X PUT ${COUCHDB_URL}/_users
#curl -X PUT ${COUCHDB_URL}/_global_changes
#curl -X PUT ${COUCHDB_URL}/_replicator
if [[ "${INSTALL_DEFAULT_GRAPHS}" == "True" ]]; then
# Import graphs
if ! graphs_exist; then
echo "Running: python manage.py packages -o import_graphs"
python ${APP_FOLDER}/manage.py packages -o import_graphs
else
echo "Graphs already exist in the database. Skipping 'import_graphs'."
fi
fi
if [[ "${INSTALL_DEFAULT_CONCEPTS}" == "True" ]]; then
# Import concepts
if ! concepts_exist; then
import_reference_data arches/db/schemes/arches_concept_scheme.rdf
else
echo "Concepts already exist in the database."
echo "Skipping 'arches_concept_scheme.rdf'."
echo "Skipping 'cvast_concept_scheme.rdf'."
fi
# Import collections
if ! collections_exist; then
import_reference_data arches/db/schemes/arches_concept_collections.rdf
else
echo "Collections already exist in the database."
echo "Skipping 'import_reference_data arches_concept_collections.rdf'."
fi
fi
run_migrations
if [[ "${INSTALL_CORAL_PACKAGE}" == "True" ]]; then
# Import graphs
echo "Running: python manage.py packages -o load_package -s coral/pkg/ -y"
python manage.py packages -o load_package -s coral/pkg/ -y;
python manage.py es index_database
fi
}
wait_for_db() {
echo "Testing if database server is up..."
while [[ ! ${return_code} == 0 ]]
do
psql --host=${PGHOST} --port=${PGPORT} --user=${PGUSERNAME} --dbname=postgres -c "select 1" >&/dev/null
return_code=$?
sleep 1
done
echo "Database server is up"
echo "Testing if Elasticsearch is up..."
while [[ ! ${return_code} == 0 ]]
do
curl -s "http://${ESHOST}:${ESPORT}/_cluster/health?wait_for_status=green&timeout=60s" >&/dev/null
return_code=$?
sleep 1
done
echo "Elasticsearch is up"
}
db_exists() {
echo "Checking if database "${PGDBNAME}" exists..."
count=`psql --host=${PGHOST} --port=${PGPORT} --user=${PGUSERNAME} --dbname=postgres -Atc "SELECT COUNT(*) FROM pg_catalog.pg_database WHERE datname='${PGDBNAME}'"`
# Check if returned value is a number and not some error message
re='^[0-9]+$'
if ! [[ ${count} =~ $re ]] ; then
echo "Error: Something went wrong when checking if database "${PGDBNAME}" exists..." >&2;
echo "Exiting..."
exit 1
fi
# Return 0 (= true) if database exists
if [[ ${count} > 0 ]]; then
return 0
else
return 1
fi
}
set_dev_mode() {
echo ""
echo ""
echo "----- SETTING DEV MODE -----"
echo ""
cd_arches_root
python ${ARCHES_ROOT}/setup.py develop
}
# Yarn
init_yarn_components() {
if [[ ! -d ${YARN_MODULES_FOLDER} ]] || [[ ! "$(ls ${YARN_MODULES_FOLDER})" ]]; then
echo "Yarn modules do not exist, installing..."
install_yarn_components
fi
}
# This is also done in Dockerfile, but that does not include user's custom Arches app package.json
# Also, the packages folder may have been overlaid by a Docker volume.
install_yarn_components() {
echo ""
echo ""
echo "----- INSTALLING YARN COMPONENTS -----"
echo ""
cd_yarn_folder
yarn install -D
}
#### Main commands
run_arches_graphql() {
if [[ "${DJANGO_MODE}" == "DEV" ]]; then
set_dev_mode
fi
run_graphql_server
}
run_graphql_server() {
echo ""
echo ""
echo "----- *** RUNNING GRAPHQL SERVER *** -----"
echo ""
cd_app_folder
uvicorn --host 0.0.0.0 --port 8000 ${ARCHES_PROJECT}.graph.asgi:app
}
run_yarn_start() {
echo ""
echo ""
echo "----- RUNNING YARN SERVER -----"
echo ""
cd_app_folder
sleep 10
cd ${ARCHES_PROJECT}
yarn start
}
run_yarn_build_production() {
echo ""
echo ""
echo "----- RUNNING YARN SERVER -----"
echo ""
cd_app_folder
sleep 10
cd ${ARCHES_PROJECT}
yarn build_production
}
run_yarn_build_development() {
echo ""
echo ""
echo "----- RUNNING YARN SERVER -----"
echo ""
cd_app_folder
sleep 10
cd ${ARCHES_PROJECT}
yarn build_development
}
#### Misc
init_arches_project() {
if [[ ! -z ${ARCHES_PROJECT} ]]; then
echo "Checking if Arches project "${ARCHES_PROJECT}" exists..."
if [[ ! -d ${APP_FOLDER} ]] || [[ ! "$(ls ${APP_FOLDER})" ]]; then
echo ""
echo "----- Custom Arches project '${ARCHES_PROJECT}' does not exist. -----"
echo "----- Creating '${ARCHES_PROJECT}'... -----"
echo ""
cd_web_root
[[ -d ${APP_FOLDER} ]] || mkdir ${APP_FOLDER}
arches-project create ${ARCHES_PROJECT} --directory ${ARCHES_PROJECT}
exit_code=$?
if [[ ${exit_code} != 0 ]]; then
echo "Something went wrong when creating your Arches project: ${ARCHES_PROJECT}."
echo "Exiting..."
exit ${exit_code}
fi
copy_settings_local
else
echo "Custom Arches project '${ARCHES_PROJECT}' already exists."
fi
fi
}
graphs_exist() {
row_count=$(psql -h ${PGHOST} -p ${PGPORT} -U postgres -d ${PGDBNAME} -Atc "SELECT COUNT(*) FROM public.graphs")
if [[ ${row_count} -le 3 ]]; then
return 1
else
return 0
fi
}
concepts_exist() {
row_count=$(psql -h ${PGHOST} -p ${PGPORT} -U postgres -d ${PGDBNAME} -Atc "SELECT COUNT(*) FROM public.concepts WHERE nodetype = 'Concept'")
if [[ ${row_count} -le 2 ]]; then
return 1
else
return 0
fi
}
collections_exist() {
row_count=$(psql -h ${PGHOST} -p ${PGPORT} -U postgres -d ${PGDBNAME} -Atc "SELECT COUNT(*) FROM public.concepts WHERE nodetype = 'Collection'")
if [[ ${row_count} -le 1 ]]; then
return 1
else
return 0
fi
}
import_reference_data() {
# Import example concept schemes
local rdf_file="$1"
echo "Running: python manage.py packages -o import_reference_data -s \"${rdf_file}\""
python ${APP_FOLDER}/manage.py packages -o import_reference_data -s "${rdf_file}"
}
copy_settings_local() {
# The settings_local.py in ${ARCHES_ROOT}/arches/ gets ignored if running manage.py from a custom Arches project instead of Arches core app
echo "Copying ${ARCHES_ROOT}/arches/settings_local.py to ${APP_FOLDER}/${ARCHES_PROJECT}/settings_local.py..."
cp ${ARCHES_ROOT}/arches/settings_local.py ${APP_FOLDER}/${ARCHES_PROJECT}/settings_local.py
}
# Allows users to add scripts that are run on startup (after this entrypoint)
run_custom_scripts() {
for file in ${CUSTOM_SCRIPT_FOLDER}/*; do
if [[ -f ${file} ]]; then
echo ""
echo ""
echo "----- RUNNING CUSTUM SCRIPT: ${file} -----"
echo ""
source ${file}
fi
done
}
#### Run
run_migrations() {
echo ""
echo ""
echo "----- RUNNING DATABASE MIGRATIONS -----"
echo ""
cd_app_folder
python manage.py migrate
echo $?
echo "[output code]"
}
collect_static(){
echo ""
echo ""
echo "----- NOT COLLECTING DJANGO STATIC FILES AS FROZEN -----"
echo ""
# cd_app_folder
# python manage.py collectstatic --noinput
}
collect_static_real(){
echo ""
echo ""
echo "----- COLLECT DJANGO STATIC FILES -----"
echo ""
cd_app_folder
python manage.py collectstatic --noinput
python manage.py compress --verbosity=3
}
run_django_server() {
echo ""
echo ""
echo "----- *** RUNNING DJANGO DEVELOPMENT SERVER *** -----"
echo ""
cd_app_folder
if [[ ${DJANGO_REMOTE_DEBUG} != "True" ]]; then
echo "Running Django with livereload."
exec python manage.py runserver 0.0.0.0:${DJANGO_PORT}
else
echo "Running Django with options --noreload --nothreading for remote debugging."
exec python manage.py runserver --noreload --nothreading 0.0.0.0:${DJANGO_PORT}
fi
}
run_celery_worker() {
echo ""
echo ""
echo "----- *** RUNNING CELERY WORKER *** -----"
echo ""
cd_app_folder
exec python manage.py celery start
}
run_api_server() {
echo ""
echo ""
echo "----- *** RUNNING API SERVER *** -----"
echo ""
cd_app_folder
if [[ ! -z ${ARCHES_PROJECT} ]]; then
DJANGO_SETTINGS_MODULE=${ARCHES_PROJECT}.settings gunicorn arches_orm.graphql.django_asgi:app \
--config ${ARCHES_ROOT}/docker/gunicorn_config.py \
-k uvicorn.workers.UvicornWorker
fi
}
run_gunicorn_server() {
echo ""
echo ""
echo "----- *** RUNNING GUNICORN PRODUCTION SERVER *** -----"
echo ""
cd_app_folder
if [[ ! -z ${ARCHES_PROJECT} ]]; then
gunicorn ${ARCHES_PROJECT}.wsgi:application \
--config ${ARCHES_ROOT}/docker/gunicorn_config.py
else
gunicorn arches.wsgi:application \
--config ${ARCHES_ROOT}/docker/gunicorn_config.py
fi
}
#### Main commands
run_arches() {
init_arches
if [[ "${DJANGO_MODE}" == "DEV" ]]; then
set_dev_mode
fi
run_custom_scripts
if [[ "${DJANGO_MODE}" == "DEV" ]]; then
run_django_server
elif [[ "${DJANGO_MODE}" == "STATIC" ]]; then
collect_static_real
elif [[ "${DJANGO_MODE}" == "PROD" ]]; then
run_gunicorn_server
fi
}
run_tests() {
set_dev_mode
echo ""
echo ""
echo "----- RUNNING ARCHES TESTS -----"
echo ""
cd_arches_root
python manage.py test tests --pattern="*.py" --settings="tests.test_settings" --exe
if [ $? -ne 0 ]; then
echo "Error: Not all tests ran succesfully."
echo "Exiting..."
exit 1
fi
}
### Starting point ###
activate_virtualenv
# Use -gt 1 to consume two arguments per pass in the loop (e.g. each
# argument has a corresponding value to go with it).
# Use -gt 0 to consume one or more arguments per pass in the loop (e.g.
# some arguments don't have a corresponding value to go with it, such as --help ).
# If no arguments are supplied, assume the server needs to be run
if [[ $# -eq 0 ]]; then
wait_for_db
run_arches
fi
# Else, process arguments
echo "Full command: $@"
while [[ $# -gt 0 ]]
do
key="$1"
echo "Command: ${key}"
case ${key} in
bootstrap)
wait_for_db
bootstrap
;;
run_arches)
wait_for_db
run_arches
;;
run_api)
wait_for_db
run_api_server
;;
run_celery)
wait_for_db
run_celery_worker
;;
setup_arches)
wait_for_db
setup_arches
;;
run_arches_graphql)
wait_for_db
run_arches_graphql
;;
run_tests)
wait_for_db
run_tests
;;
run_migrations)
wait_for_db
run_migrations
;;
install_yarn_components)
install_yarn_components
;;
run_yarn_build_development)
run_yarn_build_development
;;
run_yarn_build_production)
run_yarn_build_production
;;
run_yarn_start)
run_yarn_start
;;
help|-h)
display_help
;;
*)
cd_app_folder
"$@"
exit 0
;;
esac
shift # next argument or value
done