1
1
#! /bin/bash
2
- set -euo pipefail
2
+ set -eo pipefail
3
3
4
4
cd $( dirname " $0 " ) /..
5
5
6
- DUMP_LOCAL_FILENAME=" jore4dump.pgdump"
6
+ DUMP_ROUTES_FILENAME=" routes-12-2024.pgdump"
7
+ DUMP_TIMETABLES_FILENAME=" timetables-12-2024.pgdump"
8
+ DUMP_STOPS_FILENAME=" stopdb-12-2024.pgdump"
9
+
10
+ DOCKER_TESTDB_IMAGE=" jore4-testdb"
11
+ DOCKER_IMAGES=" jore4-auth jore4-hasura jore4-mbtiles jore4-mapmatchingdb jore4-mapmatching jore4-hastus jore4-tiamat jore4-timetablesapi"
12
+ DOCKER_E2E_IMAGES=" jore4-hasura-e2e jore4-tiamat-e2e jore4-timetablesapi-e2e jore4-testdb-e2e"
13
+
14
+ ROUTES_DB_CONNECTION_STRING=postgresql://dbadmin:adminpassword@localhost:5432/jore4e2e
15
+
16
+ INCLUDE_E2E=true
17
+ USE_VOLUME=false
18
+
19
+ LOGGED_IN=false
20
+
21
+ for param in $@
22
+ do
23
+ if [ " $param " = " --volume" ]
24
+ then
25
+ USE_VOLUME=true
26
+ fi
27
+ if [ " $param " = " --skip-e2e" ]
28
+ then
29
+ INCLUDE_E2E=false
30
+ fi
31
+ done
32
+
33
+ DOCKER_COMPOSE_CMD=" docker compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.custom.yml"
34
+ if [ " $USE_VOLUME " = true ]; then
35
+ # start the testdb with mounted volume
36
+ DOCKER_COMPOSE_CMD=" $DOCKER_COMPOSE_CMD -f ./docker/docker-compose.testdb-volume.yml"
37
+ fi
38
+
39
+ # start up only services that are needed in local ui development
40
+ if [ " $INCLUDE_E2E " = true ]; then
41
+ DOCKER_COMPOSE_CMD=" $DOCKER_COMPOSE_CMD -f ./docker/docker-compose.e2e.yml"
42
+ fi
43
+
44
+ function login {
45
+ if [ $LOGGED_IN != true ]; then
46
+ echo " Log in to Azure"
47
+ az login
48
+ LOGGED_IN=true
49
+ fi
50
+ }
51
+
52
+ function wait_for_database {
53
+ SUCCESS=false
54
+ while ! $SUCCESS ; do
55
+ echo " $1 : Checking if schema $2 and table $3 exist..."
56
+ if [[ $( docker exec $1 psql $ROUTES_DB_CONNECTION_STRING -AXqtc " SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = '$2 ' AND tablename = '$3 ');" 2> /dev/null) = " t" ]]; then
57
+ SUCCESS=true
58
+ fi
59
+ sleep 2
60
+ done
61
+ }
62
+
63
+ function seed_infra_links {
64
+ echo " $1 : Seeding infrastructure links..."
65
+
66
+ wait_for_database $1 infrastructure_network infrastructure_link
67
+ docker exec -i $1 psql $ROUTES_DB_CONNECTION_STRING < test-db-manager/src/dumps/infraLinks/infraLinks.sql;
68
+ }
69
+
70
+ function check_pinned_image {
71
+ DOCKER_JQ=" docker run --rm -i imega/jq"
72
+ DOCKER_YQ=" docker run --rm -i mikefarah/yq"
73
+ GREEN=' \033[1;32m'
74
+ RED=' \033[1;31m'
75
+ NO_COLOR=' \033[0m'
76
+
77
+ PREFIX=" ${2:- main-} "
78
+
79
+ # Find latest image with "hsl-main-" tag prefix from docker hub
80
+ dockerHubImageList=$( curl --silent --get -H \" Accept: application/json\" https://hub.docker.com/v2/repositories/hsldevcom/jore4-${1} /tags/\? page_size=100\& page=1\& ordering=last_updated)
81
+ dockerHubTag=" $( echo ${dockerHubImageList} | ${DOCKER_JQ} --arg PREFIX $PREFIX --raw-output ' first(.results[] | select(.name | startswith($PREFIX))).name' ) "
82
+ dockerHubImage=" hsldevcom/jore4-${1} :${dockerHubTag} "
83
+ echo " Docker hub image: ${dockerHubImage} "
84
+
85
+ # Find current tag from values
86
+ localImage=" $( cat ./docker/docker-compose.custom.yml | ${DOCKER_YQ} e \" .services.jore4-${1} .image\" ) "
87
+ echo " Local image: ${localImage} "
88
+
89
+ # Warn the user if the local pinned version differs from the current one
90
+ if [[ " $dockerHubImage " == " $localImage " ]]; then
91
+ echo -e " ${GREEN} The pinned ${1} image is current, no need to update${NO_COLOR} "
92
+ else
93
+ echo -e " ${RED} The pinned ${1} image version is different compared to the docker hub version"
94
+ echo -e " You should update the ${1} image in 'docker-compose.custom.yml' to: ${dockerHubImage}${NO_COLOR} "
95
+ fi
96
+ }
97
+
98
+ function start_docker_images {
99
+ echo " Running docker compose command: $DOCKER_COMPOSE_CMD "
100
+
101
+ $DOCKER_COMPOSE_CMD up -d $@
102
+ }
103
+
104
+ function stop_dependencies {
105
+ docker compose -f ./docker/docker-compose.yml -f ./docker/docker-compose.custom.yml -f ./docker/docker-compose.e2e.yml down --volumes
106
+ }
107
+
108
+ function start_dependencies {
109
+ if ! command -v gh; then
110
+ echo " Please install the github gh tool on your machine."
111
+ exit 1
112
+ fi
113
+
114
+ # initialize package folder
115
+ mkdir -p ./docker
116
+
117
+ echo " Downloading latest version of E2E docker-compose package..."
118
+ curl https://raw.githubusercontent.com/HSLdevcom/jore4-tools/main/docker/download-docker-bundle.sh | bash
119
+
120
+ additional_images=" "
121
+ if [ " $INCLUDE_E2E " = true ]; then
122
+ additional_images=$DOCKER_E2E_IMAGES
123
+ fi
124
+
125
+ start_docker_images $DOCKER_TESTDB_IMAGE $DOCKER_IMAGES $additional_images
126
+
127
+ check_images
128
+ }
7
129
8
130
function download_dump {
9
131
echo " Downloading JORE4 dump from Azure"
@@ -14,65 +136,173 @@ function download_dump {
14
136
# - "data-only" ~ The dump contains only data. It does not contain DDL, i.e. table and other schema element definitions.
15
137
# - "8a28ef5f" ~ The dump is based on the database migrations of the jore4-hasura image version starting with this hash.
16
138
# - "20240104" (2nd) ~ The day when the jore3-importer was run
17
- read -p " Dump file name (default: jore4e2e-test-20240104-data-only-8a28ef5f-20240104.pgdump): " DUMP_FILENAME
18
- DUMP_FILENAME=" ${DUMP_FILENAME:- jore4e2e-test-20240104-data-only-8a28ef5f-20240104.pgdump} "
19
-
20
- echo " Log in to Azure"
21
- az login
22
-
23
- echo " Downloading dump file as jore4dump.pgdump"
24
- az storage blob download \
25
- --account-name " jore4storage" \
26
- --container-name " jore4-dump" \
27
- --name " $DUMP_FILENAME " \
28
- --file " $DUMP_LOCAL_FILENAME " \
29
- --auth-mode login
30
- }
139
+ if [ -z ${1+x} ]; then
140
+ read -p " Dump file name (default: jore4e2e-test-20240104-data-only-8a28ef5f-20240104.pgdump): " DUMP_FILENAME
141
+ DUMP_FILENAME=" ${DUMP_FILENAME:- jore4e2e-test-20240104-data-only-8a28ef5f-20240104.pgdump} "
142
+ else
143
+ DUMP_FILENAME=$1
144
+ fi
31
145
32
- function import_dump {
33
- echo " Importing JORE4 dump to database"
146
+ login
34
147
35
- read -p " Warning: all the current data in the database will be overwritten! Are you sure (y/n)? " REPLY
36
- if [[ ! $REPLY =~ ^[Yy]$ ] ]
148
+ # Check dump file
149
+ if [ ! -f $1 ]
37
150
then
38
- exit 1
151
+ echo " Downloading dump file as $DUMP_FILENAME "
152
+ az storage blob download \
153
+ --account-name " jore4storage" \
154
+ --container-name " jore4-dump" \
155
+ --name " $DUMP_FILENAME " \
156
+ --file " $DUMP_FILENAME " \
157
+ --auth-mode login
39
158
fi
159
+ }
40
160
41
- docker exec -i testdb pg_restore --format=c --disable-triggers --no-owner --role=dbhasura -f dump.sql < $DUMP_LOCAL_FILENAME
161
+ function import_dump {
162
+ if [[ -z ${1+x} || -z ${2+x} ]]; then
163
+ echo " File and target database need to be defined!"
164
+ echo " usage:"
165
+ echo " development.sh dump:import file database"
166
+ exit
167
+ fi
168
+
169
+ echo " Importing JORE4 dump to $2 database"
42
170
43
- # Add a row to sql dump disabling triggers
44
- docker exec -i testdb sed -i ' 1s;^;SET session_replication_role = replica\;\n;' dump.sql
171
+ # Download dump if it is missing
172
+ if [ ! -f $1 ]; then
173
+ download_dump $1
174
+ fi
45
175
46
- ./scripts/seed-from-dump.sh
176
+ docker exec -i testdb pg_restore -U dbadmin --dbname= $2 --format=c < $1
47
177
}
48
178
49
179
function download_digitransit_key {
50
- echo " Log in to Azure"
51
- az login
180
+ login
52
181
53
182
echo " Downloading secret value to ui/.env.local"
54
183
{ echo -n " NEXT_PUBLIC_DIGITRANSIT_API_KEY=" && az keyvault secret show --name " hsl-jore4-digitransit-api-key" --vault-name " hsl-jore4-dev-vault" --query " value" ; } > ui/.env.local
55
184
}
56
185
186
+ function setup_environment {
187
+
188
+ read -p " Warning: all the current data in the database will be overwritten! Are you sure (y/n)? " REPLY
189
+ if [[ ! $REPLY =~ ^[Yy]$ ]]; then
190
+ exit 1
191
+ fi
192
+
193
+ if ! command -v gh; then
194
+ echo " Please install the github gh tool on your machine."
195
+ exit 1
196
+ fi
197
+
198
+ if [ ! -f ui/.env.local ]; then
199
+ download_digitransit_key
200
+ fi
201
+
202
+ # initialize package folder
203
+ mkdir -p ./docker
204
+
205
+ echo " Downloading latest version of E2E docker-compose package..."
206
+ curl https://raw.githubusercontent.com/HSLdevcom/jore4-tools/main/docker/download-docker-bundle.sh | bash
207
+
208
+ start_docker_images $DOCKER_TESTDB_IMAGE
209
+
210
+ if [[ -z ${1+x} || $1 != " test" ]]; then
211
+ wait_for_database testdb topology topology
212
+ import_dump $DUMP_ROUTES_FILENAME jore4e2e
213
+ import_dump $DUMP_TIMETABLES_FILENAME timetablesdb
214
+ import_dump $DUMP_STOPS_FILENAME stopdb
215
+ fi
216
+
217
+ additional_images=" "
218
+ if [ " $INCLUDE_E2E " = true ]; then
219
+ additional_images=$DOCKER_E2E_IMAGES
220
+ fi
221
+
222
+ start_docker_images $DOCKER_IMAGES $additional_images
223
+
224
+ if [ " $INCLUDE_E2E " = true ]; then
225
+ seed_infra_links testdb-e2e
226
+ fi
227
+
228
+ if [[ $1 = " test" ]]; then
229
+ # Existing tests are made using old data and the data is not compatible with stop registry dump
230
+ seed_infra_links testdb
231
+ old_dump=jore4e2e-test-20240104-data-only-8a28ef5f-20240104.pgdump
232
+ if [ ! -f $old_dump ]; then
233
+ download_dump $old_dump
234
+ fi
235
+ docker exec -i testdb pg_restore --format=c --disable-triggers --no-owner --role=dbhasura -f dump.sql < $old_dump
236
+
237
+ # Add a row to sql dump disabling triggers
238
+ docker exec -i testdb sed -i ' 1s;^;SET session_replication_role = replica\;\n;' dump.sql
239
+ docker exec -i testdb sh -c ' psql postgresql://dbadmin:adminpassword@localhost:5432/jore4e2e < dump.sql'
240
+
241
+ cd ./test-db-manager
242
+ yarn build
243
+ yarn seed
244
+ cd ..
245
+ fi
246
+
247
+
248
+ echo " All done! Happy coding! :)"
249
+ }
250
+
251
+ function check_images {
252
+ check_pinned_image hasura hsl-main-
253
+ check_pinned_image tiamat
254
+ }
255
+
57
256
function usage {
58
257
echo "
59
258
Usage $0 <command>
60
259
260
+ start:deps
261
+ Start dependencies but do not insert data to database
262
+
263
+ stop:deps
264
+ Stop all dependencies
265
+
266
+ setup:env
267
+ Start dependencies and seed databases with dump data
268
+
269
+ setup:test
270
+ Start dependencies and seed databases with test data
271
+
61
272
dump:download
62
273
Downloads JORE4 dump from Azure.
63
274
64
- dump:import
275
+ dump:import file database
65
276
Imports JORE4 dump to running instance of jore4-testdb
66
277
67
278
digitransit:fetch
68
279
Download JORE4 digitransit map API key
69
280
281
+ check:images
282
+ Check if the used images are newest
283
+
70
284
help
71
285
Show this usage information
72
286
"
73
287
}
74
288
75
289
case $1 in
290
+ start:deps)
291
+ start_dependencies
292
+ ;;
293
+
294
+ stop:deps)
295
+ stop_dependencies
296
+ ;;
297
+
298
+ setup:env)
299
+ setup_environment
300
+ ;;
301
+
302
+ setup:test)
303
+ setup_environment test
304
+ ;;
305
+
76
306
dump:download)
77
307
download_dump
78
308
;;
@@ -85,6 +315,10 @@ digitransit:fetch)
85
315
download_digitransit_key
86
316
;;
87
317
318
+ check:images)
319
+ check_images
320
+ ;;
321
+
88
322
help)
89
323
usage
90
324
;;
93
327
usage
94
328
;;
95
329
esac
330
+
0 commit comments