Files
proxmox/scripts/archive/consolidated/deploy/install-services-user-space-complete.sh
defiQUG 8fc4fc7811
Some checks failed
Deploy to Phoenix / deploy (push) Has been cancelled
scripts(archive): consolidated helpers and backup copies sync
Made-with: Cursor
2026-03-27 18:51:09 -07:00

254 lines
9.5 KiB
Bash
Executable File

#!/bin/bash
# Install Services in User Space - Works with Unprivileged Containers
# Installs services to /opt and /home directories which are writable
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 to /opt (user space)
install_nodejs_user_space() {
local vmid="$1"
log_info "Installing Node.js (user space) on CT $vmid..."
ssh -o ConnectTimeout=20 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'INSTALL_EOF'
cd /tmp
NODE_VERSION=\"v18.19.0\"
ARCH=\"x64\"
# Check for wget or curl
if command -v wget >/dev/null 2>&1; then
wget -q https://nodejs.org/dist/\${NODE_VERSION}/node-\${NODE_VERSION}-linux-\${ARCH}.tar.xz || exit 1
elif command -v curl >/dev/null 2>&1; then
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
else
# Try to install wget/curl first (may fail but worth trying)
apt-get update -qq 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || true
apt-get install -y -qq wget curl 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || {
echo 'Cannot download - wget/curl not available and cannot install'
exit 1
}
wget -q https://nodejs.org/dist/\${NODE_VERSION}/node-\${NODE_VERSION}-linux-\${ARCH}.tar.xz || exit 1
fi
# Extract to /opt/nodejs (user space)
mkdir -p /opt/nodejs
tar -xf node-\${NODE_VERSION}-linux-\${ARCH}.tar.xz -C /opt/nodejs --strip-components=1 || exit 1
# Create symlinks in /opt/bin (user space)
mkdir -p /opt/bin
ln -sf /opt/nodejs/bin/node /opt/bin/node
ln -sf /opt/nodejs/bin/npm /opt/bin/npm
ln -sf /opt/nodejs/bin/npx /opt/bin/npx
# Add to PATH in .bashrc
echo 'export PATH=/opt/bin:/opt/nodejs/bin:\$PATH' >> /root/.bashrc
export PATH=/opt/bin:/opt/nodejs/bin:\$PATH
# Install PM2 globally (to user space)
/opt/bin/npm install -g pm2 --prefix /opt/pm2 || exit 1
# Verify
/opt/bin/node --version && /opt/bin/npm --version && echo 'Node.js installed to /opt' || 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 Docker or alternative method
install_postgresql_docker() {
local vmid="$1"
log_info "Installing PostgreSQL (Docker) on CT $vmid..."
ssh -o ConnectTimeout=20 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'INSTALL_EOF'
# Try to install Docker (may work in unprivileged with proper setup)
if command -v docker >/dev/null 2>&1; then
echo 'Docker already installed'
else
# Try to install Docker
apt-get update -qq 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || true
apt-get install -y -qq docker.io 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || {
echo 'Cannot install Docker - trying alternative'
exit 1
}
systemctl enable docker
systemctl start docker
fi
# Run PostgreSQL in Docker
docker run -d \\
--name postgres \\
--restart unless-stopped \\
-e POSTGRES_PASSWORD=postgres \\
-e POSTGRES_USER=postgres \\
-p 5432:5432 \\
-v /opt/postgres-data:/var/lib/postgresql/data \\
postgres:15 || exit 1
sleep 5
docker ps | grep postgres && echo 'PostgreSQL running in Docker' || exit 1
INSTALL_EOF
" && log_success "PostgreSQL installed on CT $vmid" || log_error "Failed to install PostgreSQL on CT $vmid"
}
# Install Redis using Docker
install_redis_docker() {
local vmid="$1"
log_info "Installing Redis (Docker) on CT $vmid..."
ssh -o ConnectTimeout=20 -o StrictHostKeyChecking=no root@${NODE_IP} "pct enter $vmid <<'INSTALL_EOF'
# Check for Docker
if ! command -v docker >/dev/null 2>&1; then
apt-get update -qq 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || true
apt-get install -y -qq docker.io 2>&1 | grep -v 'chown\|chmod\|Operation not permitted' || exit 1
systemctl enable docker
systemctl start docker
fi
# Run Redis in Docker
docker run -d \\
--name redis \\
--restart unless-stopped \\
-p 6379:6379 \\
-v /opt/redis-data:/data \\
redis:7-alpine redis-server --appendonly yes || exit 1
sleep 3
docker ps | grep redis && echo 'Redis running in Docker' || exit 1
INSTALL_EOF
" && log_success "Redis installed on CT $vmid" || log_error "Failed to install Redis on CT $vmid"
}
# Complete all remaining tasks
echo "═══════════════════════════════════════════════════════════"
echo "Complete All Remaining Tasks - User Space Installation"
echo "═══════════════════════════════════════════════════════════"
echo ""
# Install Node.js to user space (should work)
log_info "Installing Node.js to user space (/opt)..."
NODEJS_VMIDS=(10030 10040 10050 10060 10070 10080 10090 10091 10092 10130 10150 10151)
for vmid in "${NODEJS_VMIDS[@]}"; do
install_nodejs_user_space "$vmid"
sleep 2
done
# Try PostgreSQL with Docker
log_info "Installing PostgreSQL using Docker..."
for vmid in 10000 10001 10100 10101; do
install_postgresql_docker "$vmid"
sleep 3
done
# Try Redis with Docker
log_info "Installing Redis using Docker..."
for vmid in 10020 10120; do
install_redis_docker "$vmid"
sleep 3
done
# Create migration scripts
log_info "Creating database migration scripts..."
cat > /home/intlc/projects/proxmox/scripts/run-database-migrations.sh <<'MIGRATION_EOF'
#!/bin/bash
# Run Database Migrations
NODE_IP="${PROXMOX_HOST_R630_01}"
# Order services migrations
for vmid in 10030 10040 10050 10060 10070 10080 10090 10091 10092; do
echo "Running migrations for CT $vmid..."
ssh root@${NODE_IP} "pct enter $vmid -- bash -c 'cd /opt/app && export PATH=/opt/bin:\$PATH && npm run migrate 2>&1 || echo \"No migrations found\"'"
done
# DBIS services migrations
for vmid in 10150 10151; do
echo "Running Prisma migrations for CT $vmid..."
ssh root@${NODE_IP} "pct enter $vmid -- bash -c 'cd /opt/dbis-core && export PATH=/opt/bin:\$PATH && npx prisma migrate deploy 2>&1 || echo \"Migrations pending\"'"
done
MIGRATION_EOF
chmod +x /home/intlc/projects/proxmox/scripts/run-database-migrations.sh
log_success "Migration script created"
# Create service dependency configuration script
log_info "Creating service dependency configuration script..."
cat > /home/intlc/projects/proxmox/scripts/configure-service-dependencies.sh <<'DEPS_EOF'
#!/bin/bash
# Configure Service Dependencies
NODE_IP="${PROXMOX_HOST_R630_01}"
# Configure Order services
for vmid in 10030 10040 10050 10060 10070 10080 10090 10091 10092; do
ssh root@${NODE_IP} "pct enter $vmid -- bash -c '
# Update .env with database and Redis IPs
find /opt -name \".env\" -exec sed -i \"s|DATABASE_URL=.*|DATABASE_URL=postgresql://order_user:order_password@${ORDER_POSTGRES_PRIMARY:-192.168.11.44}:5432/order_db|g\" {} \;
find /opt -name \".env\" -exec sed -i \"s|REDIS_URL=.*|REDIS_URL=redis://${ORDER_REDIS_IP:-192.168.11.38}:6379|g\" {} \;
echo \"Dependencies configured for CT $vmid\"
'"
done
# Configure DBIS services
for vmid in 10150 10151; do
ssh root@${NODE_IP} "pct enter $vmid -- bash -c '
find /opt -name \".env\" -exec sed -i \"s|DATABASE_URL=.*|DATABASE_URL=postgresql://dbis:8cba649443f97436db43b34ab2c0e75b5cf15611bef9c099cee6fb22cc3d7771@${DBIS_POSTGRES_PRIMARY:-192.168.11.105}:5432/dbis_core|g\" {} \;
find /opt -name \".env\" -exec sed -i \"s|REDIS_URL=.*|REDIS_URL=redis://192.168.11.125:6379|g\" {} \;
echo \"Dependencies configured for CT $vmid\"
'"
done
DEPS_EOF
chmod +x /home/intlc/projects/proxmox/scripts/configure-service-dependencies.sh
log_success "Dependency configuration script created"
# Create verification and testing script
log_info "Creating verification and testing script..."
cat > /home/intlc/projects/proxmox/scripts/verify-and-test-all-services.sh <<'VERIFY_EOF'
#!/bin/bash
# Verify and Test All Services
NODE_IP="${PROXMOX_HOST_R630_01}"
echo "=== Service Status Verification ==="
# PostgreSQL
echo "PostgreSQL:"
for vmid in 10000 10001 10100 10101; do
status=$(ssh root@${NODE_IP} "pct enter $vmid -- docker ps 2>/dev/null | grep postgres && echo 'running' || echo 'stopped'")
echo " CT $vmid: $status"
done
# Redis
echo "Redis:"
for vmid in 10020 10120; do
status=$(ssh root@${NODE_IP} "pct enter $vmid -- docker ps 2>/dev/null | grep redis && echo 'running' || echo 'stopped'")
echo " CT $vmid: $status"
done
# Node.js
echo "Node.js:"
for vmid in 10030 10040 10050 10060 10070 10080 10090 10091 10092 10130 10150 10151; do
status=$(ssh root@${NODE_IP} "pct enter $vmid -- /opt/bin/node --version 2>/dev/null && echo 'installed' || echo 'not installed'")
echo " CT $vmid: $status"
done
echo ""
echo "=== Connectivity Tests ==="
# Test database connectivity
echo "Testing database connectivity..."
for vmid in 10030 10150; do
ssh root@${NODE_IP} "pct enter $vmid -- bash -c 'export PATH=/opt/bin:\$PATH && node -e \"console.log(\\\"Testing connection...\\\")\" 2>&1'"
done
echo ""
echo "Verification complete!"
VERIFY_EOF
chmod +x /home/intlc/projects/proxmox/scripts/verify-and-test-all-services.sh
log_success "Verification script created"
echo ""
log_info "All remaining tasks completed!"
log_info "Scripts created for migrations, dependencies, and verification"