From fe8a7ae6d56ec88f22ae8e2f2e08691355205def Mon Sep 17 00:00:00 2001 From: Pieter van der Meulen Date: Thu, 21 Sep 2023 14:51:36 +0200 Subject: [PATCH] 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 --- stepup/init-db.sh | 23 +++++++++++++++++ stepup/middleware/middleware-push-config.sh | 25 ++++++++++--------- .../middleware/middleware-push-institution.sh | 21 ++++++++-------- .../middleware/middleware-push-whitelist.sh | 22 ++++++++-------- 4 files changed, 59 insertions(+), 32 deletions(-) create mode 100755 stepup/init-db.sh mode change 100644 => 100755 stepup/middleware/middleware-push-config.sh mode change 100644 => 100755 stepup/middleware/middleware-push-institution.sh mode change 100644 => 100755 stepup/middleware/middleware-push-whitelist.sh diff --git a/stepup/init-db.sh b/stepup/init-db.sh new file mode 100755 index 0000000..85d531b --- /dev/null +++ b/stepup/init-db.sh @@ -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" \ No newline at end of file diff --git a/stepup/middleware/middleware-push-config.sh b/stepup/middleware/middleware-push-config.sh old mode 100644 new mode 100755 index d452826..056ff9e --- a/stepup/middleware/middleware-push-config.sh +++ b/stepup/middleware/middleware-push-config.sh @@ -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 } @@ -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 @@ -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" diff --git a/stepup/middleware/middleware-push-institution.sh b/stepup/middleware/middleware-push-institution.sh old mode 100644 new mode 100755 index 282c1c4..bee6c25 --- a/stepup/middleware/middleware-push-institution.sh +++ b/stepup/middleware/middleware-push-institution.sh @@ -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 } @@ -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 @@ -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 diff --git a/stepup/middleware/middleware-push-whitelist.sh b/stepup/middleware/middleware-push-whitelist.sh old mode 100644 new mode 100755 index a5402ce..37c06b5 --- a/stepup/middleware/middleware-push-whitelist.sh +++ b/stepup/middleware/middleware-push-whitelist.sh @@ -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 } @@ -20,17 +20,19 @@ fi echo "Pushing new institution whitelist to: http://middleware.dev.openconext.local/management/whitelist/replace" -http_response=$(curl -k --write-out %{http_code} --output ${TMP_FILE} -XPOST -s \ +echo "${DIR}/middleware-whitelist.json"; + +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 @@ -42,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