From add4d5518ccc4882d3e70692c7d35d5340d39336 Mon Sep 17 00:00:00 2001 From: spatialgeobyte <158478685+spatialgeobyte@users.noreply.github.com> Date: Sun, 17 Mar 2024 08:57:16 +0200 Subject: [PATCH] DRY functions --- scripts/env-data.sh | 11 +++++++++++ scripts/setup-user.sh | 12 ++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/scripts/env-data.sh b/scripts/env-data.sh index d6df53d8..122b3d9d 100644 --- a/scripts/env-data.sh +++ b/scripts/env-data.sh @@ -610,6 +610,7 @@ function non_root_permission() { function role_check() { ROLE_NAME=$1 echo "Creating user $1" + echo -e "\e[32m [Entrypoint] Creating/Updating user \e[1;31m $1 \033[0m" RESULT=$(su - postgres -c "psql postgres -t -c \"SELECT 1 FROM pg_roles WHERE rolname = '$ROLE_NAME'\"") COMMAND="ALTER" if [ -z "$RESULT" ]; then @@ -618,3 +619,13 @@ function role_check() { } +function role_creation() { + ROLE_NAME=$1 + ROLE_STATUS=$2 + ROLE_PASS=$3 + STATEMENT="$COMMAND USER \"$ROLE_NAME\" WITH ${ROLE_STATUS} ENCRYPTED PASSWORD '$ROLE_PASS';" + echo "$STATEMENT" > /tmp/setup_user.sql + su - postgres -c "psql postgres -f /tmp/setup_user.sql" + rm /tmp/setup_user.sql + +} \ No newline at end of file diff --git a/scripts/setup-user.sh b/scripts/setup-user.sh index 293fa82c..3427c654 100644 --- a/scripts/setup-user.sh +++ b/scripts/setup-user.sh @@ -7,14 +7,10 @@ source /scripts/env-data.sh # Check user already exists role_check "$POSTGRES_USER" -STATEMENT="$COMMAND USER \"$POSTGRES_USER\" WITH SUPERUSER ENCRYPTED PASSWORD '$POSTGRES_PASS';" -echo "$STATEMENT" > /tmp/setup_superuser.sql -su - postgres -c "psql postgres -f /tmp/setup_superuser.sql" -rm /tmp/setup_superuser.sql +role_creation ${POSTGRES_USER} SUPERUSER $POSTGRES_PASS + role_check "$REPLICATION_USER" -STATEMENT_REPLICATION="$COMMAND USER \"$REPLICATION_USER\" WITH REPLICATION ENCRYPTED PASSWORD '$REPLICATION_PASS';" -echo "$STATEMENT_REPLICATION" > /tmp/setup_replication.sql -su - postgres -c "psql postgres -f /tmp/setup_replication.sql" -rm /tmp/setup_replication.sql +role_creation ${REPLICATION_USER} REPLICATION ${REPLICATION_PASS} +