#!/bin/bash source ~/.bashrc # Monitor VMs and Automatically Complete Tasks When Ready # This script continuously checks VM readiness and runs complete-all-vm-tasks.sh when ready set -e # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' BLUE='\033[0;34m' CYAN='\033[0;36m' 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 "${BLUE}[STEP]${NC} $1" } declare -A VMS=( [100]="cloudflare-tunnel:192.168.1.60" [101]="k3s-master:192.168.1.188" [102]="git-server:192.168.1.121" [103]="observability:192.168.1.82" ) SSH_USER="${SSH_USER:-ubuntu}" SSH_KEY="${SSH_KEY:-$HOME/.ssh/id_rsa}" CHECK_INTERVAL=30 # Check every 30 seconds MAX_WAIT=3600 # Maximum wait time: 1 hour check_all_vms_ready() { local all_ready=true for vmid in 100 101 102 103; do IFS=':' read -r name ip <<< "${VMS[$vmid]}" # Check ping if ! ping -c 1 -W 2 "$ip" > /dev/null 2>&1; then all_ready=false return 1 fi # Check SSH if ! ssh -o ConnectTimeout=3 -o StrictHostKeyChecking=no -i "$SSH_KEY" "${SSH_USER}@${ip}" "echo 'OK'" > /dev/null 2>&1; then all_ready=false return 1 fi done return 0 } main() { echo "=========================================" echo "VM Monitor - Auto-Complete Tasks" echo "=========================================" echo "" log_info "Monitoring VMs for readiness..." log_info "Will automatically run tasks when all VMs are ready" log_info "Checking every $CHECK_INTERVAL seconds" log_info "Maximum wait: $MAX_WAIT seconds (1 hour)" echo "" if [ ! -f "$SSH_KEY" ]; then log_error "SSH key not found: $SSH_KEY" exit 1 fi local start_time=$(date +%s) local check_count=0 while true; do check_count=$((check_count + 1)) local elapsed=$(($(date +%s) - start_time)) if [ $elapsed -gt $MAX_WAIT ]; then log_error "Maximum wait time exceeded" exit 1 fi echo -n "[Check $check_count] $(date '+%H:%M:%S') - " if check_all_vms_ready; then echo "" log_info "✓ All VMs are ready!" echo "" log_step "Running complete-all-vm-tasks.sh..." echo "" export SSH_KEY="$SSH_KEY" export SSH_USER="$SSH_USER" ./scripts/complete-all-vm-tasks.sh exit $? else echo "VMs not ready yet... (elapsed: ${elapsed}s)" sleep $CHECK_INTERVAL fi done } main "$@"