-
Notifications
You must be signed in to change notification settings - Fork 2
/
entrypoint.sh
352 lines (271 loc) · 7.22 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
#!/bin/bash
set -e
DBUSER=${DBUSER:-postgres}
# psql uses PGPASSWORD environment variable to login
export PGPASSWORD=${DBPASSWORD}
DYCAST_INBOX=${DYCAST_INBOX:-$DYCAST_PATH/inbox}
DYCAST_INBOX_COMPLETED=${DYCAST_INBOX}/completed
DYCAST_OUTBOX=${DYCAST_OUTBOX:-$DYCAST_PATH/outbox}
DYCAST_INIT_PATH=${DYCAST_APP_PATH}/init
PG_SHARE_PATH_2_0=/usr/share/postgresql/9.6/contrib/postgis-2.3
START_DATE=$(date -I -d "${START_DATE}") || exit -1
END_DATE=$(date -I -d "${END_DATE}") || exit -1
SRID_CASES="${SRID_CASES}"
SRID_EXTENT="${SRID_EXTENT}"
EXTENT_MIN_X=${EXTENT_MIN_X}
EXTENT_MIN_Y=${EXTENT_MIN_Y}
EXTENT_MAX_X=${EXTENT_MAX_X}
EXTENT_MAX_Y=${EXTENT_MAX_Y}
HELP_TEXT="
============== Dycast help ==============
Dycast supports any of the following commands:
$(python dycast.py --help)
**** To get more help, use 'docker run cvast/cvast-dycast command --help' ****
Additional commands provided by Docker container:
[setup_dycast] Initializes Dycast: creates the database if it does not exist and
sets up necessary folders
[setup_db] Deletes any existing Dycast database and sets up a fresh one
[run_tests] Run unit tests
[help] or [-h] Display help text
"
display_help() {
echo "${HELP_TEXT}"
}
init_dycast() {
if db_exists; then
echo "Database ${DBNAME} already exists, skipping initialization."
else
echo "Database ${DBNAME} does not exists."
init_db
fi
init_directories
}
wait_for_db() {
echo "Testing database connection: ${DBHOST}:${DBPORT}..."
set +e
tries=0
connected="False"
while [[ ${connected} == "False" ]]
do
psql -h ${DBHOST} -p ${DBPORT} -U postgres -c "select 1" >&/dev/null
return_code=$?
((tries++))
if [[ ${return_code} == 0 ]]; then
connected="True"
elif [[ ${tries} == 45 ]]; then
echo "Database server cannot be reached, exiting..."
exit 1
fi
sleep 1
done
set -e
echo "Connected."
}
db_exists() {
psql -lqt -h ${DBHOST} -U ${DBUSER} | cut -d \| -f 1 | grep -qw ${DBNAME}
}
init_db() {
echo "" && echo ""
echo "*** Initializing database ***"
echo ""
echo "*** Any existing Dycast database will be deleted ***"
echo "" && echo ""
echo "5" && sleep 1 && echo "4" && sleep 1 && echo "3" && sleep 1 && echo "2" && sleep 1 && echo "1" && sleep 1 && echo "0" && echo ""
set +e
echo "Dropping existing database ${DBNAME}"
dropdb -h ${DBHOST} -U ${DBUSER} ${DBNAME} # if necessary
echo ""
set -e
echo "Creating database ${DBNAME}"
createdb -h ${DBHOST} -U ${DBUSER} --encoding=UTF8 ${DBNAME}
echo ""
### Using the new 9.1+ extension method:
echo "Creating extension 'postgis'"
psql -h ${DBHOST} -U ${DBUSER} -d ${DBNAME} -c "CREATE EXTENSION postgis;"
echo ""
echo "Running ${DYCAST_INIT_PATH}/postgres_init.sql"
psql -h ${DBHOST} -U ${DBUSER} -d ${DBNAME} -f ${DYCAST_INIT_PATH}/postgres_init.sql
echo ""
echo "Importing Monte Carlo data: ${MONTE_CARLO_FILE}"
psql -h ${DBHOST} -U ${DBUSER} -d ${DBNAME} -c "\COPY dist_margs FROM '${MONTE_CARLO_FILE}' delimiter ',';"
echo ""
}
init_directories() {
if [[ ! -d ${DYCAST_INBOX_COMPLETED} ]]; then
mkdir -p ${DYCAST_INBOX_COMPLETED}
fi
if [[ ! -d ${DYCAST_OUTBOX}/tmp ]]; then
mkdir -p ${DYCAST_OUTBOX}/{tmp,cur,new}
fi
}
listen_for_input() {
echo ""
echo "*** Dycast is now listening for new .tsv files in ${DYCAST_INBOX}... ***"
echo ""
while true; do
for file in ${DYCAST_INBOX}/*.tsv; do
if [[ -f ${file} ]]; then
load_cases "${file}"
move_case_file "${filePath}"
generate_risk
export_risk
echo "Done."
fi
done
sleep 5
done
}
check_variable() {
local VARIABLE_VALUE=$1
local VARIABLE_NAME=$2
if [[ -z ${VARIABLE_VALUE} ]] || [[ "${VARIABLE_VALUE}" == "" ]]; then
echo "ERROR! Environment variable ${VARIABLE_NAME} not specified. Exiting..."
exit 1
fi
}
check_database_variables() {
check_variable "${DBPASSWORD}" DBPASSWORD
check_variable "${DBNAME}" DBNAME
check_variable "${DBHOST}" DBHOST
check_variable "${DBPORT}" DBPORT
}
move_case_file() {
local file="$1"
echo "Completed loading input file, moving it to ${DYCAST_INBOX_COMPLETED}"
filename=$(basename "$file")
mv "${file}" "${DYCAST_INBOX_COMPLETED}/${filename}_completed"
}
### Commands
run_dycast() {
local arguments="$@"
echo "Running Dycast using arguments: ${arguments}..."
python ${DYCAST_APP_PATH}/dycast.py run_dycast ${arguments}
exit_code=$?
if [[ ! "${exit_code}" == "0" ]]; then
echo "Command 'run_dycast' failed, exiting..."
exit ${exit_code}
else
echo "Finished running the full Dycast procedure"
fi
}
load_cases() {
local arguments="$@"
echo "Loading cases using arguments: ${arguments}..."
python ${DYCAST_APP_PATH}/dycast.py load_cases ${arguments}
exit_code=$?
if [[ ! "${exit_code}" == "0" ]]; then
echo "Command 'load_cases' failed, exiting..."
exit ${exit_code}
else
echo "Done loading cases"
fi
}
generate_risk() {
local arguments="$@"
echo ""
echo "Generating risk using arguments: ${arguments}..."
echo ""
python ${DYCAST_APP_PATH}/dycast.py generate_risk ${arguments}
exit_code=$?
if [[ ! "${exit_code}" == "0" ]]; then
echo "Command 'generate_risk' failed, exiting..."
exit ${exit_code}
else
echo "Done generating risk"
fi
}
export_risk() {
local arguments="$@"
echo ""
echo "Exporting risk using arguments: ${arguments}..."
echo ""
python ${DYCAST_APP_PATH}/dycast.py export_risk ${arguments}
exit_code=$?
if [[ ! "${exit_code}" == "0" ]]; then
echo "Command 'export_risk' failed, exiting..."
exit ${exit_code}
else
echo "Done exporting risk"
fi
}
run_tests() {
local arguments="$@"
init_dycast
echo "Running unit tests..."
nosetests -vv --exe tests ${arguments}
exit_code=$?
if [[ ! "${exit_code}" == "0" ]]; then
echo "Unit test(s) failed, exiting..."
exit ${exit_code}
fi
}
### Starting point ###
# 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
display_help
fi
# Else, process arguments
full_command="$@"
arguments="${@:2}"
command="$1"
echo "Command: ${command}"
if [[ ! "${command}" == "help" ]] &&
[[ ! "${command}" == "-h" ]] &&
[[ ! "${command}" == "--help" ]] &&
[[ ! "${arguments}" == "-h" ]] &&
[[ ! "${arguments}" == "--help" ]]; then
check_database_variables
else
help=true
fi
case ${command} in
### First list all commands built into Dycast, not into this Docker entrypoint
run_dycast)
if [[ ! ${help} == true ]]; then
wait_for_db
init_dycast
fi
run_dycast ${arguments}
;;
load_cases)
if [[ ! ${help} == true ]]; then
wait_for_db
fi
load_cases ${arguments}
;;
generate_risk)
if [[ ! ${help} == true ]]; then
wait_for_db
fi
generate_risk ${arguments}
;;
export_risk)
if [[ ! ${help} == true ]]; then
wait_for_db
fi
export_risk ${arguments}
;;
### From here list only commands that are specific for this Docker entrypoint
run_tests)
wait_for_db
run_tests ${arguments}
;;
setup_dycast)
wait_for_db
init_dycast
;;
setup_db)
wait_for_db
init_db
;;
help|-h|--help)
display_help
;;
*)
exec "$@"
;;
esac