Files
proxmox/scripts/copy-project-to-vm.sh

155 lines
5.7 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Copy Project Files to Proxmox VM
# Uses SCP to copy necessary files for deployment
set -euo pipefail
# Load IP configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
PROXMOX_USER="${PROXMOX_USER:-root}"
VMID="${VMID:-2101}"
VM_USER="${VM_USER:-intlc}"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_section() { echo -e "\n${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"; echo -e "${CYAN}$1${NC}"; echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}\n"; }
log_section "Copy Project Files to VM"
# Get VM IP from Proxmox
log_info "Getting VM IP address..."
VM_IP=$(ssh "${PROXMOX_USER}@${PROXMOX_HOST}" "pct config $VMID | grep '^net0:' | sed 's/.*ip=\([^,]*\).*/\1/'" 2>&1 || echo "${RPC_CORE_1:-${IP_SERVICE_21:-${IP_SERVICE_21:-${IP_SERVICE_21:-${IP_SERVICE_21:-${IP_SERVICE_21:-192.168.11.21}}}}}1}")
log_info "VM IP: $VM_IP"
# Test SSH to VM
log_info "Testing SSH connection to VM..."
if ssh -o ConnectTimeout=5 "${VM_USER}@${VM_IP}" "echo 'Connected'" 2>/dev/null; then
log_success "SSH connection to VM works"
USE_VM_SSH=true
else
log_warn "Direct SSH to VM not available, using pct exec"
USE_VM_SSH=false
fi
# Function to copy file via pct exec
copy_via_pct() {
local src="$1"
local dst="$2"
local content=$(cat "$src")
ssh "${PROXMOX_USER}@${PROXMOX_HOST}" "pct exec $VMID -- bash -c 'mkdir -p \$(dirname \"$dst\") && cat > \"$dst\" <<EOF
$content
EOF
'" 2>&1
}
# Function to copy file via SCP
copy_via_scp() {
local src="$1"
local dst="$2"
scp "$src" "${VM_USER}@${VM_IP}:$dst" 2>&1
}
# Step 1: Create directory structure
log_section "Step 1: Create Directory Structure"
log_info "Creating project directories in VM..."
if [ "$USE_VM_SSH" = true ]; then
ssh "${VM_USER}@${VM_IP}" "mkdir -p /home/$VM_USER/projects/proxmox/smom-dbis-138 /home/$VM_USER/projects/proxmox/scripts" 2>&1
else
ssh "${PROXMOX_USER}@${PROXMOX_HOST}" "pct exec $VMID -- bash -c 'mkdir -p /home/$VM_USER/projects/proxmox/smom-dbis-138 /home/$VM_USER/projects/proxmox/scripts'" 2>&1
fi
log_success "Directories created"
# Step 2: Copy .env file
log_section "Step 2: Copy Environment File"
if [ -f "smom-dbis-138/.env" ]; then
log_info "Copying .env file..."
if [ "$USE_VM_SSH" = true ]; then
copy_via_scp "smom-dbis-138/.env" "/home/$VM_USER/projects/proxmox/smom-dbis-138/.env"
else
copy_via_pct "smom-dbis-138/.env" "/home/$VM_USER/projects/proxmox/smom-dbis-138/.env"
fi
log_success ".env file copied"
else
log_warn ".env file not found"
fi
# Step 3: Copy deployment scripts
log_section "Step 3: Copy Deployment Scripts"
for script in "scripts/deploy-all-bridges-standalone.sh" "scripts/calculate-chain138-gas-price.sh"; do
if [ -f "$script" ]; then
log_info "Copying $(basename $script)..."
if [ "$USE_VM_SSH" = true ]; then
copy_via_scp "$script" "/home/$VM_USER/projects/proxmox/$script"
ssh "${VM_USER}@${VM_IP}" "chmod +x /home/$VM_USER/projects/proxmox/$script" 2>&1
else
copy_via_pct "$script" "/home/$VM_USER/projects/proxmox/$script"
ssh "${PROXMOX_USER}@${PROXMOX_HOST}" "pct exec $VMID -- chmod +x /home/$VM_USER/projects/proxmox/$script" 2>&1
fi
log_success "$(basename $script) copied"
fi
done
# Step 4: Copy contract source files (essential directories)
log_section "Step 4: Copy Contract Source Files"
log_info "Copying contract source files (this may take a while)..."
# Copy essential directories
for dir in "smom-dbis-138/contracts" "smom-dbis-138/script" "smom-dbis-138/foundry.toml" "smom-dbis-138/remappings.txt"; do
if [ -e "$dir" ]; then
log_info "Copying $dir..."
if [ "$USE_VM_SSH" = true ]; then
if [ -d "$dir" ]; then
scp -r "$dir" "${VM_USER}@${VM_IP}:/home/$VM_USER/projects/proxmox/$(dirname $dir)/" 2>&1 | tail -5
else
copy_via_scp "$dir" "/home/$VM_USER/projects/proxmox/$dir"
fi
else
# For pct exec, we need to use tar
if [ -d "$dir" ]; then
tar czf - "$dir" | ssh "${PROXMOX_USER}@${PROXMOX_HOST}" "pct exec $VMID -- bash -c 'cd /home/$VM_USER/projects/proxmox && tar xzf -'" 2>&1 | tail -5
else
copy_via_pct "$dir" "/home/$VM_USER/projects/proxmox/$dir"
fi
fi
log_success "$dir copied"
fi
done
# Step 5: Copy config files
log_section "Step 5: Copy Configuration Files"
if [ -d "smom-dbis-138/config" ]; then
log_info "Copying config directory..."
if [ "$USE_VM_SSH" = true ]; then
scp -r "smom-dbis-138/config" "${VM_USER}@${VM_IP}:/home/$VM_USER/projects/proxmox/smom-dbis-138/" 2>&1 | tail -5
else
tar czf - "smom-dbis-138/config" | ssh "${PROXMOX_USER}@${PROXMOX_HOST}" "pct exec $VMID -- bash -c 'cd /home/$VM_USER/projects/proxmox && tar xzf -'" 2>&1 | tail -5
fi
log_success "Config directory copied"
fi
log_section "Copy Complete"
log_success "✅ Project files copied to VM"
log_info "Next: Run deployment script in VM"