From 6e95a0c1a9e9e9e49c5994871acc0997b1bac505 Mon Sep 17 00:00:00 2001 From: Boris D Date: Tue, 14 Oct 2025 14:13:10 +0300 Subject: [PATCH] chore: Refactor deployment scripts and workflows for improved clarity and efficiency --- .gitea/workflows/deploy-testing.yml | 254 +++--------------------- .gitea/workflows/test.yml | 18 -- DEPLOYMENT.md | 209 -------------------- docker-compose.yml | 34 ++-- scripts/setup-docker.sh | 37 ++++ scripts/setup-testing-server.sh | 293 ---------------------------- 6 files changed, 80 insertions(+), 765 deletions(-) delete mode 100644 .gitea/workflows/test.yml delete mode 100644 DEPLOYMENT.md create mode 100644 scripts/setup-docker.sh delete mode 100755 scripts/setup-testing-server.sh diff --git a/.gitea/workflows/deploy-testing.yml b/.gitea/workflows/deploy-testing.yml index 943407c..d8fa6c3 100644 --- a/.gitea/workflows/deploy-testing.yml +++ b/.gitea/workflows/deploy-testing.yml @@ -1,247 +1,53 @@ name: Deploy to Testing Server on: - pull_request: + push: branches: - develop - types: [opened, synchronize, reopened] + workflow_dispatch: jobs: deploy: - name: Deploy to Testing Server runs-on: ubuntu-latest - if: github.event.pull_request.merged == false # Only for open PRs + name: Deploy to Testing Environment steps: - name: Checkout code - uses: actions/checkout@v4 - with: - fetch-depth: 0 + uses: actions/checkout@v3 - - name: Setup Node.js - uses: actions/setup-node@v4 - with: - node-version: "18" - cache: "yarn" - - - name: Install dependencies - run: yarn install --frozen-lockfile - - - name: Run linting - run: yarn lint:check - - - name: Build application - run: yarn build - - - name: Build Docker image - run: | - docker build -t low-code-engine:testing-${{ github.event.pull_request.number }} . - - - name: Save Docker image - run: | - docker save low-code-engine:testing-${{ github.event.pull_request.number }} | gzip > low-code-engine-testing.tar.gz - - - name: Deploy to Testing Server + - name: Deploy via SSH uses: appleboy/ssh-action@v1.0.3 with: - host: ${{ secrets.TESTING_SERVER_HOST }} - username: ${{ secrets.TESTING_SERVER_USER }} - key: ${{ secrets.TESTING_SERVER_SSH_KEY }} - port: ${{ secrets.TESTING_SERVER_PORT || 22 }} + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + port: ${{ secrets.SSH_PORT || 22 }} script: | - # Create application directory if it doesn't exist - mkdir -p /opt/low-code-engine/testing-pr-${{ github.event.pull_request.number }} - cd /opt/low-code-engine/testing-pr-${{ github.event.pull_request.number }} + # Navigate to project directory + cd ${{ secrets.PROJECT_PATH }} - # Stop existing containers if they exist - docker-compose down || true + # Pull latest code + echo "πŸ”„ Pulling latest code from repository..." + git pull origin develop - # Remove old images - docker image prune -f || true + # Install dependencies + echo "πŸ“¦ Installing dependencies with yarn..." + yarn install --frozen-lockfile - - name: Copy files to server - uses: appleboy/scp-action@v0.1.7 - with: - host: ${{ secrets.TESTING_SERVER_HOST }} - username: ${{ secrets.TESTING_SERVER_USER }} - key: ${{ secrets.TESTING_SERVER_SSH_KEY }} - port: ${{ secrets.TESTING_SERVER_PORT || 22 }} - source: "low-code-engine-testing.tar.gz,docker-compose.yml,docker/" - target: "/opt/low-code-engine/testing-pr-${{ github.event.pull_request.number }}/" + # Run database migrations + echo "πŸ—„οΈ Running database migrations..." + yarn migration:run - - name: Load and run Docker containers - uses: appleboy/ssh-action@v1.0.3 - with: - host: ${{ secrets.TESTING_SERVER_HOST }} - username: ${{ secrets.TESTING_SERVER_USER }} - key: ${{ secrets.TESTING_SERVER_SSH_KEY }} - port: ${{ secrets.TESTING_SERVER_PORT || 22 }} - script: | - cd /opt/low-code-engine/testing-pr-${{ github.event.pull_request.number }} + # Build the project + echo "πŸ—οΈ Building the project..." + yarn build - # Load Docker image - gunzip -c low-code-engine-testing.tar.gz | docker load + # Restart PM2 process + echo "πŸ”„ Restarting PM2 application..." + pm2 restart ${{ secrets.PM2_APP_NAME || 'low-code-engine' }} - # Create .env file for testing environment - cat > .env << EOF - NODE_ENV=testing - DB_ROOT_PASSWORD=${{ secrets.TESTING_DB_ROOT_PASSWORD }} - DB_DATABASE=low_code_engine_pr_${{ github.event.pull_request.number }} - DB_USERNAME=${{ secrets.TESTING_DB_USERNAME }} - DB_PASSWORD=${{ secrets.TESTING_DB_PASSWORD }} - DB_PORT=3306 - APP_PORT=${{ vars.TESTING_BASE_PORT || 3000 }}${{ github.event.pull_request.number }} - REDIS_HOST=redis - REDIS_PORT=6379 - EOF + # Show PM2 status + echo "βœ… Deployment completed! PM2 status:" + pm2 status - # Create docker-compose.override.yml for testing environment - cat > docker-compose.override.yml << EOF - version: "3.8" - services: - app: - image: low-code-engine:testing-${{ github.event.pull_request.number }} - ports: - - "\${APP_PORT}:3000" - environment: - NODE_ENV: testing - DB_HOST: mariadb - DB_PORT: 3306 - DB_USERNAME: \${DB_USERNAME} - DB_PASSWORD: \${DB_PASSWORD} - DB_DATABASE: \${DB_DATABASE} - REDIS_HOST: redis - REDIS_PORT: 6379 - - mariadb: - ports: - - "${{ vars.TESTING_BASE_DB_PORT || 3306 }}${{ github.event.pull_request.number }}:3306" - environment: - MYSQL_DATABASE: \${DB_DATABASE} - - redis: - image: redis:7-alpine - ports: - - "${{ vars.TESTING_BASE_REDIS_PORT || 6379 }}${{ github.event.pull_request.number }}:6379" - EOF - - # Start containers - docker-compose up -d - - # Wait for database to start - sleep 30 - - # Run migrations - docker-compose exec -T app yarn migration:run || true - - # Check container status - docker-compose ps - - - name: Health check - uses: appleboy/ssh-action@v1.0.3 - with: - host: ${{ secrets.TESTING_SERVER_HOST }} - username: ${{ secrets.TESTING_SERVER_USER }} - key: ${{ secrets.TESTING_SERVER_SSH_KEY }} - port: ${{ secrets.TESTING_SERVER_PORT || 22 }} - script: | - cd /opt/low-code-engine/testing-pr-${{ github.event.pull_request.number }} - - # Check application availability - APP_PORT=${{ vars.TESTING_BASE_PORT || 3000 }}${{ github.event.pull_request.number }} - - for i in {1..10}; do - if curl -f http://localhost:$APP_PORT/health > /dev/null 2>&1; then - echo "βœ… Application is healthy on port $APP_PORT" - break - fi - echo "⏳ Waiting for application to start... (attempt $i/10)" - sleep 10 - done - - - name: Comment PR with deployment info - uses: actions/github-script@v7 - with: - script: | - const prNumber = context.payload.pull_request.number; - const appPort = ${{ vars.TESTING_BASE_PORT || 3000 }} + prNumber; - const dbPort = ${{ vars.TESTING_BASE_DB_PORT || 3306 }} + prNumber; - const redisPort = ${{ vars.TESTING_BASE_REDIS_PORT || 6379 }} + prNumber; - - const comment = `## πŸš€ Testing Deployment - - Your PR has been deployed to the testing server! - - **Deployment Details:** - - 🌐 Application URL: http://${{ secrets.TESTING_SERVER_HOST }}:${appPort} - - πŸ—„οΈ Database Port: ${dbPort} - - πŸ”΄ Redis Port: ${redisPort} - - πŸ“ Server Path: \`/opt/low-code-engine/testing-pr-${prNumber}\` - - **Available Commands on Server:** - \`\`\`bash - cd /opt/low-code-engine/testing-pr-${prNumber} - docker-compose logs app # View application logs - docker-compose logs mariadb # View database logs - docker-compose ps # Check container status - docker-compose exec app yarn migration:run # Run migrations - \`\`\` - - > **Note:** This deployment will be automatically cleaned up when the PR is closed or merged. - `; - - github.rest.issues.createComment({ - issue_number: prNumber, - owner: context.repo.owner, - repo: context.repo.repo, - body: comment - }); - - cleanup: - name: Cleanup on PR Close - runs-on: ubuntu-latest - if: github.event.pull_request.state == 'closed' - - steps: - - name: Cleanup testing environment - uses: appleboy/ssh-action@v1.0.3 - with: - host: ${{ secrets.TESTING_SERVER_HOST }} - username: ${{ secrets.TESTING_SERVER_USER }} - key: ${{ secrets.TESTING_SERVER_SSH_KEY }} - port: ${{ secrets.TESTING_SERVER_PORT || 22 }} - script: | - cd /opt/low-code-engine/testing-pr-${{ github.event.pull_request.number }} - - # Stop and remove containers - docker-compose down -v || true - - # Remove Docker image - docker rmi low-code-engine:testing-${{ github.event.pull_request.number }} || true - - # Remove deployment directory - cd .. - rm -rf testing-pr-${{ github.event.pull_request.number }} - - echo "βœ… Cleanup completed for PR #${{ github.event.pull_request.number }}" - - - name: Comment PR with cleanup info - uses: actions/github-script@v7 - with: - script: | - const comment = `## 🧹 Testing Environment Cleaned Up - - The testing deployment for this PR has been cleaned up: - - βœ… Docker containers stopped and removed - - βœ… Docker images cleaned up - - βœ… Server files removed - - Thank you for testing! πŸŽ‰ - `; - - github.rest.issues.createComment({ - issue_number: context.payload.pull_request.number, - owner: context.repo.owner, - repo: context.repo.repo, - body: comment - }); + echo "πŸš€ Application deployed successfully!" diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml deleted file mode 100644 index a0008fe..0000000 --- a/.gitea/workflows/test.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: Test Runner - -on: - push: - branches: - - main - - develop - -jobs: - hello-world: - runs-on: [ubuntu-latest] - steps: - - name: Test multiple commands - run: | - echo "Step 1 complete βœ…" - echo "Step 2 complete βœ…" - echo "All good!" - echo "Tests passed! πŸŽ‰" diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md deleted file mode 100644 index 053036f..0000000 --- a/DEPLOYMENT.md +++ /dev/null @@ -1,209 +0,0 @@ -# GitHub Actions Deployment Setup - -This document describes the setup for automatic deployment to a testing server when creating a Pull Request to the `develop` branch. - -## Required GitHub Secrets - -Go to repository settings β†’ Settings β†’ Secrets and variables β†’ Actions and add the following secrets: - -### SSH Connection - -- `TESTING_SERVER_HOST` - IP address or domain of the testing server -- `TESTING_SERVER_USER` - User for SSH connection (e.g., `deploy`) -- `TESTING_SERVER_SSH_KEY` - Private SSH key for server connection -- `TESTING_SERVER_PORT` - (optional) SSH port (default: 22) - -### Database Configuration - -- `TESTING_DB_ROOT_PASSWORD` - Root password for MariaDB -- `TESTING_DB_USERNAME` - Database user -- `TESTING_DB_PASSWORD` - Database user password - -## Required GitHub Variables - -Go to repository settings β†’ Settings β†’ Secrets and variables β†’ Actions β†’ Variables and add: - -- `TESTING_BASE_PORT` - Base port for applications (default: 3000) -- `TESTING_BASE_DB_PORT` - Base port for databases (default: 3306) -- `TESTING_BASE_REDIS_PORT` - Base port for Redis (default: 6379) - -## Testing Server Setup - -### 1. Installing Docker and Docker Compose - -```bash -# System update -sudo apt update && sudo apt upgrade -y - -# Docker installation -curl -fsSL https://get.docker.com -o get-docker.sh -sudo sh get-docker.sh - -# Add user to docker group -sudo usermod -aG docker $USER - -# Install Docker Compose -sudo apt install docker-compose-plugin -y -``` - -### 2. Creating deployment user - -```bash -# Create user -sudo useradd -m -s /bin/bash deploy -sudo usermod -aG docker deploy - -# Create SSH keys directory -sudo mkdir -p /home/deploy/.ssh -sudo chmod 700 /home/deploy/.ssh - -# Add public SSH key -sudo nano /home/deploy/.ssh/authorized_keys -# Insert public key corresponding to private key in TESTING_SERVER_SSH_KEY - -sudo chmod 600 /home/deploy/.ssh/authorized_keys -sudo chown -R deploy:deploy /home/deploy/.ssh - -# Create applications directory -sudo mkdir -p /opt/low-code-engine -sudo chown deploy:deploy /opt/low-code-engine -``` - -### 3. Nginx Setup (Optional) - -If you want to use domain names instead of ports: - -```bash -sudo apt install nginx -y - -# Create configuration for testing applications -sudo nano /etc/nginx/sites-available/testing-apps -``` - -File content: - -```nginx -server { - listen 80; - server_name ~^pr-(?\d+)\.testing\.yourdomain\.com$; - - location / { - set $app_port 3000$pr_number; - proxy_pass http://127.0.0.1:$app_port; - 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; - } -} -``` - -```bash -# Activate configuration -sudo ln -s /etc/nginx/sites-available/testing-apps /etc/nginx/sites-enabled/ -sudo nginx -t -sudo systemctl reload nginx -``` - -## How Deployment Works - -### Deployment Process - -1. **Trigger**: Creating or updating Pull Request to `develop` branch -2. **Build**: Building application and creating Docker image -3. **Deploy**: Copying files to server and starting containers -4. **Health Check**: Checking application availability -5. **Comment**: Adding comment to PR with deployment information - -### Server Structure - -``` -/opt/low-code-engine/ -β”œβ”€β”€ testing-pr-123/ # Separate directory for each PR -β”‚ β”œβ”€β”€ docker-compose.yml # Main docker-compose file -β”‚ β”œβ”€β”€ docker-compose.override.yml # Testing overrides -β”‚ β”œβ”€β”€ .env # Environment variables -β”‚ β”œβ”€β”€ docker/ # Docker configurations -β”‚ └── low-code-engine-testing.tar.gz # Docker image -β”œβ”€β”€ testing-pr-124/ -└── ... -``` - -### Ports - -Each PR is assigned unique ports: - -- Application: `TESTING_BASE_PORT + PR_NUMBER` (e.g., 3000 + 123 = 3123) -- Database: `TESTING_BASE_DB_PORT + PR_NUMBER` (e.g., 3306 + 123 = 3429) -- Redis: `TESTING_BASE_REDIS_PORT + PR_NUMBER` (e.g., 6379 + 123 = 6502) - -### Cleanup - -When PR is closed or merged, automatically: - -1. Stop and remove containers -2. Remove Docker images -3. Remove files on server -4. Add cleanup comment - -## Security - -1. **SSH Keys**: Use separate SSH key only for deployment -2. **User**: Create separate user with minimal privileges -3. **Firewall**: Configure firewall to restrict port access -4. **SSL/TLS**: Consider using SSL certificates for HTTPS - -## Monitoring and Logs - -### View Application Logs - -```bash -cd /opt/low-code-engine/testing-pr-{PR_NUMBER} -docker-compose logs -f app -``` - -### View Container Status - -```bash -docker-compose ps -``` - -### Resource Monitoring - -```bash -docker stats -``` - -## Troubleshooting - -### Port Issues - -If port is occupied, check which applications are using it: - -```bash -sudo netstat -tulpn | grep :{PORT} -``` - -### Docker Issues - -Clean up unused resources: - -```bash -docker system prune -f -``` - -### Database Issues - -Check database connection: - -```bash -docker-compose exec mariadb mysql -u root -p -e "SHOW DATABASES;" -``` - -### Migration Issues - -Manual migration run: - -```bash -docker-compose exec app yarn migration:run -``` diff --git a/docker-compose.yml b/docker-compose.yml index ed74915..e5755dd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -34,36 +34,28 @@ services: retries: 10 interval: 10s - # NestJS Application - app: - build: - context: . - dockerfile: Dockerfile - target: development - container_name: low-code-engine-app + # Redis Cache + redis: + image: redis:7-alpine + container_name: low-code-engine-redis restart: unless-stopped - environment: - NODE_ENV: ${NODE_ENV:-development} - DB_HOST: mariadb - DB_PORT: 3306 - DB_USERNAME: ${DB_USERNAME:-app_user} - DB_PASSWORD: ${DB_PASSWORD:-app_password} - DB_DATABASE: ${DB_DATABASE:-low_code_engine} ports: - - "${APP_PORT:-3000}:3000" + - "${REDIS_PORT:-6379}:6379" volumes: - - .:/usr/src/app - - /usr/src/app/node_modules + - redis_data:/data networks: - app-network - depends_on: - mariadb: - condition: service_healthy - command: yarn start:dev + healthcheck: + test: ["CMD", "redis-cli", "ping"] + timeout: 5s + retries: 10 + interval: 10s volumes: mariadb_data: driver: local + redis_data: + driver: local networks: app-network: diff --git a/scripts/setup-docker.sh b/scripts/setup-docker.sh new file mode 100644 index 0000000..60f7d3e --- /dev/null +++ b/scripts/setup-docker.sh @@ -0,0 +1,37 @@ +#!/bin/bash +set -e + +echo "πŸ“¦ Updating system packages..." +apt update -y +apt upgrade -y + +echo "πŸ”§ Installing dependencies..." +apt install -y apt-transport-https ca-certificates curl gnupg lsb-release + +echo "πŸ”‘ Adding Docker’s official GPG key..." +curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg + +echo "πŸ“‚ Adding Docker repository..." +echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \ + https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \ + $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null + +echo "πŸ“¦ Installing Docker Engine and Compose..." +apt update -y +apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin + +echo "βš™οΈ Enabling and starting Docker service..." +systemctl enable docker +systemctl start docker + +echo "πŸ‘€ Adding current user to docker group..." +usermod -aG docker $USER + +echo "βœ… Docker and Docker Compose installation complete!" +echo "➑️ Log out and log back in (or run 'newgrp docker') to use Docker without sudo." +echo +echo "πŸ’‘ Docker version:" +docker --version || echo "Docker not yet available in current shell" +echo "πŸ’‘ Docker Compose version:" +docker compose version || echo "Docker Compose not yet available in current shell" diff --git a/scripts/setup-testing-server.sh b/scripts/setup-testing-server.sh deleted file mode 100755 index e579134..0000000 --- a/scripts/setup-testing-server.sh +++ /dev/null @@ -1,293 +0,0 @@ -#!/bin/bash - -# Quick setup script for testing server -# Run this script on your testing server to set up the environment - -set -e - -echo "πŸš€ Setting up testing server for Low Code Engine deployments..." - -# Colors for output -RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' -NC='\033[0m' # No Color - -# Function to print colored output -print_status() { - echo -e "${GREEN}[INFO]${NC} $1" -} - -print_warning() { - echo -e "${YELLOW}[WARNING]${NC} $1" -} - -print_error() { - echo -e "${RED}[ERROR]${NC} $1" -} - -# Check if running as root -if [[ $EUID -eq 0 ]]; then - print_error "This script should not be run as root for security reasons" - exit 1 -fi - -# Update system -print_status "Updating system packages..." -sudo apt update && sudo apt upgrade -y - -# Install Docker -print_status "Installing Docker..." -if ! command -v docker &> /dev/null; then - curl -fsSL https://get.docker.com -o get-docker.sh - sudo sh get-docker.sh - sudo usermod -aG docker $USER - rm get-docker.sh - print_status "Docker installed successfully" -else - print_status "Docker is already installed" -fi - -# Install Docker Compose -print_status "Installing Docker Compose..." -if ! command -v docker-compose &> /dev/null; then - sudo apt install docker-compose-plugin -y - print_status "Docker Compose installed successfully" -else - print_status "Docker Compose is already installed" -fi - -# Create deploy user -print_status "Creating deploy user..." -if ! id "deploy" &>/dev/null; then - sudo useradd -m -s /bin/bash deploy - sudo usermod -aG docker deploy - print_status "Deploy user created successfully" -else - print_status "Deploy user already exists" -fi - -# Setup SSH directory for deploy user -print_status "Setting up SSH for deploy user..." -sudo mkdir -p /home/deploy/.ssh -sudo chmod 700 /home/deploy/.ssh -sudo touch /home/deploy/.ssh/authorized_keys -sudo chmod 600 /home/deploy/.ssh/authorized_keys -sudo chown -R deploy:deploy /home/deploy/.ssh - -# Create application directory -print_status "Creating application directory..." -sudo mkdir -p /opt/low-code-engine -sudo chown deploy:deploy /opt/low-code-engine - -# Install nginx (optional) -print_status "Installing Nginx..." -if ! command -v nginx &> /dev/null; then - sudo apt install nginx -y - print_status "Nginx installed successfully" -else - print_status "Nginx is already installed" -fi - -# Create nginx configuration for testing apps -print_status "Creating Nginx configuration..." -sudo tee /etc/nginx/sites-available/testing-apps > /dev/null <\d+)\.testing\.(.+)$; - - # Health check endpoint - location /health { - set \$app_port 3000\$pr_number; - proxy_pass http://127.0.0.1:\$app_port; - 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; - - # Add CORS headers for API calls - add_header 'Access-Control-Allow-Origin' '*' always; - add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always; - add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always; - } - - # Main application - location / { - set \$app_port 3000\$pr_number; - proxy_pass http://127.0.0.1:\$app_port; - 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; - - # WebSocket support - proxy_http_version 1.1; - proxy_set_header Upgrade \$http_upgrade; - proxy_set_header Connection "upgrade"; - - # Timeouts - proxy_connect_timeout 60s; - proxy_send_timeout 60s; - proxy_read_timeout 60s; - } -} - -# Direct port access (fallback) -server { - listen 80 default_server; - server_name _; - - location / { - return 200 'Testing Server is running. Use pr-{NUMBER}.testing.yourdomain.com to access deployments.'; - add_header Content-Type text/plain; - } -} -EOF - -# Enable nginx configuration -if [ ! -L /etc/nginx/sites-enabled/testing-apps ]; then - sudo ln -s /etc/nginx/sites-available/testing-apps /etc/nginx/sites-enabled/ -fi - -# Remove default nginx site -if [ -L /etc/nginx/sites-enabled/default ]; then - sudo rm /etc/nginx/sites-enabled/default -fi - -# Test and reload nginx -sudo nginx -t && sudo systemctl reload nginx -print_status "Nginx configured successfully" - -# Install useful tools -print_status "Installing additional tools..." -sudo apt install -y curl wget htop netstat-nat jq - -# Create cleanup script -print_status "Creating cleanup script..." -sudo tee /usr/local/bin/cleanup-old-deployments > /dev/null <<'EOF' -#!/bin/bash -# Cleanup script for old testing deployments - -DAYS_OLD=7 -DEPLOYMENT_DIR="/opt/low-code-engine" - -echo "Cleaning up deployments older than ${DAYS_OLD} days..." - -find ${DEPLOYMENT_DIR} -name "testing-pr-*" -type d -mtime +${DAYS_OLD} | while read dir; do - echo "Cleaning up: $dir" - - # Stop containers if running - if [ -f "$dir/docker-compose.yml" ]; then - cd "$dir" - docker-compose down -v 2>/dev/null || true - fi - - # Remove directory - rm -rf "$dir" - echo "Removed: $dir" -done - -# Clean up unused Docker resources -docker system prune -f -docker image prune -f - -echo "Cleanup completed" -EOF - -sudo chmod +x /usr/local/bin/cleanup-old-deployments - -# Create cron job for cleanup -print_status "Setting up automatic cleanup..." -(crontab -l 2>/dev/null; echo "0 2 * * * /usr/local/bin/cleanup-old-deployments") | crontab - - -# Create monitoring script -print_status "Creating monitoring script..." -sudo tee /usr/local/bin/monitor-deployments > /dev/null <<'EOF' -#!/bin/bash -# Monitoring script for testing deployments - -DEPLOYMENT_DIR="/opt/low-code-engine" - -echo "=== Testing Deployments Status ===" -echo "Date: $(date)" -echo - -# Show active deployments -echo "Active Deployments:" -find ${DEPLOYMENT_DIR} -name "testing-pr-*" -type d | sort | while read dir; do - pr_number=$(basename "$dir" | sed 's/testing-pr-//') - if [ -f "$dir/docker-compose.yml" ]; then - cd "$dir" - status=$(docker-compose ps -q | wc -l) - if [ "$status" -gt 0 ]; then - app_port=$((3000 + pr_number)) - echo " PR #${pr_number}: Running on port ${app_port}" - # Check if app is responding - if curl -s --max-time 5 "http://localhost:${app_port}/health" > /dev/null 2>&1; then - echo " Status: βœ… Healthy" - else - echo " Status: ❌ Unhealthy" - fi - else - echo " PR #${pr_number}: Stopped" - fi - fi -done - -echo -echo "=== System Resources ===" -echo "Disk Usage:" -df -h /opt/low-code-engine -echo -echo "Docker Usage:" -docker system df -echo -echo "Memory Usage:" -free -h -EOF - -sudo chmod +x /usr/local/bin/monitor-deployments - -# Setup firewall (optional but recommended) -print_status "Configuring firewall..." -if command -v ufw &> /dev/null; then - sudo ufw --force enable - sudo ufw allow ssh - sudo ufw allow 80/tcp - sudo ufw allow 443/tcp - # Allow port range for testing apps (3000-3999) - sudo ufw allow 3000:3999/tcp - # Allow port range for databases (3300-3999) - sudo ufw allow 3300:3999/tcp - # Allow port range for redis (6300-6999) - sudo ufw allow 6300:6999/tcp - print_status "Firewall configured" -else - print_warning "UFW not found, skipping firewall configuration" -fi - -print_status "Setup completed successfully!" -echo -echo "πŸŽ‰ Your testing server is ready!" -echo -echo "Next steps:" -echo "1. Add your public SSH key to /home/deploy/.ssh/authorized_keys" -echo "2. Configure your GitHub repository secrets:" -echo " - TESTING_SERVER_HOST: $(curl -s ifconfig.me 2>/dev/null || echo 'YOUR_SERVER_IP')" -echo " - TESTING_SERVER_USER: deploy" -echo " - TESTING_SERVER_SSH_KEY: (your private SSH key)" -echo " - TESTING_DB_ROOT_PASSWORD: (choose a strong password)" -echo " - TESTING_DB_USERNAME: app_user" -echo " - TESTING_DB_PASSWORD: (choose a strong password)" -echo -echo "Useful commands:" -echo " - Monitor deployments: sudo /usr/local/bin/monitor-deployments" -echo " - Cleanup old deployments: sudo /usr/local/bin/cleanup-old-deployments" -echo " - Check nginx status: sudo systemctl status nginx" -echo " - View nginx logs: sudo journalctl -u nginx -f" -echo -print_warning "Please reboot the server or run 'newgrp docker' to apply Docker group changes" -EOF \ No newline at end of file