Files
loc_az_hci/scripts/azure-arc/onboard-proxmox-hosts.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

170 lines
4.5 KiB
Bash
Executable File

#!/bin/bash
source ~/.bashrc
# Azure Arc Onboarding Script for Proxmox Hosts
# Installs Azure Connected Machine Agent and connects Proxmox nodes to Azure
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Azure configuration (set via environment variables)
RESOURCE_GROUP="${RESOURCE_GROUP:-HC-Stack}"
TENANT_ID="${TENANT_ID:-}"
LOCATION="${LOCATION:-eastus}"
SUBSCRIPTION_ID="${SUBSCRIPTION_ID:-}"
CLOUD="${CLOUD:-AzureCloud}"
TAGS="${TAGS:-type=proxmox}"
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
check_root() {
if [ "$EUID" -ne 0 ]; then
log_error "Please run as root"
exit 1
fi
}
validate_config() {
if [ -z "$TENANT_ID" ] || [ -z "$SUBSCRIPTION_ID" ] || [ -z "$RESOURCE_GROUP" ]; then
log_error "Required Azure configuration missing"
log_info "Required environment variables:"
log_info " TENANT_ID - Azure tenant ID"
log_info " SUBSCRIPTION_ID - Azure subscription ID"
log_info " RESOURCE_GROUP - Azure resource group name"
log_info " LOCATION - Azure region (default: eastus)"
exit 1
fi
}
check_azure_cli() {
if ! command -v az &> /dev/null; then
log_error "Azure CLI not found. Please install it first:"
log_info " curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash"
exit 1
fi
if ! az account show &>/dev/null; then
log_error "Azure CLI not authenticated. Please run: az login"
exit 1
fi
}
install_arc_agent() {
log_info "Installing Azure Connected Machine Agent..."
# Check if already installed
if command -v azcmagent &> /dev/null; then
log_warn "Azure Arc agent already installed"
azcmagent version
return
fi
# Download and install agent
log_info "Downloading Azure Arc agent installer..."
wget -q https://aka.ms/azcmagent -O /tmp/install_linux_azcmagent.sh
chmod +x /tmp/install_linux_azcmagent.sh
log_info "Running installer..."
/tmp/install_linux_azcmagent.sh
# Verify installation
if command -v azcmagent &> /dev/null; then
log_info "Azure Arc agent installed successfully"
azcmagent version
else
log_error "Failed to install Azure Arc agent"
exit 1
fi
}
connect_to_azure() {
log_info "Connecting machine to Azure Arc..."
# Check if already connected
if azcmagent show &>/dev/null; then
log_warn "Machine already connected to Azure Arc"
azcmagent show
read -p "Reconnect? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
return
fi
azcmagent disconnect --force-local-only
fi
# Connect to Azure
log_info "Connecting to Azure..."
log_info " Resource Group: $RESOURCE_GROUP"
log_info " Location: $LOCATION"
log_info " Subscription: $SUBSCRIPTION_ID"
azcmagent connect \
--resource-group "$RESOURCE_GROUP" \
--tenant-id "$TENANT_ID" \
--location "$LOCATION" \
--subscription-id "$SUBSCRIPTION_ID" \
--cloud "$CLOUD" \
--tags "$TAGS" \
--correlation-id "proxmox-onboarding-$(date +%s)"
if [ $? -eq 0 ]; then
log_info "Successfully connected to Azure Arc"
else
log_error "Failed to connect to Azure Arc"
exit 1
fi
}
verify_connection() {
log_info "Verifying Azure Arc connection..."
# Show agent status
azcmagent show
# Verify in Azure Portal (via Azure CLI)
log_info "Verifying registration in Azure..."
MACHINE_NAME=$(hostname)
if az connectedmachine show \
--resource-group "$RESOURCE_GROUP" \
--name "$MACHINE_NAME" &>/dev/null; then
log_info "Machine found in Azure Portal"
az connectedmachine show \
--resource-group "$RESOURCE_GROUP" \
--name "$MACHINE_NAME" \
--query "{name:name, location:location, status:status}" -o table
else
log_warn "Machine not yet visible in Azure Portal (may take a few minutes)"
fi
}
main() {
log_info "Starting Azure Arc onboarding for Proxmox host..."
check_root
validate_config
check_azure_cli
install_arc_agent
connect_to_azure
verify_connection
log_info "Azure Arc onboarding completed successfully!"
log_info "View your machine in Azure Portal:"
log_info " https://portal.azure.com/#view/Microsoft_Azure_HybridCompute/MachinesBlade"
}
main "$@"