Skip to content

Commit fe8a7ae

Browse files
committed
Add script to do all the (first time) database initialisation
The script vcan be safely run multiple times, e.g. to upgrade the database schema for new versions, or to update the middleware configuration
1 parent 414361e commit fe8a7ae

File tree

4 files changed

+59
-32
lines changed

4 files changed

+59
-32
lines changed

stepup/init-db.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Get this script's directory
6+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
7+
8+
echo "Initializing middleware and gateway databases"
9+
docker compose exec middleware /var/www/html/bin/console doctrine:migrations:migrate --env=prod --em=deploy --no-interaction
10+
11+
echo "Initializing webauthn database"
12+
docker compose exec webauthn /var/www/html/bin/console doctrine:migrations:migrate --env=prod --no-interaction
13+
14+
echo "Pushing middleware configuration"
15+
sh "${DIR}/middleware/middleware-push-config.sh"
16+
17+
echo "Pushing middleware whitelist"
18+
sh "${DIR}/middleware/middleware-push-whitelist.sh"
19+
20+
echo "Pushing middleware institution configuration"
21+
sh "${DIR}/middleware/middleware-push-institution.sh"
22+
23+
echo "Done"

stepup/middleware/middleware-push-config.sh

100644100755
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/bin/bash
22

3-
CWD=$(pwd)
3+
# Get this script's directory
4+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
45

56
function error_exit {
67
echo "${1}"
7-
if [ -n "${TMP_FILE}" -a -d "${TMP_FILE}" ]; then
8+
if [ -n "${TMP_FILE}" ] && [ -d "${TMP_FILE}" ]; then
89
rm "${TMP_FILE}"
910
fi
10-
cd ${CWD}
1111
exit 1
1212
}
1313

@@ -18,20 +18,21 @@ if [ $? -ne "0" ]; then
1818
error_exit "Could not create temp file"
1919
fi
2020

21-
echo "Pushing new config to: http://middleware.dev.openconext.local/management/configuration"
21+
echo "Pushing new middleware configuration to: http://middleware.dev.openconext.local/management/configuration"
22+
echo "Reading middleware configuration from: ${DIR}/middleware-config.json";
2223

23-
http_response=$(curl -k --write-out %{http_code} --output ${TMP_FILE} -XPOST -s \
24+
http_response=$(curl -k --write-out %\{http_code\} --output "${TMP_FILE}" -XPOST -s \
2425
-u management:secret \
2526
-H "Accept: application/json" \
2627
-H "Content-type: application/json" \
27-
-d @middleware-config.json \
28+
-d "@${DIR}/middleware-config.json" \
2829
https://middleware.dev.openconext.local/management/configuration)
30+
res=$?
2931

30-
output=$(cat ${TMP_FILE})
31-
rm ${TMP_FILE}
32-
echo $output
32+
output=$(cat "${TMP_FILE}")
33+
rm "${TMP_FILE}"
34+
echo "$output"
3335

34-
res=$?
3536
if [ $res -ne "0" ]; then
3637
error_exit "Curl failed with code $res"
3738
fi
@@ -43,8 +44,8 @@ fi
4344

4445
# On success JSON output should start with: {"status":"OK"
4546
ok_count=$(echo "${output}" | grep -c "status")
46-
if [ $ok_count -ne "1" ]; then
47+
if [ "$ok_count" -ne "1" ]; then
4748
error_exit "Expected one JSON \"status: OK\" in response, found $ok_count"
4849
fi
4950

50-
echo "OK. New config pushed"%
51+
echo "OK. New config pushed"

stepup/middleware/middleware-push-institution.sh

100644100755
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/bin/bash
22

3-
CWD=$(pwd)
3+
# Get this script's directory
4+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
45

56
function error_exit {
67
echo "${1}"
7-
if [ -n "${TMP_FILE}" -a -d "${TMP_FILE}" ]; then
8+
if [ -n "${TMP_FILE}" ] && [ -d "${TMP_FILE}" ]; then
89
rm "${TMP_FILE}"
910
fi
10-
cd ${CWD}
1111
exit 1
1212
}
1313

@@ -19,19 +19,20 @@ if [ $? -ne "0" ]; then
1919
fi
2020

2121
echo "Pushing new institution configuration to: https://middleware.dev.openconext.local/management/institution-configuration"
22+
echo "Reading institution configuration from: ${DIR}/middleware-institution.json";
2223

23-
http_response=$(curl -k --write-out %{http_code} --output ${TMP_FILE} -XPOST -s \
24+
http_response=$(curl -k --write-out %\{http_code\} --output "${TMP_FILE}" -XPOST -s \
2425
-u management:secret \
2526
-H "Accept: application/json" \
2627
-H "Content-type: application/json" \
27-
-d @middleware-institution.json \
28+
-d "@${DIR}/middleware-institution.json" \
2829
https://middleware.dev.openconext.local/management/institution-configuration)
30+
res=$?
2931

30-
output=$(cat ${TMP_FILE})
31-
rm ${TMP_FILE}
32-
echo $output
32+
output=$(cat "${TMP_FILE}")
33+
rm "${TMP_FILE}"
34+
echo "$output"
3335

34-
res=$?
3536
if [ $res -ne "0" ]; then
3637
error_exit "Curl failed with code $res"
3738
fi
@@ -43,7 +44,7 @@ fi
4344

4445
# On success JSON output should start with: {"status":"OK"
4546
ok_count=$(echo "${output}" | grep -c "status")
46-
if [ $ok_count -ne "1" ]; then
47+
if [ "$ok_count" -ne "1" ]; then
4748
error_exit "Expected one JSON \"status: OK\" in response, found $ok_count"
4849
fi
4950

stepup/middleware/middleware-push-whitelist.sh

100644100755
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/bin/bash
22

3-
CWD=$(pwd)
3+
# Get this script's directory
4+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
45

56
function error_exit {
67
echo "${1}"
7-
if [ -n "${TMP_FILE}" -a -d "${TMP_FILE}" ]; then
8+
if [ -n "${TMP_FILE}" ] && [ -d "${TMP_FILE}" ]; then
89
rm "${TMP_FILE}"
910
fi
10-
cd ${CWD}
1111
exit 1
1212
}
1313

@@ -20,17 +20,19 @@ fi
2020

2121
echo "Pushing new institution whitelist to: http://middleware.dev.openconext.local/management/whitelist/replace"
2222

23-
http_response=$(curl -k --write-out %{http_code} --output ${TMP_FILE} -XPOST -s \
23+
echo "${DIR}/middleware-whitelist.json";
24+
25+
http_response=$(curl -k --write-out %\{http_code\} --output "${TMP_FILE}" -XPOST -s \
2426
-u management:secret -H "Accept: application/json" \
2527
-H "Content-type: application/json" \
26-
-d @middleware-whitelist.json \
28+
-d "@${DIR}/middleware-whitelist.json" \
2729
https://middleware.dev.openconext.local/management/whitelist/replace)
30+
res=$?
2831

29-
output=$(cat ${TMP_FILE})
30-
rm ${TMP_FILE}
31-
echo $output
32+
output=$(cat "${TMP_FILE}")
33+
rm "${TMP_FILE}"
34+
echo "$output"
3235

33-
res=$?
3436
if [ $res -ne "0" ]; then
3537
error_exit "Curl failed with code $res"
3638
fi
@@ -42,7 +44,7 @@ fi
4244

4345
# On success JSON output should start with: {"status":"OK"
4446
ok_count=$(echo "${output}" | grep -c "status")
45-
if [ $ok_count -ne "1" ]; then
47+
if [ "$ok_count" -ne "1" ]; then
4648
error_exit "Expected one JSON \"status: OK\" in response, found $ok_count"
4749
fi
4850

0 commit comments

Comments
 (0)