#!/usr/bin/env bash # Convert Containers to Privileged and Install All Services # This script converts containers to privileged mode and completes all installations set -uo pipefail NODE_IP="${PROXMOX_HOST_R630_01}" 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"; } log_warn() { echo -e "\033[1;33m[WARN]\033[0m $1"; } # Convert container to privileged convert_to_privileged() { local vmid="$1" log_info "Converting CT $vmid to privileged mode..." ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no root@${NODE_IP} " # Stop container pct stop $vmid 2>/dev/null || true sleep 2 # Try to set unprivileged=0 if pct set $vmid --unprivileged 0 2>&1; then echo 'Converted to privileged' pct start $vmid 2>/dev/null || true sleep 3 return 0 else echo 'Cannot convert - may require container recreation' pct start $vmid 2>/dev/null || true return 1 fi " } # Install PostgreSQL install_postgresql() { local vmid="$1" log_info "Installing PostgreSQL on CT $vmid..." ssh -o ConnectTimeout=15 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'INSTALL_EOF' 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 INSTALL_EOF " && 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 -o ConnectTimeout=15 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'INSTALL_EOF' 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 INSTALL_EOF " && 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 -o ConnectTimeout=15 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'INSTALL_EOF' 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 INSTALL_EOF " && log_success "Node.js installed on CT $vmid" || log_error "Failed to install Node.js on CT $vmid" } # Configure databases configure_postgresql_order() { local vmid="$1" log_info "Configuring Order database on CT $vmid..." ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'CONFIG_EOF' su - postgres -c \"psql << 'SQL_EOF' CREATE DATABASE order_db; CREATE USER order_user WITH PASSWORD 'order_password'; GRANT ALL PRIVILEGES ON DATABASE order_db TO order_user; ALTER DATABASE order_db OWNER TO order_user; SQL_EOF \" && echo 'Order DB configured' || echo 'Order DB config failed' CONFIG_EOF " && log_success "Order DB configured on CT $vmid" || log_error "Failed to configure Order DB on CT $vmid" } configure_postgresql_dbis() { local vmid="$1" log_info "Configuring DBIS database on CT $vmid..." ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'CONFIG_EOF' su - postgres -c \"psql << 'SQL_EOF' CREATE DATABASE dbis_core; CREATE USER dbis WITH PASSWORD '8cba649443f97436db43b34ab2c0e75b5cf15611bef9c099cee6fb22cc3d7771'; GRANT ALL PRIVILEGES ON DATABASE dbis_core TO dbis; ALTER DATABASE dbis_core OWNER TO dbis; SQL_EOF \" && echo 'DBIS DB configured' || echo 'DBIS DB config failed' CONFIG_EOF " && log_success "DBIS DB configured on CT $vmid" || log_error "Failed to configure DBIS DB on CT $vmid" } # Verify services verify_service() { local vmid="$1" local service_type="$2" ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'VERIFY_EOF' case \"$service_type\" in postgresql) systemctl is-active postgresql@15-main >/dev/null 2>&1 && echo 'active' || echo 'inactive' ;; redis) systemctl is-active redis-server >/dev/null 2>&1 && echo 'active' || echo 'inactive' ;; nodejs) node --version >/dev/null 2>&1 && echo 'installed' || echo 'not installed' ;; esac VERIFY_EOF " } echo "═══════════════════════════════════════════════════════════" echo "Convert to Privileged and Install All Services" echo "═══════════════════════════════════════════════════════════" echo "" # Step 1: Convert containers to privileged log_info "Step 1: Converting containers to privileged mode..." ALL_VMIDS=(10000 10001 10020 10030 10040 10050 10060 10070 10080 10090 10091 10092 10100 10101 10120 10130 10150 10151) CONVERTED=0 FAILED_CONVERT=0 for vmid in "${ALL_VMIDS[@]}"; do if convert_to_privileged "$vmid"; then ((CONVERTED++)) sleep 1 else ((FAILED_CONVERT++)) log_warn "CT $vmid could not be converted - may need recreation" fi done log_info "Converted: $CONVERTED, Failed: $FAILED_CONVERT" # Step 2: Install PostgreSQL log_info "Step 2: Installing PostgreSQL..." for vmid in 10000 10001 10100 10101; do install_postgresql "$vmid" sleep 2 done # Step 3: Configure databases log_info "Step 3: Configuring databases..." for vmid in 10000 10001; do configure_postgresql_order "$vmid" sleep 1 done for vmid in 10100 10101; do configure_postgresql_dbis "$vmid" sleep 1 done # Step 4: Install Redis log_info "Step 4: Installing Redis..." for vmid in 10020 10120; do install_redis "$vmid" sleep 2 done # Step 5: Install Node.js log_info "Step 5: Installing Node.js..." for vmid in 10030 10040 10050 10060 10070 10080 10090 10091 10092 10130 10150 10151; do install_nodejs "$vmid" sleep 2 done # Step 6: Verify all services echo "" log_info "Step 6: Verifying all installations..." echo "PostgreSQL Status:" for vmid in 10000 10001 10100 10101; do status=$(verify_service "$vmid" "postgresql") echo " CT $vmid: $status" done echo "Redis Status:" for vmid in 10020 10120; do status=$(verify_service "$vmid" "redis") echo " CT $vmid: $status" done echo "Node.js Status:" for vmid in 10030 10040 10050 10060 10070 10080 10090 10091 10092 10130 10150 10151; do status=$(verify_service "$vmid" "nodejs") echo " CT $vmid: $status" done echo "" log_info "All installations complete!"