#!/bin/bash set -e # Exit immediately if a command exits with a non-zero status. set -u # Treat unset variables as an error. # --- Configuration --- # VM and Project Details CPU_VM_NAME="osvauco-cpu-vm" CPU_VM_ZONE="us-central1-b" CPU_IP_NAME="osvauco-cpu-ip" CPU_REGION="us-central1" GPU_VM_NAME="osvauco-gpu-vm" GPU_VM_ZONE="europe-west4-c" GPU_IP_NAME="osvauco-gpu-ip" GPU_REGION="europe-west4" PROJECT_ID="propane-will-491900-m5" VM_USER="chris_christiansen" # Local SSH Configuration (for WSL/Git Bash on Windows) # IMPORTANT: This path assumes you are running this script from WSL or a similar environment. SSH_CONFIG_PATH="/mnt/c/Users/Cvias/.ssh/config" SSH_KEY_PATH="C:\Users\Cvias\.ssh\google_compute_engine" # Git Remotes for Verification on GPU VM GITEA_REMOTE="http://34.158.137.245:3000/chris/osvauco.git" GITHUB_REMOTE="git@github.com:vauco/osvauco.git" # --- Helper Functions --- # Function to print messages log() { echo "--- $1 ---" } # Pre-flight check to ensure a static IP has been reserved. preflight_check_ip() { local ip_name="$1" local region="$2" log "Pre-flight check for static IP: $ip_name" if gcloud compute addresses describe "$ip_name" --project="$PROJECT_ID" --region="$region" --format="value(name)" >/dev/null 2>&1; then echo "✅ Static IP '$ip_name' exists." else echo "❌ ERROR: Static IP '$ip_name' not found in region '$region'." echo "This script requires a reserved static IP to function reliably." echo "" echo "To fix this, please run the following command:" echo "gcloud compute addresses create \"$ip_name\" --project=\"$PROJECT_ID\" --region=\"$region\" --network-tier=STANDARD" echo "" echo "After creating the IP, ensure it is assigned to the correct VM, then re-run this script." exit 1 fi } # Function to start a VM if it's not running start_vm() { local vm_name="$1" local zone="$2" log "Checking status of $vm_name" # Get the current status of the VM. # The `|| echo "TERMINATED"` part handles cases where gcloud might fail if the VM is in a weird state. local status=$(gcloud compute instances describe "$vm_name" --project="$PROJECT_ID" --zone="$zone" --format="get(status)" || echo "TERMINATED") if [ "$status" == "RUNNING" ]; then echo "✅ $vm_name is already running." else echo "VM is $status. Starting $vm_name..." gcloud compute instances start "$vm_name" --project="$PROJECT_ID" --zone="$zone" echo "✅ $vm_name started." fi } # Function to get a reserved static IP address get_static_ip() { local ip_name="$1" local region="$2" gcloud compute addresses describe "$ip_name" --project="$PROJECT_ID" --region="$region" --format="get(address)" } # --- Main Script --- # 0. Pre-flight checks for required infrastructure preflight_check_ip "$CPU_IP_NAME" "$CPU_REGION" preflight_check_ip "$GPU_IP_NAME" "$GPU_REGION" # 1. Start VMs start_vm "$CPU_VM_NAME" "$CPU_VM_ZONE" start_vm "$GPU_VM_NAME" "$GPU_VM_ZONE" # 2. Get Static IPs log "Fetching static IP addresses" CPU_IP=$(get_static_ip "$CPU_IP_NAME" "$CPU_REGION") GPU_IP=$(get_static_ip "$GPU_IP_NAME" "$GPU_REGION") echo " - CPU VM IP: $CPU_IP" echo " - GPU VM IP: $GPU_IP" # 3. Update SSH Config on Windows log "Updating SSH config file at $SSH_CONFIG_PATH" if [ ! -f "$SSH_CONFIG_PATH" ]; then echo "⚠️ SSH config file not found. Creating it." touch "$SSH_CONFIG_PATH" fi # We use a marker block to safely update the SSH config. # This is safer than trying to edit lines in place. MARKER_BEGIN="# BEGIN OSVAUCO-MANAGED-BLOCK" MARKER_END="# END OSVAUCO-MANAGED-BLOCK" # Create a temporary file to build the new config temp_config=$(mktemp) # Copy the config content, excluding the old managed block awk -v begin="$MARKER_BEGIN" -v end="$MARKER_END" ' $0 == begin {in_block=1} !in_block {print} $0 == end {in_block=0} ' "$SSH_CONFIG_PATH" > "$temp_config" # Append the new, updated block to the temporary file cat <> "$temp_config" $MARKER_BEGIN # This block is automatically managed by dev-start.sh # Do not edit manually! Host osvauco-cpu HostName $CPU_IP User $VM_USER IdentityFile $SSH_KEY_PATH Host osvauco-gpu HostName $GPU_IP User $VM_USER IdentityFile $SSH_KEY_PATH $MARKER_END EOT # Overwrite the original config file with the updated one mv "$temp_config" "$SSH_CONFIG_PATH" echo "✅ SSH config updated successfully." # 4. Print Connect Commands log "Setup Complete! You can now connect using:" echo "" echo " ssh osvauco-cpu" echo " ssh osvauco-gpu" echo "" # 5. Verify Git Remotes on the GPU VM log "Verifying Git remotes on $GPU_VM_NAME" # This command runs non-interactively on the remote VM. gcloud compute ssh "${VM_USER}@${GPU_VM_NAME}" --zone="${GPU_VM_ZONE}" --project="${PROJECT_ID}" --command=" set -e echo '--- Running remote verification ---' cd ~/OSVauco || { echo 'Error: ~/OSVauco directory not found.'; exit 1; } # Check and fix 'origin' remote ORIGIN_URL=\$(git remote get-url origin 2>/dev/null || echo '') if [ "\$ORIGIN_URL" == "${GITEA_REMOTE}" ]; then echo '✅ Git remote "origin" is correct.' else echo '⚠️ Git remote "origin" is incorrect or missing. Setting it...' if [ -n "\$ORIGIN_URL" ]; then git remote set-url origin "${GITEA_REMOTE}" else git remote add origin "${GITEA_REMOTE}" fi echo '✅ Remote "origin" set to: ${GITEA_REMOTE}' fi # Check and fix 'github' remote GITHUB_URL=\$(git remote get-url github 2>/dev/null || echo '') if [ "\$GITHUB_URL" == "${GITHUB_REMOTE}" ]; then echo '✅ Git remote "github" is correct.' else echo '⚠️ Git remote "github" is incorrect or missing. Setting it...' if [ -n "\$GITHUB_URL" ]; then git remote set-url github "${GITHUB_REMOTE}" else git remote add github "${GITHUB_REMOTE}" fi echo '✅ Remote "github" set to: ${GITHUB_REMOTE}' fi echo '--- Remote verification complete ---' " log "All tasks finished."