diff --git a/PROXMOX_DEPLOY_TEMPLATE.md b/PROXMOX_DEPLOY_TEMPLATE.md index 8f179a3..ec36dd4 100644 --- a/PROXMOX_DEPLOY_TEMPLATE.md +++ b/PROXMOX_DEPLOY_TEMPLATE.md @@ -273,86 +273,51 @@ curl -I http://localhost --- -## 🔄 Step 6: Setup Auto-Sync (Every 30 Seconds) +## 🔄 Step 6: Setup Auto-Sync (Every 60 Seconds) - We use **Systemd Timers** instead of Cron because they support sub-minute intervals and prevent overlapping execution. + We use **Systemd Timers** instead of Cron because they support reliable intervals and execution tracking. - ### 6.1 Create robust auto-sync script + ### 6.1 Create robust auto-sync script (with file locking) ```bash - cat > /var/www/your-app-name/auto-sync.sh << 'EOF' - #!/bin/bash - - # Configuration - APP_NAME="your-app-name" - APP_DIR="/var/www/$APP_NAME" - LOG_FILE="/var/log/$APP_NAME-autosync.log" - LOCK_FILE="/var/lock/$APP_NAME-sync.lock" - - # Singleton execution with flock (prevents overlap) - exec 9>"$LOCK_FILE" - if ! flock -n 9; then - exit 0 - fi - - log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"; } - - cd "$APP_DIR" || exit 1 - - # Fetch and check - if ! git fetch origin main --quiet; then exit 1; fi - - LOCAL=$(git rev-parse HEAD) - REMOTE=$(git rev-parse origin/main) - - if [ "$LOCAL" != "$REMOTE" ]; then - log "🔄 Changes detected! Pulling..." - git pull origin main >> "$LOG_FILE" 2>&1 - - if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "package.json"; then - log "📦 package.json changed. Installing dependencies..." - npm install --production >> "$LOG_FILE" 2>&1 || exit 1 - fi - - log "🔄 Restarting service..." - systemctl restart "$APP_NAME" - log "✅ Sync complete." - fi - EOF - - chmod +x /var/www/your-app-name/auto-sync.sh + # ... (Script content same as before, ensures single instance) ``` ### 6.2 Create Systemd Timer Units - Create the Service (What to run): - ```bash - cat > /etc/systemd/system/your-app-name-sync.service << 'EOF' + Service Unit (`/etc/systemd/system/your-app-name-sync.service`): + ```ini [Unit] Description=Auto-sync Service [Service] Type=oneshot ExecStart=/var/www/your-app-name/auto-sync.sh User=root - EOF ``` - Create the Timer (When to run): - ```bash - cat > /etc/systemd/system/your-app-name-sync.timer << 'EOF' + Timer Unit (`/etc/systemd/system/your-app-name-sync.timer`): + ```ini [Unit] - Description=Run auto-sync every 30 seconds + Description=Run auto-sync every 60 seconds [Timer] OnBootSec=1min - OnUnitActiveSec=30s + OnUnitActiveSec=60s Unit=your-app-name-sync.service [Install] WantedBy=timers.target - EOF ``` + > **💡 Pro Tip: Fix SSH Connection Limits** + > If you get "Connection refused" errors during deployment, reliable automation requires higher SSH limits. + > Update `/etc/ssh/sshd_config`: + > ```bash + > MaxStartups 100:30:200 + > MaxSessions 100 + > ``` + > Then run `systemctl restart ssh`. + ### 6.3 Enable the Timer ```bash diff --git a/deploy-sync-from-git.ps1 b/deploy-sync-from-git.ps1 new file mode 100644 index 0000000..15a64a7 --- /dev/null +++ b/deploy-sync-from-git.ps1 @@ -0,0 +1,30 @@ +$Server = "172.16.69.219" +$User = "root" +$Pass = "Q4dv!Z`$nCe#`$OT&h" +$RemotePath = "/var/www/web-page-performance-test" + +# Commands to run on server +# 1. Pull latest files +# 2. Copy Systemd units to /etc/systemd/system/ +# 3. Enable and start timer +$Commands = @( + "cd $RemotePath", + "git fetch origin main", + "git reset --hard origin/main", # Force match repo + "chmod +x auto-sync-robust.sh", + "chmod +x fix-ssh-limits.sh", + "./fix-ssh-limits.sh", # Apply SSH limits fix first + "cp auto-sync-robust.sh auto-sync.sh", + "cp web-page-performance-test-sync.service /etc/systemd/system/", + "cp web-page-performance-test-sync.timer /etc/systemd/system/", + "systemctl daemon-reload", + "crontab -l | grep -v 'auto-sync.sh' | crontab -", + "systemctl enable web-page-performance-test-sync.timer", + "systemctl restart web-page-performance-test-sync.timer", + "systemctl list-timers --all | grep web-page", + "echo '✅ Systemd Timer (60s) Deployed & SSH Limits Fixed!'" +) +$CommandStr = $Commands -join " && " + +echo "🚀 Triggering remote deployment (Pull & Apply)..." +plink -batch -pw "$Pass" "$User@$Server" $CommandStr diff --git a/fix-ssh-limits.sh b/fix-ssh-limits.sh new file mode 100644 index 0000000..3ef3a62 --- /dev/null +++ b/fix-ssh-limits.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# fix-ssh-limits.sh +# Increases SSH connection limits to prevent "Connection refused" during rapid automation. + +SSHD_CONFIG="/etc/ssh/sshd_config" + +log() { + echo "[SSH-FIX] $1" +} + +if [ ! -f "$SSHD_CONFIG" ]; then + log "Error: $SSHD_CONFIG not found." + exit 1 +fi + +# Backup config +cp "$SSHD_CONFIG" "$SSHD_CONFIG.bak.$(date +%s)" + +# Function to update or add a setting +set_config() { + local param=$1 + local value=$2 + if grep -q "^#\?${param}" "$SSHD_CONFIG"; then + sed -i "s/^#\?${param}.*/${param} ${value}/" "$SSHD_CONFIG" + else + echo "${param} ${value}" >> "$SSHD_CONFIG" + fi + log "Set ${param} to ${value}" +} + +# Apply high limits +set_config "MaxStartups" "100:30:200" +set_config "MaxSessions" "100" +set_config "MaxAuthTries" "20" + +# Reload SSH service +if systemctl restart ssh; then + log "✅ SSH service restarted successfully with new limits." + log "VERIFICATION: $(grep 'MaxStartups' $SSHD_CONFIG)" +else + log "❌ Failed to restart SSH service." + exit 1 +fi diff --git a/web-page-performance-test-sync.timer b/web-page-performance-test-sync.timer index 97e6dd7..f530286 100644 --- a/web-page-performance-test-sync.timer +++ b/web-page-performance-test-sync.timer @@ -3,7 +3,7 @@ Description=Run auto-sync every 30 seconds [Timer] OnBootSec=1min -OnUnitActiveSec=30s +OnUnitActiveSec=60s Unit=web-page-performance-test-sync.service [Install]