Skip to content

Commit

Permalink
Merge pull request #36 from vicradon/feat/add-specific-database-confi…
Browse files Browse the repository at this point in the history
…guration

chore: setup specific databases
  • Loading branch information
vicradon authored Jul 19, 2024
2 parents ed178ac + 9e311d8 commit 46cea05
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 55 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
with:
go-version: "1.22.1"
- name: Build the application
run: go build -o learnai_dev
run: go build -o development_app

test:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PORT: ${{ secrets.SSH_PORT }}
SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }}
PROCESS_NAME: run_learnai_dev
PROCESS_NAME: run_development_app

steps:
- name: SSH into server and deploy
Expand Down Expand Up @@ -84,7 +84,7 @@ jobs:
fi
cp app-sample.env app.env
go build -o ~/deployments/development/learnai_dev
go build -o ~/deployments/development/development_app
# Check if pm2 is already running
if pm2 list | grep -q "${{ env.PROCESS_NAME }}"; then
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
with:
go-version: "1.22.1"
- name: Build the application
run: go build -o learnai_prod
run: go build -o production_app

test:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PORT: ${{ secrets.SSH_PORT }}
SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }}
PROCESS_NAME: run_learnai_prod
PROCESS_NAME: run_production_app

steps:
- name: SSH into server and deploy
Expand Down Expand Up @@ -84,7 +84,7 @@ jobs:
fi
cp app-sample.env app.env
go build -o ~/deployments/production/learnai_prod
go build -o ~/deployments/production/production_app
# Check if pm2 is already running
if pm2 list | grep -q "${{ env.PROCESS_NAME }}"; then
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
with:
go-version: "1.22.1"
- name: Build the application
run: go build -o learnai_staging
run: go build -o staging_app

test:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_PORT: ${{ secrets.SSH_PORT }}
SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }}
PROCESS_NAME: run_learnai_staging
PROCESS_NAME: run_staging_app

steps:
- name: SSH into server and deploy
Expand Down Expand Up @@ -84,7 +84,7 @@ jobs:
fi
cp app-sample.env app.env
go build -o ~/deployments/staging/learnai_staging
go build -o ~/deployments/staging/staging_app
# Check if pm2 is already running
if pm2 list | grep -q "${{ env.PROCESS_NAME }}"; then
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN go mod download

COPY . .

RUN go build -o learnai_app main.go
RUN go build -o production_app main.go

FROM alpine:latest

Expand All @@ -21,7 +21,7 @@ RUN addgroup -S nonroot && adduser -S nonroot -G nonroot
WORKDIR /


COPY --from=build-stage learnai_app .
COPY --from=build-stage production_app .


EXPOSE 8080
Expand All @@ -30,4 +30,4 @@ EXPOSE 8080
USER nonroot:nonroot


ENTRYPOINT ["./learnai_app"]
ENTRYPOINT ["./production_app"]
43 changes: 23 additions & 20 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
module.exports = {
apps: [
{
name: 'run_learnai_prod',
script: '/home/vicradon/deployments/production/learnai_prod',
name: "run_production_app",
script: "/home/vicradon/deployments/production/production_app",
env: {
SERVER_PORT: 9000,
DB_NAME: 'db_name',
APP_NAME: 'production',
APP_URL: 'http://localhost:9000'
}
DB_NAME: "production_db",
USERNAME: "production_user",
APP_NAME: "production",
APP_URL: "http://localhost:9000",
},
},
{
name: 'run_learnai_staging',
script: '/home/vicradon/deployments/staging/learnai_staging',
name: "run_staging_app",
script: "/home/vicradon/deployments/staging/staging_app",
env: {
SERVER_PORT: 8000,
DB_NAME: 'db_name',
APP_NAME: 'staging',
APP_URL: 'http://localhost:8000'
}
DB_NAME: "staging_db",
USERNAME: "staging_user",
APP_NAME: "staging",
APP_URL: "http://localhost:8000",
},
},
{
name: 'run_learnai_dev',
script: '/home/vicradon/deployments/development/learnai_dev',
name: "run_development_app",
script: "/home/vicradon/deployments/development/development_app",
env: {
SERVER_PORT: 7000,
DB_NAME: 'db_name',
APP_NAME: 'development',
APP_URL: 'http://localhost:7000'
}
}
]
DB_NAME: "development_db",
USERNAME: "development_user",
APP_NAME: "development",
APP_URL: "http://localhost:7000",
},
},
],
};
3 changes: 3 additions & 0 deletions run_development_app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

~/deployments/development/development_app
3 changes: 0 additions & 3 deletions run_learnai_dev.sh

This file was deleted.

3 changes: 0 additions & 3 deletions run_learnai_prod.sh

This file was deleted.

3 changes: 0 additions & 3 deletions run_learnai_staging.sh

This file was deleted.

3 changes: 3 additions & 0 deletions run_production_app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

~/deployments/production/production_app
3 changes: 3 additions & 0 deletions run_staging_app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

~/deployments/staging/staging_app
40 changes: 26 additions & 14 deletions scripts/setup_postgres.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
#! /bin/bash

# Variables for credentials
DB_USER="postgres"
DB_PASSWORD="password"
DB_NAME="db_name"

# Check if the DB_USER is postgres and alter the password of this user else create a new user
if [ "$DB_USER" == "postgres" ]; then
# Define the databases and users
DATABASES=("development" "staging" "production")
USERS=("development_user" "staging_user" "production_user")

# Check if the script is running as root (necessary for changing PostgreSQL settings)
if [ "$(id -u)" -ne "0" ]; then
echo "This script must be run as root."
exit 1
fi

# Check if the DB_USER is postgres and alter the password of this user if necessary
if echo "${USERS[@]}" | grep -qw "postgres"; then
sudo -i -u postgres psql <<EOF
ALTER USER postgres WITH PASSWORD '$DB_PASSWORD';
EOF
echo "Password for 'postgres' user has been updated to '$DB_PASSWORD'."
else
sudo -i -u postgres psql <<EOF
-- Create a user named '$DB_USER' with password '$DB_PASSWORD'
CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
ALTER USER postgres WITH PASSWORD '$DB_PASSWORD';
EOF
echo "Password for 'postgres' user has been updated."
fi

# Create the database and grant the user access to it
sudo -i -u postgres psql <<EOF
# Create databases and users, and grant permissions
for i in ${!DATABASES[@]}; do
DB_NAME=${DATABASES[$i]}
DB_USER=${USERS[$i]}

sudo -i -u postgres psql <<EOF
-- Create a database named '$DB_NAME'
CREATE DATABASE $DB_NAME;
-- Create a user named '$DB_USER' with password '$DB_PASSWORD'
CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';
-- Grant all privileges on the database '$DB_NAME' to the user '$DB_USER'
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
EOF
echo "Database '$DB_NAME' and user '$DB_USER' created with full access."
done

# Modify pg_hba.conf to allow password authentication
PG_HBA_FILE=$(sudo -i -u postgres psql -t -P format=unaligned -c 'SHOW hba_file')
Expand All @@ -42,4 +54,4 @@ sudo sed -i "s/^#listen_addresses = 'localhost'/listen_addresses = '*'/" $POSTGR
# Restart PostgreSQL to apply changes
sudo systemctl restart postgresql

echo "PostgreSQL setup is complete. User '$DB_USER' with database '$DB_NAME' has been created. The user can connect using the password '$DB_PASSWORD'."
echo "PostgreSQL setup is complete. Databases and users have been created and configured."

0 comments on commit 46cea05

Please sign in to comment.