-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.sh
332 lines (283 loc) · 11.2 KB
/
lib.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
#!/bin/bash
# library for building and running the Beluga Project
set -euo pipefail
# Global variables
container_name_db=postgres
container_name_webapp=webapp
container_name_server=server
path_postgresql=/var/lib/postgresql
path_db_content=$path_postgresql/dbContent
aircraft_database_zipfilename=aircraftDatabase.zip
aircraft_database_filename=aircraftDatabase.csv
airport_database_filename=airports.csv
aircraft_database_url=https://opensky-network.org/datasets/metadata/$aircraft_database_zipfilename
airport_database_url=https://davidmegginson.github.io/ourairports-data/$airport_database_filename
load_beluga_db=loadBelugaDb
load_beluga_db_filename=$load_beluga_db.sh
path_load_beluga_db=assets/scripts/$load_beluga_db_filename
load_beluga_db_output_file=$load_beluga_db-output.txt
load_aircraftdata=loadAircraftData
load_aircraftdata_filename=$load_aircraftdata.sh
path_load_aircraftdata=assets/scripts/$load_aircraftdata_filename
load_aircraftdata_output_file=$load_aircraftdata-output.txt
_ask_user_with_message() {
read -p "$1" choice
case "$choice" in
y | Y) echo "-> Yes, let's continue ..." ;;
n | N)
echo "-> No, let's stop here."
exit
;;
*)
echo "-> Invalid, let's stop here."
exit
;;
esac
}
_ask_user_for_decision() {
read -p "$1" choice
case "$choice" in
y | Y) echo "-> Okay, let's do it ..." ;;
n | N)
echo "-> Okay, we skip that step." ;;
*)
echo "-> Invalid answer." ;;
esac
}
_docker_run() {
echo "Run the containers ..."
docker compose up
}
_docker_run_background() {
echo "Run the containers in the background ..."
docker compose up -d
}
_docker_build() {
if [[ $# -eq 0 ]]; then
echo "Build all images (force rebuild of existing images) ..."
docker compose build --progress=plain --no-cache
else
echo "Build the image for $1 (force rebuild of existing image) ..."
docker compose build --progress=plain --no-cache $1
fi
}
_docker_stop_container() {
if [[ $# -eq 0 ]]; then
_ask_user_with_message "Do you really want to stop all containers for the Beluga Project (y/n)?"
_docker_stop_all_containers
else
echo "Stop container $1 ..."
local container_id=$(docker ps -aqf "name=$1")
if [[ $container_id ]]; then
docker stop $container_id
echo "-> Stopped container for $1. Done."
else
echo "-> No container $1 to stop. Done."
fi
fi
}
_docker_stop_all_containers() {
echo "Stop all containers for Beluga Project ..."
_docker_stop_container $container_name_webapp
_docker_stop_container $container_name_server
_docker_stop_container $container_name_db
echo "-> Stopped all containers for Beluga Project. Done."
}
_docker_rm_container() {
if [[ $# -eq 0 ]]; then
_ask_user_with_message "Do you really want to remove all containers for the Beluga Project (y/n)?"
_docker_rm_all_containers
else
echo "Remove container $1 ..."
local container_id=$(docker ps -aqf "name=$1")
if [[ $container_id ]]; then
docker rm $container_id
echo "-> Removed container for $1. Done."
else
echo "-> No container $1 to remove. Done."
fi
fi
}
_docker_rm_all_containers() {
echo "Remove all containers for Beluga Project ..."
_docker_rm_container $container_name_webapp
_docker_rm_container $container_name_server
_docker_rm_container $container_name_db
echo "-> All containers for Beluga Project removed. Done."
}
_docker_rm_image() {
if [[ $# -eq 0 ]]; then
_ask_user_with_message "Do you really want to remove all images for the Beluga Project (y/n)?"
_docker_rm_all_images
else
echo "Remove image $1 ..."
local image_id=$(docker images -q $1)
if [[ $image_id ]]; then
docker image rm -f $image_id
echo "-> Removed image for $1. Done."
else
echo "-> No image $1 to remove. Done."
fi
fi
}
_docker_rm_all_images() {
echo "Remove all images for Beluga Project ..."
_docker_rm_image $container_name_webapp
_docker_rm_image $container_name_server
_docker_rm_image $container_name_db
echo "-> Removed all images for Beluga Project. Done."
}
_docker_rm_project() {
_ask_user_with_message "Do you really want to remove all containers and images for the Beluga Project (y/n)?"
_docker_stop_all_containers
_docker_rm_all_containers
_docker_rm_all_images
}
_copy_db_content_to_container() {
echo "Create dbContent directory in $path_db_content ..."
docker exec -ti $container_name_db bash -c "mkdir $path_db_content"
echo "-> Create dbContent directory in $path_db_content. Done."
echo "Copy content from assets/dbContent to $path_db_content ..."
docker cp assets/dbContent $container_name_db:$path_postgresql
echo "-> Copy content from assets/dbContent to $path_db_content. Done."
}
_download_aircraft_database() {
echo "Download $aircraft_database_filename from Opensky-Network ..."
docker exec -ti $container_name_db bash -c "wget $aircraft_database_url -O $aircraft_database_zipfilename"
docker exec -ti $container_name_db bash -c "unzip $aircraft_database_zipfilename -o -j"
echo "-> Download $aircraft_database_filename from Opensky-Network. Done."
echo "Copy $aircraft_database_filename to $path_db_content ..."
docker exec -ti $container_name_db bash -c "cp $aircraft_database_filename $path_db_content"
echo "-> Copy $aircraft_database_filename to $path_db_content. Done."
}
_download_airport_database() {
echo "Download $airport_database_filename from OurAirports ..."
docker exec -ti $container_name_db bash -c "wget $airport_database_url -O $airport_database_filename"
echo "-> Download $airport_database_filename from OurAirports. Done."
echo "Copy $airport_database_filename to $path_db_content ..."
docker exec -ti $container_name_db bash -c "cp $airport_database_filename $path_db_content"
echo "-> Copy $airport_database_filename to $path_db_content. Done."
}
_copy_load_db_script_to_container() {
echo "Copy $load_beluga_db_filename to container ..."
docker cp $path_load_beluga_db $container_name_db:$load_beluga_db_filename
echo "-> Copy $load_beluga_db_filename to container. Done."
}
_copy_load_aircraftdata_script_to_container() {
echo "Copy $load_aircraftdata_filename to container ..."
docker cp $path_load_aircraftdata $container_name_db:$load_aircraftdata_filename
echo "-> Copy $load_aircraftdata_filename to container. Done."
}
_exec_load_db_script() {
echo "Execute $load_beluga_db_filename on container to populate database with content ..."
docker exec $container_name_db bash -c ". $load_beluga_db_filename" >$load_beluga_db_output_file
echo "-> Execute $load_beluga_db_filename on container to populate database with content. Done."
}
_exec_load_aircraftdata_script() {
echo "Execute $load_aircraftdata_filename on container to populate database with aircraftdata ..."
docker exec $container_name_db bash -c ". $load_aircraftdata_filename" >$load_aircraftdata_output_file
echo "-> Execute $load_aircraftdata_filename on container to populate database with aircraftdata. Done."
}
_load_db_content() {
echo "Load csv files into postgres database ..."
if _check_tables_exist -eq 0; then
exit
fi
echo "Create dbContent directory in $path_db_content ..."
if [[ -z $(docker exec -ti $container_name_db bash -c "if [ -d $path_db_content ]; then echo does exist; fi") ]]; then
_copy_db_content_to_container
else
echo "-> Directory $path_db_content already exists. Done."
fi
echo "Download $aircraft_database_filename and $airport_database_filename ... "
if [[ -z $(docker exec -ti $container_name_db bash -c "if test -f $aircraft_database_filename; then echo exists; fi") ]]; then
_download_aircraft_database
else
echo "-> File $aircraft_database_filename already exists. Done."
fi
if [[ -z $(docker exec -ti $container_name_db bash -c "if test -f $airport_database_filename; then echo exists; fi") ]]; then
_download_airport_database
else
echo "-> File $airport_database_filename already exists. Done."
fi
_copy_load_aircraftdata_script_to_container
_copy_load_db_script_to_container
_exec_load_aircraftdata_script
_exec_load_db_script
}
_update_db_content() {
echo "update ... ask user for download of current version ..."
echo "Download $aircraft_database_filename ... "
if [[ -z $(docker exec -ti $container_name_db bash -c "if test -f $aircraft_database_filename; then echo exists; fi") ]]; then
echo "-> file $aircraft_database_filename does not exist, download required."
_download_aircraft_database
else
if [[ $# -eq 0 ]]; then
_ask_user_for_decision "Do you want to download current version of $aircraft_database_filename (y/n)?"
if [ "$choice" != "${choice#[Yy]}" ] ; then
echo "$aircraft_database_filename exists. Download for update requested."
_download_aircraft_database
else
if [ "$choice" != "${choice#[Nn]}" ] ;then
echo "-> $aircraft_database_filename exists. Download for update not requested."
else
echo "-> Invalid answer: $choice. Operation cancelled. Try again."
exit
fi
fi
fi
fi
echo "Download $airport_database_filename ... "
if [[ -z $(docker exec -ti $container_name_db bash -c "if test -f $airport_database_filename; then echo exists; fi") ]]; then
echo "-> file $aircraft_database_filename does not exist, download required."
_download_airport_database
else
if [[ $# -eq 0 ]]; then
_ask_user_for_decision "Do you want to download current version of $airport_database_filename (y/n)?"
if [ "$choice" != "${choice#[Yy]}" ] ; then
echo "$airport_database_filename exists. Download for update requested."
_download_airport_database
else
if [ "$choice" != "${choice#[Nn]}" ] ;then
echo "-> $airport_database_filename exists. Download for update not requested."
else
echo "-> Invalid answer: $choice. Operation cancelled. Try again."
exit
fi
fi
fi
fi
echo "update ... Loading csv files into postgres database ..."
_copy_load_aircraftdata_script_to_container
_copy_load_db_script_to_container
_exec_load_aircraftdata_script
_exec_load_db_script
}
_env() {
echo "Content of .env file:"
cat .env
}
_check_tables_exist() {
local table_does_not_exist=true
local table_to_check=opensky_aircraft
local postgres_db=$(docker exec $container_name_db bash -c "echo \$POSTGRES_DB")
local postgres_user=$(docker exec $container_name_db bash -c "echo \$POSTGRES_USER")
echo "Check if tables in postgres database were created by spring ..."
while $table_does_not_exist; do
if [[ -n $(docker exec -it $container_name_db psql $postgres_db $postgres_user -c "psql $postgres_db -U $postgres_user" -c "\c $postgres_db" -c "\dt" | grep $table_to_check) ]]; then
echo "-> Check if tables in postgres database were created by spring. Done."
table_does_not_exist=false
return 1
else
echo "-> Tables in postgres database were not created by spring yet ... waiting ..."
sleep 1
fi
done
}
_install() {
echo "Install the Beluga Project ..."
_ask_user_with_message "Gentle reminder: Have you configured the values in the .env file (y/n)?"
_docker_run_background
_load_db_content
echo "-> The Beluga Project is running. Done."
}