#!/bin/bash # Robust Service Installation Script # Handles SSH connection issues and retries set -uo pipefail NODE_IP="${PROXMOX_HOST_R630_01}" MAX_RETRIES=3 RETRY_DELAY=2 log_info() { echo -e "\033[0;32m[INFO]\033[0m $1"; } log_error() { echo -e "\033[0;31m[ERROR]\033[0m $1"; } log_success() { echo -e "\033[0;32m[✓]\033[0m $1"; } # Robust SSH execution with retries ssh_exec() { local vmid="$1" local command="$2" local retry=0 while [ $retry -lt $MAX_RETRIES ]; do if ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o ServerAliveInterval=5 root@${NODE_IP} "pct exec $vmid -- bash -c '$command'" 2>&1; then return 0 fi retry=$((retry + 1)) if [ $retry -lt $MAX_RETRIES ]; then log_info "Retry $retry/$MAX_RETRIES for CT $vmid..." sleep $RETRY_DELAY fi done return 1 } # Install PostgreSQL install_postgresql() { local vmid="$1" log_info "Installing PostgreSQL on CT $vmid..." ssh_exec "$vmid" " export DEBIAN_FRONTEND=noninteractive apt-get update -qq apt-get install -y -qq postgresql-15 postgresql-contrib-15 || exit 1 # Configure PostgreSQL sed -i \"s/#listen_addresses = .*/listen_addresses = '*'/\" /etc/postgresql/15/main/postgresql.conf 2>/dev/null || true echo \"host all all 0.0.0.0/0 md5\" >> /etc/postgresql/15/main/pg_hba.conf 2>/dev/null || true systemctl enable postgresql@15-main systemctl start postgresql@15-main sleep 3 systemctl is-active postgresql@15-main && echo 'PostgreSQL installed' || exit 1 " && log_success "PostgreSQL installed on CT $vmid" || log_error "Failed to install PostgreSQL on CT $vmid" } # Install Redis install_redis() { local vmid="$1" log_info "Installing Redis on CT $vmid..." ssh_exec "$vmid" " export DEBIAN_FRONTEND=noninteractive apt-get update -qq apt-get install -y -qq redis-server || exit 1 sed -i \"s/^bind .*/bind 0.0.0.0/\" /etc/redis/redis.conf 2>/dev/null || true systemctl enable redis-server systemctl restart redis-server sleep 2 systemctl is-active redis-server && echo 'Redis installed' || exit 1 " && log_success "Redis installed on CT $vmid" || log_error "Failed to install Redis on CT $vmid" } # Install Node.js install_nodejs() { local vmid="$1" log_info "Installing Node.js on CT $vmid..." ssh_exec "$vmid" " export DEBIAN_FRONTEND=noninteractive apt-get update -qq apt-get install -y -qq curl ca-certificates gnupg || exit 1 curl -fsSL https://deb.nodesource.com/setup_18.x | bash - || exit 1 apt-get install -y -qq nodejs || exit 1 npm install -g pm2 || exit 1 node --version && npm --version && echo 'Node.js installed' || exit 1 " && log_success "Node.js installed on CT $vmid" || log_error "Failed to install Node.js on CT $vmid" } # Main execution echo "═══════════════════════════════════════════════════════════" echo "Robust Service Installation" echo "═══════════════════════════════════════════════════════════" echo "" # Install PostgreSQL log_info "Installing PostgreSQL on database containers..." for vmid in 10000 10001 10100 10101; do install_postgresql "$vmid" sleep 1 done # Install Redis log_info "Installing Redis on cache containers..." for vmid in 10020 10120; do install_redis "$vmid" sleep 1 done # Install Node.js log_info "Installing Node.js on application containers..." for vmid in 10030 10040 10050 10060 10070 10080 10090 10091 10092 10130 10150 10151; do install_nodejs "$vmid" sleep 1 done echo "" log_info "Installation complete!"