Skip to content

Commit

Permalink
Add script to do all the (first time) database initialisation
Browse files Browse the repository at this point in the history
The script vcan be safely run multiple times, e.g. to upgrade the database schema for new versions, or to update the middleware configuration
  • Loading branch information
pmeulen committed Sep 21, 2023
1 parent 414361e commit 78f380c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
23 changes: 23 additions & 0 deletions stepup/init-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -e

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

echo "Initializing middleware and gateway databases"
docker compose exec middleware /var/www/html/bin/console doctrine:migrations:migrate --env=prod --em=deploy --no-interaction

echo "Initializing webauthn database"
docker compose exec webauthn /var/www/html/bin/console doctrine:migrations:migrate --env=prod --no-interaction

echo "Pushing middleware configuration"
sh "${DIR}/middleware/middleware-push-config.sh"

echo "Pushing middleware whitelist"
sh "${DIR}/middleware/middleware-push-whitelist.sh"

echo "Pushing middleware institution configuration"
sh "${DIR}/middleware/middleware-push-institution.sh"

echo "Done"
25 changes: 13 additions & 12 deletions stepup/middleware/middleware-push-config.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash

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

function error_exit {
echo "${1}"
if [ -n "${TMP_FILE}" -a -d "${TMP_FILE}" ]; then
if [ -n "${TMP_FILE}" ] && [ -d "${TMP_FILE}" ]; then
rm "${TMP_FILE}"
fi
cd ${CWD}
exit 1
}

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

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

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

output=$(cat ${TMP_FILE})
rm ${TMP_FILE}
echo $output
output=$(cat "${TMP_FILE}")
rm "${TMP_FILE}"
echo "$output"

res=$?
if [ $res -ne "0" ]; then
error_exit "Curl failed with code $res"
fi
Expand All @@ -43,8 +44,8 @@ fi

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

echo "OK. New config pushed"%
echo "OK. New config pushed"
21 changes: 11 additions & 10 deletions stepup/middleware/middleware-push-institution.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash

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

function error_exit {
echo "${1}"
if [ -n "${TMP_FILE}" -a -d "${TMP_FILE}" ]; then
if [ -n "${TMP_FILE}" ] && [ -d "${TMP_FILE}" ]; then
rm "${TMP_FILE}"
fi
cd ${CWD}
exit 1
}

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

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

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

output=$(cat ${TMP_FILE})
rm ${TMP_FILE}
echo $output
output=$(cat "${TMP_FILE}")
rm "${TMP_FILE}"
echo "$output"

res=$?
if [ $res -ne "0" ]; then
error_exit "Curl failed with code $res"
fi
Expand All @@ -43,7 +44,7 @@ fi

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

Expand Down
21 changes: 11 additions & 10 deletions stepup/middleware/middleware-push-whitelist.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash

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

function error_exit {
echo "${1}"
if [ -n "${TMP_FILE}" -a -d "${TMP_FILE}" ]; then
if [ -n "${TMP_FILE}" ] && [ -d "${TMP_FILE}" ]; then
rm "${TMP_FILE}"
fi
cd ${CWD}
exit 1
}

Expand All @@ -19,18 +19,19 @@ if [ $? -ne "0" ]; then
fi

echo "Pushing new institution whitelist to: http://middleware.dev.openconext.local/management/whitelist/replace"
echo "Reading whitelist from: ${DIR}/middleware-whitelist.json";

http_response=$(curl -k --write-out %{http_code} --output ${TMP_FILE} -XPOST -s \
http_response=$(curl -k --write-out %\{http_code\} --output "${TMP_FILE}" -XPOST -s \
-u management:secret -H "Accept: application/json" \
-H "Content-type: application/json" \
-d @middleware-whitelist.json \
-d "@${DIR}/middleware-whitelist.json" \
https://middleware.dev.openconext.local/management/whitelist/replace)
res=$?

output=$(cat ${TMP_FILE})
rm ${TMP_FILE}
echo $output
output=$(cat "${TMP_FILE}")
rm "${TMP_FILE}"
echo "$output"

res=$?
if [ $res -ne "0" ]; then
error_exit "Curl failed with code $res"
fi
Expand All @@ -42,7 +43,7 @@ fi

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

Expand Down

0 comments on commit 78f380c

Please sign in to comment.