diff --git a/.forgejo/workflows/beta.yml b/.forgejo/workflows/beta.yml new file mode 100644 index 0000000..c381d01 --- /dev/null +++ b/.forgejo/workflows/beta.yml @@ -0,0 +1,146 @@ +name: Deploy Beta (NATIVE) + +on: + push: + branches: + - beta + +jobs: + deploy: + runs-on: ubuntu-latest + container: + image: node:22-alpine + steps: + - name: Install git + run: apk add --no-cache git + + - name: Checkout Code + uses: https://github.com/actions/checkout@v4 + + - name: Generate CloudWatch and Nginx Configs Locally + run: | + mkdir -p tmp + cat << 'EOF' > tmp/amazon-cloudwatch-agent.json + { + "agent": { "metrics_collection_interval": 60, "run_as_user": "root" }, + "metrics": { + "metrics_collected": { + "disk": { "measurement": ["used_percent"], "metrics_collection_interval": 60, "resources": ["/"] }, + "mem": { "measurement": ["mem_used_percent"], "metrics_collection_interval": 60 } + } + }, + "logs": { + "logs_collected": { + "files": { + "collect_list": [ + { "file_path": "/home/ubuntu/beta-documentverification/logs/app.log", "log_group_name": "/beta-documentverification/app", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/beta-documentverification/logs/error.log", "log_group_name": "/beta-documentverification/error", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/beta-documentverification/logs/requests.log", "log_group_name": "/beta-documentverification/requests", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/beta-documentverification/logs/gunicorn-access.log", "log_group_name": "/beta-documentverification/gunicorn-access", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/beta-documentverification/logs/gunicorn-error.log", "log_group_name": "/beta-documentverification/gunicorn-error", "log_stream_name": "{instance_id}", "retention_in_days": 30 } + ] + } + } + } + } + EOF + cat << 'EOF' > tmp/nginx-proxy.conf + server { + server_name beta-documentverification.tradhox.com; + + location / { + proxy_pass http://127.0.0.1:8092; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + listen 443 ssl; + ssl_certificate /etc/letsencrypt/live/beta-documentverification.tradhox.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/beta-documentverification.tradhox.com/privkey.pem; + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; + } + + server { + listen 80; + server_name beta-documentverification.tradhox.com; + return 301 https://$host$request_uri; + } + EOF + + - name: Package Application + run: | + tar -czf app.tar.gz --exclude=.git --exclude=tmp --exclude=venv --exclude=db.sqlite3 . + + - name: Deploy to Beta EC2 + run: | + apk add --no-cache openssh-client aws-cli + mkdir -p ~/.ssh + echo "${{ secrets.BETA_EC2_SSH_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + + # Copy app archive and configuration files + scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json tmp/nginx-proxy.conf beta-documentverification.service ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }}:/tmp/ + + # Execute deployment commands on remote server + ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }} << 'EOF' + set -e + + # ── 1. Install Host Dependencies ────────────────────────────── + sudo apt-get update + sudo apt-get install -y python3-pip python3-venv python3-dev libpq-dev build-essential nginx certbot python3-certbot-nginx + + # ── 2. Extract codebase to target directory ─────────────────── + sudo mkdir -p /home/ubuntu/beta-documentverification + sudo chown -R ubuntu:ubuntu /home/ubuntu/beta-documentverification + tar -xzf /tmp/app.tar.gz -C /home/ubuntu/beta-documentverification + + # ── 3. Set up python virtual environment & dependencies ──────── + cd /home/ubuntu/beta-documentverification + mkdir -p logs + python3 -m venv venv + ./venv/bin/pip install --upgrade pip + ./venv/bin/pip install -r requirements.txt + + # ── 4. Configure environment variables ───────────────────────── + echo "DEBUG=0" > .env + echo "ENVIRONMENT=beta" >> .env + echo "DB_USER=vignesh" >> .env + echo "DB_PASSWORD=vignesh_secret_pass_2026" >> .env + echo "DB_HOST=16.113.106.152" >> .env + echo "DB_PORT=5432" >> .env + echo "DB_NAME=betadocumentverifcation" >> .env + + # ── 5. Run Database Migrations ───────────────────────────────── + ./venv/bin/python manage.py migrate --noinput + + # ── 6. Configure and restart Systemd service ─────────────────── + sudo mv /tmp/beta-documentverification.service /etc/systemd/system/beta-documentverification.service + sudo systemctl daemon-reload + sudo systemctl enable beta-documentverification + sudo systemctl restart beta-documentverification + + # ── 7. Configure CloudWatch Agent ────────────────────────────── + if [ -d "/opt/aws/amazon-cloudwatch-agent/etc" ]; then + sudo mv /tmp/amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json + sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \ + -a fetch-config -m ec2 -s \ + -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json || true + fi + + # ── 8. Configure Nginx Reverse Proxy & SSL Cert ──────────────── + sudo mv /tmp/nginx-proxy.conf /etc/nginx/sites-available/beta-documentverification + sudo ln -sf /etc/nginx/sites-available/beta-documentverification /etc/nginx/sites-enabled/beta-documentverification + sudo rm -f /etc/nginx/sites-enabled/default + + if [ ! -d "/etc/letsencrypt/live/beta-documentverification.tradhox.com" ]; then + sudo certbot certonly --nginx -d beta-documentverification.tradhox.com --non-interactive --agree-tos --register-unsafely-without-email || true + fi + + sudo systemctl restart nginx || true + + # ── 9. Cleanup tmp files ─────────────────────────────────────── + rm -f /tmp/app.tar.gz + EOF diff --git a/.forgejo/workflows/main.yml b/.forgejo/workflows/main.yml new file mode 100644 index 0000000..fd1054a --- /dev/null +++ b/.forgejo/workflows/main.yml @@ -0,0 +1,146 @@ +name: Deploy Main Production (NATIVE) + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + container: + image: node:22-alpine + steps: + - name: Install git + run: apk add --no-cache git + + - name: Checkout Code + uses: https://github.com/actions/checkout@v4 + + - name: Generate CloudWatch and Nginx Configs Locally + run: | + mkdir -p tmp + cat << 'EOF' > tmp/amazon-cloudwatch-agent.json + { + "agent": { "metrics_collection_interval": 60, "run_as_user": "root" }, + "metrics": { + "metrics_collected": { + "disk": { "measurement": ["used_percent"], "metrics_collection_interval": 60, "resources": ["/"] }, + "mem": { "measurement": ["mem_used_percent"], "metrics_collection_interval": 60 } + } + }, + "logs": { + "logs_collected": { + "files": { + "collect_list": [ + { "file_path": "/home/ubuntu/documentverification/logs/app.log", "log_group_name": "/documentverification/app", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/documentverification/logs/error.log", "log_group_name": "/documentverification/error", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/documentverification/logs/requests.log", "log_group_name": "/documentverification/requests", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/documentverification/logs/gunicorn-access.log", "log_group_name": "/documentverification/gunicorn-access", "log_stream_name": "{instance_id}", "retention_in_days": 30 }, + { "file_path": "/home/ubuntu/documentverification/logs/gunicorn-error.log", "log_group_name": "/documentverification/gunicorn-error", "log_stream_name": "{instance_id}", "retention_in_days": 30 } + ] + } + } + } + } + EOF + cat << 'EOF' > tmp/nginx-proxy.conf + server { + server_name documentverification.tradhox.com; + + location / { + proxy_pass http://127.0.0.1:8093; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + + listen 443 ssl; + ssl_certificate /etc/letsencrypt/live/documentverification.tradhox.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/documentverification.tradhox.com/privkey.pem; + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; + } + + server { + listen 80; + server_name documentverification.tradhox.com; + return 301 https://$host$request_uri; + } + EOF + + - name: Package Application + run: | + tar -czf app.tar.gz --exclude=.git --exclude=tmp --exclude=venv --exclude=db.sqlite3 . + + - name: Deploy to Production EC2 + run: | + apk add --no-cache openssh-client aws-cli + mkdir -p ~/.ssh + echo "${{ secrets.PROD_EC2_SSH_KEY || secrets.BETA_EC2_SSH_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + + # Copy app archive and configuration files + scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json tmp/nginx-proxy.conf documentverification.service ${{ secrets.PROD_EC2_USERNAME || secrets.BETA_EC2_USERNAME }}@${{ secrets.PROD_EC2_HOST || secrets.BETA_EC2_HOST }}:/tmp/ + + # Execute deployment commands on remote server + ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.PROD_EC2_USERNAME || secrets.BETA_EC2_USERNAME }}@${{ secrets.PROD_EC2_HOST || secrets.BETA_EC2_HOST }} << 'EOF' + set -e + + # ── 1. Install Host Dependencies ────────────────────────────── + sudo apt-get update + sudo apt-get install -y python3-pip python3-venv python3-dev libpq-dev build-essential nginx certbot python3-certbot-nginx + + # ── 2. Extract codebase to target directory ─────────────────── + sudo mkdir -p /home/ubuntu/documentverification + sudo chown -R ubuntu:ubuntu /home/ubuntu/documentverification + tar -xzf /tmp/app.tar.gz -C /home/ubuntu/documentverification + + # ── 3. Set up python virtual environment & dependencies ──────── + cd /home/ubuntu/documentverification + mkdir -p logs + python3 -m venv venv + ./venv/bin/pip install --upgrade pip + ./venv/bin/pip install -r requirements.txt + + # ── 4. Configure environment variables ───────────────────────── + echo "DEBUG=0" > .env + echo "ENVIRONMENT=production" >> .env + echo "DB_USER=vignesh" >> .env + echo "DB_PASSWORD=vignesh_secret_pass_2026" >> .env + echo "DB_HOST=16.113.106.152" >> .env + echo "DB_PORT=5432" >> .env + echo "DB_NAME=documentverifcation" >> .env + + # ── 5. Run Database Migrations ───────────────────────────────── + ./venv/bin/python manage.py migrate --noinput + + # ── 6. Configure and restart Systemd service ─────────────────── + sudo mv /tmp/documentverification.service /etc/systemd/system/documentverification.service + sudo systemctl daemon-reload + sudo systemctl enable documentverification + sudo systemctl restart documentverification + + # ── 7. Configure CloudWatch Agent ────────────────────────────── + if [ -d "/opt/aws/amazon-cloudwatch-agent/etc" ]; then + sudo mv /tmp/amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json + sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \ + -a fetch-config -m ec2 -s \ + -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json || true + fi + + # ── 8. Configure Nginx Reverse Proxy & SSL Cert ──────────────── + sudo mv /tmp/nginx-proxy.conf /etc/nginx/sites-available/documentverification + sudo ln -sf /etc/nginx/sites-available/documentverification /etc/nginx/sites-enabled/documentverification + sudo rm -f /etc/nginx/sites-enabled/default + + if [ ! -d "/etc/letsencrypt/live/documentverification.tradhox.com" ]; then + sudo certbot certonly --nginx -d documentverification.tradhox.com --non-interactive --agree-tos --register-unsafely-without-email || true + fi + + sudo systemctl restart nginx || true + + # ── 9. Cleanup tmp files ─────────────────────────────────────── + rm -f /tmp/app.tar.gz + EOF diff --git a/beta-documentverification.service b/beta-documentverification.service new file mode 100644 index 0000000..426ebc2 --- /dev/null +++ b/beta-documentverification.service @@ -0,0 +1,14 @@ +[Unit] +Description=Beta Document Verification Backend Service +After=network.target + +[Service] +User=ubuntu +Group=ubuntu +WorkingDirectory=/home/ubuntu/beta-documentverification +ExecStart=/home/ubuntu/beta-documentverification/venv/bin/gunicorn --workers 3 --bind 127.0.0.1:8092 docverify.wsgi:application +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target diff --git a/documentverification.service b/documentverification.service new file mode 100644 index 0000000..0064322 --- /dev/null +++ b/documentverification.service @@ -0,0 +1,14 @@ +[Unit] +Description=Document Verification Main Production Backend Service +After=network.target + +[Service] +User=ubuntu +Group=ubuntu +WorkingDirectory=/home/ubuntu/documentverification +ExecStart=/home/ubuntu/documentverification/venv/bin/gunicorn --workers 3 --bind 127.0.0.1:8093 docverify.wsgi:application +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target diff --git a/docverify/settings.py b/docverify/settings.py index 3846d9e..edd96e1 100644 --- a/docverify/settings.py +++ b/docverify/settings.py @@ -58,10 +58,16 @@ TEMPLATES = [ WSGI_APPLICATION = 'docverify.wsgi.application' +# PostgreSQL Database Configuration DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': os.environ.get('DB_NAME', 'documentverifcation'), + 'USER': os.environ.get('DB_USER', 'vignesh'), + 'PASSWORD': os.environ.get('DB_PASSWORD', 'vignesh_secret_pass_2026'), + 'HOST': os.environ.get('DB_HOST', '16.113.106.152'), + 'PORT': os.environ.get('DB_PORT', '5432'), + 'CONN_MAX_AGE': 60, } } diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1a576d3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +Django>=5.2 +djangorestframework>=3.14.0 +requests>=2.31.0 +python-dotenv>=1.0.0 +psycopg2-binary>=2.9.0 +gunicorn>=21.2.0