#!/bin/bash # Complete Service Installation Using Binary Methods # Bypasses apt-get limitations by using direct binary 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"; } # Install Node.js via binary (works in unprivileged containers) install_nodejs_binary() { local vmid="$1" log_info "Installing Node.js (binary) on CT $vmid..." ssh -o ConnectTimeout=15 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'INSTALL_EOF' cd /tmp NODE_VERSION=\"v18.19.0\" ARCH=\"x64\" # Check if wget is available, if not try curl if command -v wget >/dev/null 2>&1; then DOWNLOAD_CMD=\"wget -q\" elif command -v curl >/dev/null 2>&1; then DOWNLOAD_CMD=\"curl -fsSL -o\" else echo 'Neither wget nor curl available' exit 1 fi # Download Node.js binary if [ \"\$DOWNLOAD_CMD\" = \"wget -q\" ]; then wget -q https://nodejs.org/dist/\${NODE_VERSION}/node-\${NODE_VERSION}-linux-\${ARCH}.tar.xz || exit 1 else curl -fsSL https://nodejs.org/dist/\${NODE_VERSION}/node-\${NODE_VERSION}-linux-\${ARCH}.tar.xz -o node-\${NODE_VERSION}-linux-\${ARCH}.tar.xz || exit 1 fi # Extract tar -xf node-\${NODE_VERSION}-linux-\${ARCH}.tar.xz || exit 1 # Install to /usr/local cp -r node-\${NODE_VERSION}-linux-\${ARCH}/* /usr/local/ || exit 1 # Create symlinks if needed ln -sf /usr/local/bin/node /usr/bin/node 2>/dev/null || true ln -sf /usr/local/bin/npm /usr/bin/npm 2>/dev/null || true # Install PM2 /usr/local/bin/npm install -g pm2 || /usr/bin/npm install -g pm2 || exit 1 # Verify node --version && npm --version && echo 'Node.js installed successfully' || exit 1 # Cleanup rm -rf node-\${NODE_VERSION}-linux-\${ARCH}* || true INSTALL_EOF " && log_success "Node.js installed on CT $vmid" || log_error "Failed to install Node.js on CT $vmid" } # Install PostgreSQL using official APT repository (if we can get apt working) # Alternative: Use Docker or pre-compiled binaries install_postgresql_alternative() { local vmid="$1" log_info "Attempting PostgreSQL installation on CT $vmid..." # First try to fix and use apt ssh -o ConnectTimeout=15 -o StrictHostKeyChecking=no root@${NODE_IP} " pct stop $vmid 2>/dev/null || true sleep 1 MOUNT=\$(pct mount $vmid 2>&1 | grep rootfs | awk '{print \$NF}') if [ -n \"\$MOUNT\" ]; then # Remove all lock files rm -f \$MOUNT/var/lib/apt/lists/lock \$MOUNT/var/lib/dpkg/lock* \$MOUNT/var/cache/apt/archives/lock 2>/dev/null # Fix ownership chown -R root:root \$MOUNT/var/lib/apt \$MOUNT/var/cache/apt \$MOUNT/var/lib/dpkg 2>/dev/null || true pct unmount $vmid 2>/dev/null || true fi pct start $vmid 2>/dev/null || true sleep 3 " # Try installation ssh -o ConnectTimeout=15 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'INSTALL_EOF' export DEBIAN_FRONTEND=noninteractive # Try to update package lists (ignore permission warnings) apt-get update -qq 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || true # Try installation apt-get install -y -qq postgresql-15 postgresql-contrib-15 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || { echo 'apt-get failed, trying alternative method...' 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 "PostgreSQL installation failed on CT $vmid - will document for manual installation" return 1 } } # Install Redis alternative method install_redis_alternative() { local vmid="$1" log_info "Attempting Redis installation on CT $vmid..." # Fix permissions first ssh -o ConnectTimeout=15 -o StrictHostKeyChecking=no root@${NODE_IP} " pct stop $vmid 2>/dev/null || true sleep 1 MOUNT=\$(pct mount $vmid 2>&1 | grep rootfs | awk '{print \$NF}') if [ -n \"\$MOUNT\" ]; then rm -f \$MOUNT/var/lib/apt/lists/lock \$MOUNT/var/lib/dpkg/lock* 2>/dev/null chown -R root:root \$MOUNT/var/lib/apt \$MOUNT/var/cache/apt \$MOUNT/var/lib/dpkg 2>/dev/null || true pct unmount $vmid 2>/dev/null || true fi pct start $vmid 2>/dev/null || true sleep 3 " ssh -o ConnectTimeout=15 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'INSTALL_EOF' export DEBIAN_FRONTEND=noninteractive apt-get update -qq 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || true apt-get install -y -qq redis-server 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || 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 "Redis installation failed on CT $vmid - will document for manual installation" return 1 } } # Configure databases (if PostgreSQL is installed) configure_databases() { local vmid="$1" local db_type="$2" # "order" or "dbis" log_info "Configuring $db_type database on CT $vmid..." ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'CONFIG_EOF' # Check if PostgreSQL is running if systemctl is-active postgresql@15-main >/dev/null 2>&1; then if [ \"$db_type\" = \"order\" ]; then 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' else 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' fi else echo 'PostgreSQL not running, skipping database configuration' fi CONFIG_EOF " && log_success "$db_type DB configured on CT $vmid" || log_error "Failed to configure $db_type DB on CT $vmid" } # Verify services verify_services() { 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 "Complete Service Installation - Binary Methods" echo "═══════════════════════════════════════════════════════════" echo "" # Install Node.js via binary (this should work) log_info "Installing Node.js via binary download..." NODEJS_VMIDS=(10030 10040 10050 10060 10070 10080 10090 10091 10092 10130 10150 10151) for vmid in "${NODEJS_VMIDS[@]}"; do install_nodejs_binary "$vmid" sleep 1 done # Try PostgreSQL installation log_info "Attempting PostgreSQL installation..." for vmid in 10000 10001 10100 10101; do install_postgresql_alternative "$vmid" sleep 2 done # Try Redis installation log_info "Attempting Redis installation..." for vmid in 10020 10120; do install_redis_alternative "$vmid" sleep 2 done # Configure databases for containers where PostgreSQL is installed log_info "Configuring databases..." for vmid in 10000 10001; do configure_databases "$vmid" "order" sleep 1 done for vmid in 10100 10101; do configure_databases "$vmid" "dbis" sleep 1 done # Verify installations echo "" log_info "Verifying installations..." echo "PostgreSQL:" for vmid in 10000 10001 10100 10101; do status=$(verify_services "$vmid" "postgresql") echo " CT $vmid: $status" done echo "Redis:" for vmid in 10020 10120; do status=$(verify_services "$vmid" "redis") echo " CT $vmid: $status" done echo "Node.js:" for vmid in "${NODEJS_VMIDS[@]}"; do status=$(verify_services "$vmid" "nodejs") echo " CT $vmid: $status" done echo "" log_info "Installation process complete!"