From 544997a004b751f5b01cf5e257f57659dc2fbed4 Mon Sep 17 00:00:00 2001 From: hoangvv Date: Wed, 15 Jan 2025 10:25:27 +0700 Subject: [PATCH] update --- .gitea/workflows/setup_native.yml | 11 ++++++++--- .gitignore | 3 ++- Makefile | 1 + setup/init_config.sh | 16 +++++++++++----- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.gitea/workflows/setup_native.yml b/.gitea/workflows/setup_native.yml index d3f14d89c..a70723d39 100644 --- a/.gitea/workflows/setup_native.yml +++ b/.gitea/workflows/setup_native.yml @@ -34,9 +34,14 @@ jobs: with: postgresql version: '16' - - name : Test postgres - run: psql postgresql://postgres:postgres@localhost:5432/postgres -c "SELECT 1" - + - name: Make envfile + uses: SpicyPizza/create-envfile@v2.0 + with: + PG_USER: ${{ secrets.PG_USER }} + PG_PASSWORD: ${{ secrets.PG_PASSWORD }} + DB_SERVER: ${{ vars.DB_SERVER }} + DB_PORT: ${{ vars.DB_PORT }} + - name: Generate Config run: make gen_config diff --git a/.gitignore b/.gitignore index 82efcafd1..500426244 100644 --- a/.gitignore +++ b/.gitignore @@ -49,4 +49,5 @@ package.json /man/ /share/ /src/ -/deployment/postgresql/* \ No newline at end of file +/deployment/postgresql/* +venv/* diff --git a/Makefile b/Makefile index 69a5a5007..d5c4841d3 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +include .env .SHELLFLAGS += ${SHELLFLAGS} -e PWD = $(shell pwd) UID = $(shell id -u) diff --git a/setup/init_config.sh b/setup/init_config.sh index 556f7d0cc..2500372d0 100755 --- a/setup/init_config.sh +++ b/setup/init_config.sh @@ -192,24 +192,30 @@ Generate_Config_Docker(){ Show 0 " Generate Config Complete" } Generate_Config_Native(){ + # Load environment variables from .env file + if [ -f .env ]; then + export $(grep -v '^#' .env | xargs) + else + Show 1 ".env file not found!" + exit 1 + fi ADDONS=${1:-} REPO_NAME=$(basename "$(git rev-parse --show-toplevel)" | sed -E 's/[.-]/_/g') USER="${REPO_NAME:-"default_repo"}" - DB_PORT=$(sudo -u postgres psql -t -c "SHOW port;" | awk '{print $1}' || echo 5432) PASSWORD="$(openssl rand -hex 24)" # Check if the user already exists - USER_EXISTS=$(sudo -u postgres psql -t -c "SELECT 1 FROM pg_roles WHERE rolname='$USER';") + USER_EXISTS=$(psql "postgresql://${PG_USER}:${PG_PASSWORD}@${DB_SERVER}:${DB_PORT}/postgres" -c "SELECT 1 FROM pg_roles WHERE rolname='$USER';") if [ -z "$USER_EXISTS" ]; then # User does not exist, create the user Show 2 "Create the new PostgreSQL username: $USER with password: $PASSWORD" - sudo -u postgres psql -c "CREATE USER $USER WITH PASSWORD '$PASSWORD';" + psql "postgresql://${PG_USER}:${PG_PASSWORD}@${DB_SERVER}:${DB_PORT}/postgres" -c "CREATE USER $USER WITH PASSWORD '$PASSWORD';" Show 2 " Grant $USER superuser (admin) privileges" - sudo -u postgres psql -c "ALTER USER $USER WITH SUPERUSER;" + psql "postgresql://${PG_USER}:${PG_PASSWORD}@${DB_SERVER}:${DB_PORT}/postgres" -c "ALTER USER $USER WITH SUPERUSER;" else # User exists, update the password Show 2 "User $USER already exists, updating password to $PASSWORD" - sudo -u postgres psql -c "ALTER USER $USER WITH PASSWORD '$PASSWORD';" + psql "postgresql://${PG_USER}:${PG_PASSWORD}@${DB_SERVER}:${DB_PORT}/postgres" -c "ALTER USER $USER WITH PASSWORD '$PASSWORD';" fi ${PYTHON} "$SETUP_PATH/gen_config.py" --db_user $USER --db_pass $PASSWORD --deploy_path "$(pwd)" \ --addons_path $ADDONS --db_port $DB_PORT