Skip to content

Commit

Permalink
DRY functions
Browse files Browse the repository at this point in the history
  • Loading branch information
spatialgeobyte committed Mar 17, 2024
1 parent d9a3df7 commit add4d55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
11 changes: 11 additions & 0 deletions scripts/env-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

}
12 changes: 4 additions & 8 deletions scripts/setup-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}


0 comments on commit add4d55

Please sign in to comment.