#!/bin/bash source ~/.bashrc # Configure All Services on VMs # Run this script after VMs have booted and are accessible via SSH set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" # VM IP addresses CLOUDFLARE_IP="192.168.1.60" K3S_IP="192.168.1.188" GIT_IP="192.168.1.121" OBSERVABILITY_IP="192.168.1.82" # SSH user (default for Ubuntu cloud images) SSH_USER="${SSH_USER:-ubuntu}" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${GREEN}[INFO]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } log_step() { echo -e "\n${BLUE}=== $1 ===${NC}" } execute_remote() { local host=$1 local command=$2 local description=$3 log_info "$description on $host" if ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 "$SSH_USER@$host" "$command"; then log_info "✓ $description completed on $host" return 0 else log_error "✗ $description failed on $host" return 1 fi } copy_file_remote() { local host=$1 local source=$2 local dest=$3 log_info "Copying $source to $SSH_USER@$host:$dest" scp -o StrictHostKeyChecking=no "$source" "$SSH_USER@$host:$dest" } # Configure Cloudflare Tunnel configure_cloudflare() { log_step "Configuring Cloudflare Tunnel on VM 100" execute_remote "$CLOUDFLARE_IP" \ "curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o /usr/local/bin/cloudflared && chmod +x /usr/local/bin/cloudflared" \ "Install cloudflared" log_warn "Cloudflare Tunnel authentication requires manual steps:" log_warn " 1. SSH to $CLOUDFLARE_IP" log_warn " 2. Run: cloudflared tunnel login" log_warn " 3. Create tunnel: cloudflared tunnel create azure-stack-hci" log_warn " 4. Configure routes and systemd service" } # Configure K3s configure_k3s() { log_step "Configuring K3s on VM 101" execute_remote "$K3S_IP" \ "curl -sfL https://get.k3s.io | sh -" \ "Install K3s" execute_remote "$K3S_IP" \ "kubectl get nodes" \ "Verify K3s installation" log_info "K3s kubeconfig location: /etc/rancher/k3s/k3s.yaml" } # Configure Git Server configure_git() { log_step "Configuring Git Server on VM 102" # Check if setup script exists if [ -f "$PROJECT_ROOT/infrastructure/gitops/gitea-deploy.sh" ]; then copy_file_remote "$GIT_IP" \ "$PROJECT_ROOT/infrastructure/gitops/gitea-deploy.sh" \ "/tmp/gitea-deploy.sh" execute_remote "$GIT_IP" \ "chmod +x /tmp/gitea-deploy.sh && sudo /tmp/gitea-deploy.sh" \ "Deploy Gitea" else log_warn "Gitea deployment script not found, manual installation required" fi } # Configure Observability configure_observability() { log_step "Configuring Observability Stack on VM 103" # Install Prometheus execute_remote "$OBSERVABILITY_IP" \ "sudo apt-get update && sudo apt-get install -y prometheus" \ "Install Prometheus" # Install Grafana execute_remote "$OBSERVABILITY_IP" \ "sudo apt-get install -y apt-transport-https software-properties-common wget && wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add - && echo 'deb https://packages.grafana.com/oss/deb stable main' | sudo tee -a /etc/apt/sources.list.d/grafana.list && sudo apt-get update && sudo apt-get install -y grafana && sudo systemctl enable grafana-server && sudo systemctl start grafana-server" \ "Install Grafana" log_info "Grafana should be accessible at http://$OBSERVABILITY_IP:3000" log_info "Default credentials: admin/admin" } main() { log_info "Configuring all services on VMs" log_warn "This script requires SSH access to all VMs" log_warn "Ensure VMs have booted and are accessible" # Test connectivity log_info "Testing VM connectivity..." for ip in "$CLOUDFLARE_IP" "$K3S_IP" "$GIT_IP" "$OBSERVABILITY_IP"; do if ! ping -c 1 -W 2 "$ip" &> /dev/null; then log_error "Cannot reach $ip - VM may not be ready" log_warn "Wait for VMs to fully boot and try again" exit 1 fi done log_info "All VMs are reachable" # Configure services configure_cloudflare configure_k3s configure_git configure_observability log_info "Service configuration completed!" log_warn "Some services may require additional manual configuration" } main "$@"