Files
loc_az_hci/scripts/vm-management/configure/apply-install-scripts.sh
defiQUG c39465c2bd
Some checks failed
Test / test (push) Has been cancelled
Initial commit: loc_az_hci (smom-dbis-138 excluded via .gitignore)
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 09:04:46 -08:00

197 lines
4.5 KiB
Bash
Executable File

#!/bin/bash
source ~/.bashrc
# Apply Install Scripts to VMs via SSH
# This script connects to each VM and runs the appropriate install script
set -e
# 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_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
# VM Configuration
declare -A VMS=(
[100]="cloudflare-tunnel:192.168.1.60:setup-cloudflare-tunnel.sh"
[101]="k3s-master:192.168.1.188:setup-k3s.sh"
[102]="git-server:192.168.1.121:setup-git-server.sh"
[103]="observability:192.168.1.82:setup-observability.sh"
)
SSH_USER="${SSH_USER:-ubuntu}"
SSH_KEY="${SSH_KEY:-~/.ssh/id_rsa}"
# Check if VM is reachable
check_vm_reachable() {
local ip=$1
local timeout=5
if ping -c 1 -W $timeout "$ip" > /dev/null 2>&1; then
return 0
else
return 1
fi
}
# Wait for VM to be ready
wait_for_vm() {
local ip=$1
local max_attempts=30
local attempt=0
log_info "Waiting for VM at $ip to be reachable..."
while [ $attempt -lt $max_attempts ]; do
if check_vm_reachable "$ip"; then
log_info "✓ VM is reachable"
return 0
fi
attempt=$((attempt + 1))
echo -n "."
sleep 2
done
echo ""
log_error "VM at $ip is not reachable after $max_attempts attempts"
return 1
}
# Check SSH connectivity
check_ssh() {
local ip=$1
local user=$2
if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no -i "$SSH_KEY" "${user}@${ip}" "echo 'SSH OK'" > /dev/null 2>&1; then
return 0
else
return 1
fi
}
# Wait for SSH
wait_for_ssh() {
local ip=$1
local user=$2
local max_attempts=60
local attempt=0
log_info "Waiting for SSH on $ip..."
while [ $attempt -lt $max_attempts ]; do
if check_ssh "$ip" "$user"; then
log_info "✓ SSH is ready"
return 0
fi
attempt=$((attempt + 1))
echo -n "."
sleep 5
done
echo ""
log_error "SSH not available after $max_attempts attempts"
return 1
}
# Apply install script to VM
apply_install_script() {
local vmid=$1
local name=$2
local ip=$3
local script=$4
log_step "Applying install script to VM $vmid: $name"
# Wait for VM to be ready
if ! wait_for_vm "$ip"; then
log_error "VM not reachable, skipping..."
return 1
fi
# Wait for SSH
if ! wait_for_ssh "$ip" "$SSH_USER"; then
log_error "SSH not available, skipping..."
return 1
fi
# Copy install script to VM
log_info "Copying install script to VM..."
if ! scp -o StrictHostKeyChecking=no -i "$SSH_KEY" "scripts/${script}" "${SSH_USER}@${ip}:/tmp/install-service.sh"; then
log_error "Failed to copy script"
return 1
fi
# Make script executable and run it
log_info "Running install script on VM..."
ssh -o StrictHostKeyChecking=no -i "$SSH_KEY" "${SSH_USER}@${ip}" <<EOF
sudo chmod +x /tmp/install-service.sh
sudo /tmp/install-service.sh
EOF
if [ $? -eq 0 ]; then
log_info "✓ Install script completed successfully"
return 0
else
log_error "✗ Install script failed"
return 1
fi
}
main() {
echo "========================================="
echo "Apply Install Scripts to VMs"
echo "========================================="
echo ""
if [ ! -f "$SSH_KEY" ]; then
log_error "SSH key not found: $SSH_KEY"
log_info "Set SSH_KEY environment variable or create key pair"
exit 1
fi
log_info "Using SSH key: $SSH_KEY"
log_info "SSH user: $SSH_USER"
echo ""
# Apply scripts to each VM
for vmid in 100 101 102 103; do
IFS=':' read -r name ip script <<< "${VMS[$vmid]}"
if apply_install_script "$vmid" "$name" "$ip" "$script"; then
log_info "✓ VM $vmid ($name) setup complete"
else
log_error "✗ Failed to setup VM $vmid"
fi
echo ""
done
log_info "========================================="
log_info "Install Script Application Complete"
log_info "========================================="
echo ""
log_info "All VMs should now have their services installed"
log_info "Check each VM to verify services are running"
}
main "$@"