From cd8e70df17fc4960c230dd5cd514eec53df55e34 Mon Sep 17 00:00:00 2001 From: Anagh Hegde Date: Wed, 25 Sep 2024 18:03:09 +0530 Subject: [PATCH 1/5] create appsmith schema for the pg branch with user and password --- deploy/docker/fs/opt/appsmith/entrypoint.sh | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/deploy/docker/fs/opt/appsmith/entrypoint.sh b/deploy/docker/fs/opt/appsmith/entrypoint.sh index 3d73af5b391a..939dd878a36c 100644 --- a/deploy/docker/fs/opt/appsmith/entrypoint.sh +++ b/deploy/docker/fs/opt/appsmith/entrypoint.sh @@ -435,6 +435,48 @@ init_postgres() { runEmbeddedPostgres=0 fi + # Create the appsmith schema + echo "Initializing PostgreSQL..." + + # Extract credentials from APPSMITH_DB_URL + if [[ -n "$APPSMITH_DB_URL" ]]; then + # Extract username, password, host, port, and database name + DB_PROTOCOL=$(echo "$APPSMITH_DB_URL" | awk -F'://' '{print $1}') + if [[ "$DB_PROTOCOL" == "postgres" ]]; then + DB_USER=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://\([^:]*\):.*#\1#p') + DB_PASSWORD=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^:]*:\([^@]*\)@.*#\1#p') + DB_HOST=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@\([^:]*\):.*#\1#p') + DB_PORT=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^:]*:\([^/]*\)/.*#\1#p') + DB_NAME=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^/]*/\([^?]*\).*#\1#p') + fi + fi + + # Generate a random password if DB_USER or DB_PASSWORD are empty + if [[ -z "$DB_USER" || -z "$DB_PASSWORD" ]]; then + DB_USER="postgres" + DB_PASSWORD=$(openssl rand -base64 12) # Generate a random password + echo "Generated random password for postgres user: $DB_PASSWORD" + fi + + # Set defaults for host, port, and database name if not set + DB_HOST=${DB_HOST:-localhost} + DB_PORT=${DB_PORT:-5432} + DB_NAME=${DB_NAME:-appsmith} + + echo "Connecting to PostgreSQL at $DB_HOST:$DB_PORT with user $DB_USER" + + # Create the appsmith schema if it does not exist + PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME < Date: Thu, 26 Sep 2024 15:35:57 +0530 Subject: [PATCH 2/5] fix EOF issue --- deploy/docker/fs/opt/appsmith/entrypoint.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/deploy/docker/fs/opt/appsmith/entrypoint.sh b/deploy/docker/fs/opt/appsmith/entrypoint.sh index 939dd878a36c..681796faacb1 100644 --- a/deploy/docker/fs/opt/appsmith/entrypoint.sh +++ b/deploy/docker/fs/opt/appsmith/entrypoint.sh @@ -466,9 +466,7 @@ init_postgres() { echo "Connecting to PostgreSQL at $DB_HOST:$DB_PORT with user $DB_USER" # Create the appsmith schema if it does not exist - PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME < Date: Thu, 26 Sep 2024 16:01:27 +0530 Subject: [PATCH 3/5] add postgres url check --- deploy/docker/fs/opt/appsmith/entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy/docker/fs/opt/appsmith/entrypoint.sh b/deploy/docker/fs/opt/appsmith/entrypoint.sh index 681796faacb1..12dabc591016 100644 --- a/deploy/docker/fs/opt/appsmith/entrypoint.sh +++ b/deploy/docker/fs/opt/appsmith/entrypoint.sh @@ -439,7 +439,7 @@ init_postgres() { echo "Initializing PostgreSQL..." # Extract credentials from APPSMITH_DB_URL - if [[ -n "$APPSMITH_DB_URL" ]]; then + if [[ -n "$APPSMITH_DB_URL" && "$APPSMITH_DB_URL" == postgres*://* ]]; then # Extract username, password, host, port, and database name DB_PROTOCOL=$(echo "$APPSMITH_DB_URL" | awk -F'://' '{print $1}') if [[ "$DB_PROTOCOL" == "postgres" ]]; then From b67064d548ea5309a5fbf55cc088412adf509e5d Mon Sep 17 00:00:00 2001 From: Anagh Hegde Date: Thu, 26 Sep 2024 16:27:12 +0530 Subject: [PATCH 4/5] add postgres url check --- deploy/docker/fs/opt/appsmith/entrypoint.sh | 63 +++++++++++---------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/deploy/docker/fs/opt/appsmith/entrypoint.sh b/deploy/docker/fs/opt/appsmith/entrypoint.sh index 12dabc591016..81408ca0b3de 100644 --- a/deploy/docker/fs/opt/appsmith/entrypoint.sh +++ b/deploy/docker/fs/opt/appsmith/entrypoint.sh @@ -436,45 +436,50 @@ init_postgres() { fi # Create the appsmith schema + init_postgres() { echo "Initializing PostgreSQL..." - # Extract credentials from APPSMITH_DB_URL - if [[ -n "$APPSMITH_DB_URL" && "$APPSMITH_DB_URL" == postgres*://* ]]; then - # Extract username, password, host, port, and database name - DB_PROTOCOL=$(echo "$APPSMITH_DB_URL" | awk -F'://' '{print $1}') - if [[ "$DB_PROTOCOL" == "postgres" ]]; then - DB_USER=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://\([^:]*\):.*#\1#p') - DB_PASSWORD=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^:]*:\([^@]*\)@.*#\1#p') - DB_HOST=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@\([^:]*\):.*#\1#p') - DB_PORT=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^:]*:\([^/]*\)/.*#\1#p') - DB_NAME=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^/]*/\([^?]*\).*#\1#p') + # Check if APPSMITH_DB_URL is a PostgreSQL URL + if [[ -n "$APPSMITH_DB_URL" && "$APPSMITH_DB_URL" == postgres*://* ]]; then + echo "APPSMITH_DB_URL is a valid PostgreSQL URL." + + # Extract username, password, host, port, and database name from APPSMITH_DB_URL + DB_USER=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://\([^:]*\):.*#\1#p') + DB_PASSWORD=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^:]*:\([^@]*\)@.*#\1#p') + DB_HOST=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@\([^:]*\):.*#\1#p') + DB_PORT=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^:]*:\([^/]*\)/.*#\1#p') + DB_NAME=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^/]*/\([^?]*\).*#\1#p') + + # Generate a random password if DB_USER or DB_PASSWORD are empty + if [[ -z "$DB_USER" || -z "$DB_PASSWORD" ]]; then + DB_USER="postgres" + DB_PASSWORD=$(openssl rand -base64 12) # Generate a random password + echo "Generated random password for postgres user: $DB_PASSWORD" fi - fi - # Generate a random password if DB_USER or DB_PASSWORD are empty - if [[ -z "$DB_USER" || -z "$DB_PASSWORD" ]]; then - DB_USER="postgres" - DB_PASSWORD=$(openssl rand -base64 12) # Generate a random password - echo "Generated random password for postgres user: $DB_PASSWORD" - fi + # Set defaults for host, port, and database name if not set + DB_HOST=${DB_HOST:-localhost} + DB_PORT=${DB_PORT:-5432} + DB_NAME=${DB_NAME:-appsmith} - # Set defaults for host, port, and database name if not set - DB_HOST=${DB_HOST:-localhost} - DB_PORT=${DB_PORT:-5432} - DB_NAME=${DB_NAME:-appsmith} + echo "Connecting to PostgreSQL at $DB_HOST:$DB_PORT with user $DB_USER" - echo "Connecting to PostgreSQL at $DB_HOST:$DB_PORT with user $DB_USER" + # Execute SQL to create the appsmith schema if it doesn't exist (single line) + PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "CREATE SCHEMA IF NOT EXISTS appsmith;" - # Create the appsmith schema if it does not exist - PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "CREATE SCHEMA IF NOT EXISTS appsmith;" + # Check if the schema creation was successful + if [ $? -eq 0 ]; then + echo "Schema 'appsmith' created or already exists." + else + echo "Failed to create schema 'appsmith'." + exit 1 + fi - if [ $? -eq 0 ]; then - echo "Schema 'appsmith' created or already exists." + echo "PostgreSQL initialization completed." else - echo "Failed to create schema 'appsmith'." - exit 1 + echo "APPSMITH_DB_URL is either empty or not a PostgreSQL URL. Skipping PostgreSQL initialization." fi - + } } safe_init_postgres() { From 973dabfc3946157a70210e5858d5bceac2a6e293 Mon Sep 17 00:00:00 2001 From: Anagh Hegde Date: Thu, 26 Sep 2024 17:14:58 +0530 Subject: [PATCH 5/5] review comments --- deploy/docker/fs/opt/appsmith/entrypoint.sh | 70 ++++++++++----------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/deploy/docker/fs/opt/appsmith/entrypoint.sh b/deploy/docker/fs/opt/appsmith/entrypoint.sh index 81408ca0b3de..31e279c9235b 100644 --- a/deploy/docker/fs/opt/appsmith/entrypoint.sh +++ b/deploy/docker/fs/opt/appsmith/entrypoint.sh @@ -436,50 +436,46 @@ init_postgres() { fi # Create the appsmith schema - init_postgres() { - echo "Initializing PostgreSQL..." - - # Check if APPSMITH_DB_URL is a PostgreSQL URL - if [[ -n "$APPSMITH_DB_URL" && "$APPSMITH_DB_URL" == postgres*://* ]]; then - echo "APPSMITH_DB_URL is a valid PostgreSQL URL." - - # Extract username, password, host, port, and database name from APPSMITH_DB_URL - DB_USER=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://\([^:]*\):.*#\1#p') - DB_PASSWORD=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^:]*:\([^@]*\)@.*#\1#p') - DB_HOST=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@\([^:]*\):.*#\1#p') - DB_PORT=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^:]*:\([^/]*\)/.*#\1#p') - DB_NAME=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^/]*/\([^?]*\).*#\1#p') - - # Generate a random password if DB_USER or DB_PASSWORD are empty - if [[ -z "$DB_USER" || -z "$DB_PASSWORD" ]]; then - DB_USER="postgres" - DB_PASSWORD=$(openssl rand -base64 12) # Generate a random password - echo "Generated random password for postgres user: $DB_PASSWORD" - fi - - # Set defaults for host, port, and database name if not set - DB_HOST=${DB_HOST:-localhost} - DB_PORT=${DB_PORT:-5432} - DB_NAME=${DB_NAME:-appsmith} + echo "Initializing PostgreSQL..." + + # Check if APPSMITH_DB_URL is a PostgreSQL URL + if [[ -n "$APPSMITH_DB_URL" && "$APPSMITH_DB_URL" == postgres*://* ]]; then + echo "APPSMITH_DB_URL is a valid PostgreSQL URL." + # Extract username, password, host, port, and database name from APPSMITH_DB_URL + DB_USER=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://\([^:]*\):.*#\1#p') + DB_PASSWORD=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^:]*:\([^@]*\)@.*#\1#p') + DB_HOST=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@\([^:]*\):.*#\1#p') + DB_PORT=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^:]*:\([^/]*\)/.*#\1#p') + DB_NAME=$(echo "$APPSMITH_DB_URL" | sed -n 's#.*://[^@]*@[^/]*/\([^?]*\).*#\1#p') + + # Generate a random password if DB_USER or DB_PASSWORD are empty + if [[ -z "$DB_USER" || -z "$DB_PASSWORD" ]]; then + DB_USER="appsmith" + DB_PASSWORD=$(openssl rand -base64 12) # Generate a random password + echo "Generated random password for appsmith user: $DB_PASSWORD" + fi - echo "Connecting to PostgreSQL at $DB_HOST:$DB_PORT with user $DB_USER" + # Set defaults for host, port, and database name if not set + DB_HOST=${DB_HOST:-localhost} + DB_PORT=${DB_PORT:-5432} + DB_NAME=${DB_NAME:-appsmith} - # Execute SQL to create the appsmith schema if it doesn't exist (single line) - PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "CREATE SCHEMA IF NOT EXISTS appsmith;" + echo "Connecting to PostgreSQL at $DB_HOST:$DB_PORT with user $DB_USER" - # Check if the schema creation was successful - if [ $? -eq 0 ]; then - echo "Schema 'appsmith' created or already exists." - else - echo "Failed to create schema 'appsmith'." - exit 1 - fi + # Execute SQL to create the appsmith schema if it doesn't exist (single line) + PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "CREATE SCHEMA IF NOT EXISTS appsmith;" - echo "PostgreSQL initialization completed." + # Check if the schema creation was successful + if [ $? -eq 0 ]; then + echo "Schema 'appsmith' created or already exists." else + echo "Failed to create schema 'appsmith'." + exit 1 + fi + echo "PostgreSQL initialization completed." + else echo "APPSMITH_DB_URL is either empty or not a PostgreSQL URL. Skipping PostgreSQL initialization." fi - } } safe_init_postgres() {