Files
wild-cloud/bin/wild-cluster-node-image-create
2025-06-30 09:16:21 -07:00

266 lines
9.8 KiB
Plaintext
Executable File

#\!/bin/bash
set -e
set -o pipefail
# Usage function
usage() {
echo "Usage: wild-cluster-node-image-create [options]"
echo ""
echo "Generate custom Talos installer image URLs for cluster nodes."
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo ""
echo "This script will:"
echo " - Configure basic cluster settings if needed"
echo " - Generate custom Talos installer image URL"
echo " - Display the installer URL for PXE boot or ISO creation"
echo ""
echo "Requirements:"
echo " - Must be run from a wild-cloud directory"
echo " - Requires Talos version and schematic ID configuration"
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
exit 0
;;
-*)
echo "Unknown option $1"
usage
exit 1
;;
*)
echo "Unexpected argument: $1"
usage
exit 1
;;
esac
done
# Initialize Wild-Cloud environment
if [ -z "${WC_ROOT}" ]; then
print "WC_ROOT is not set."
exit 1
else
source "${WC_ROOT}/scripts/common.sh"
init_wild_env
fi
# Configure basic settings if needed
if [ ! -f "${WC_HOME}/config.yaml" ] || [ -z "$(get_current_config "operator.email")" ]; then
print_header "Basic Configuration"
# Detect current network for suggestions
CURRENT_IP=$(ip route get 8.8.8.8 | awk '{print $7; exit}' 2>/dev/null || echo "192.168.1.100")
GATEWAY_IP=$(ip route | grep default | awk '{print $3; exit}' 2>/dev/null || echo "192.168.1.1")
SUBNET_PREFIX=$(echo "${CURRENT_IP}" | cut -d. -f1-3)
print_info "Detected network: ${SUBNET_PREFIX}.x (gateway: ${GATEWAY_IP})"
echo "This will configure basic settings for your wild-cloud deployment."
echo ""
# Basic Information
current_email=$(get_current_config "operator.email")
email=$(prompt_with_default "Your email address (for Let's Encrypt certificates)" "" "${current_email}")
wild-config-set "operator.email" "${email}"
# Domain Configuration
current_base_domain=$(get_current_config "cloud.baseDomain")
base_domain=$(prompt_with_default "Your base domain name (e.g., example.com)" "" "${current_base_domain}")
wild-config-set "cloud.baseDomain" "${base_domain}"
current_domain=$(get_current_config "cloud.domain")
domain=$(prompt_with_default "Your public cloud domain" "cloud.${base_domain}" "${current_domain}")
wild-config-set "cloud.domain" "${domain}"
current_internal_domain=$(get_current_config "cloud.internalDomain")
internal_domain=$(prompt_with_default "Your internal cloud domain" "internal.${domain}" "${current_internal_domain}")
wild-config-set "cloud.internalDomain" "${internal_domain}"
# Derive cluster name from domain
cluster_name=$(echo "${domain}" | tr '.' '-' | tr '[:upper:]' '[:lower:]')
wild-config-set "cluster.name" "${cluster_name}"
print_info "Set cluster name to: ${cluster_name}"
print_success "Basic configuration completed"
echo ""
fi
# Configure cluster settings if needed
if [ -z "$(get_current_config "cluster.nodes.talos.version")" ] || [ -z "$(get_current_config "cluster.nodes.talos.schematicId")" ]; then
print_header "Kubernetes Cluster Configuration"
current_talos_version=$(get_current_config "cluster.nodes.talos.version")
talos_version=$(prompt_with_default "Talos version" "v1.10.4" "${current_talos_version}")
wild-config-set "cluster.nodes.talos.version" "${talos_version}"
# Talos schematic ID
current_schematic_id=$(get_current_config "cluster.nodes.talos.schematicId")
echo ""
print_info "Get your Talos schematic ID from: https://factory.talos.dev/"
print_info "This customizes Talos with the drivers needed for your hardware."
# Look up default schematic ID from talos-schemas.yaml
default_schematic_id=""
schemas_file="${WC_ROOT}/setup/cluster-nodes/talos-schemas.yaml"
if [ -f "$schemas_file" ]; then
default_schematic_id=$(yq eval ".talos-schemas.\"${talos_version}\"" "$schemas_file" 2>/dev/null)
if [ -n "$default_schematic_id" ] && [ "$default_schematic_id" != "null" ]; then
print_info "Default schematic ID available for Talos $talos_version"
else
default_schematic_id=""
fi
fi
schematic_id=$(prompt_with_default "Talos schematic ID" "${default_schematic_id}" "${current_schematic_id}")
wild-config-set "cluster.nodes.talos.schematicId" "${schematic_id}"
print_success "Cluster configuration completed"
echo ""
fi
# =============================================================================
# INSTALLER IMAGE GENERATION AND ASSET DOWNLOADING
# =============================================================================
print_header "Talos Installer Image Generation and Asset Download"
# Get Talos version and schematic ID from config
TALOS_VERSION=$(get_current_config cluster.nodes.talos.version)
SCHEMATIC_ID=$(get_current_config cluster.nodes.talos.schematicId)
print_info "Creating custom Talos installer image..."
print_info "Talos version: $TALOS_VERSION"
# Check if schematic ID exists
if [ -z "$SCHEMATIC_ID" ] || [ "$SCHEMATIC_ID" = "null" ]; then
print_error "No schematic ID found in config.yaml"
print_info "You can get a schematic ID from: https://factory.talos.dev/"
# Look up default schematic ID from talos-schemas.yaml
fallback_default=""
schemas_file="${WC_ROOT}/setup/cluster-nodes/talos-schemas.yaml"
if [ -f "$schemas_file" ]; then
fallback_default=$(yq eval ".talos-schemas.\"${TALOS_VERSION}\"" "$schemas_file" 2>/dev/null)
if [ -n "$fallback_default" ] && [ "$fallback_default" != "null" ]; then
print_info "Default schematic ID available for Talos $TALOS_VERSION"
read -p "Enter schematic ID [$fallback_default]: " -r SCHEMATIC_ID
if [ -z "$SCHEMATIC_ID" ]; then
SCHEMATIC_ID="$fallback_default"
fi
else
read -p "Enter schematic ID: " -r SCHEMATIC_ID
fi
else
read -p "Enter schematic ID: " -r SCHEMATIC_ID
fi
if [ -n "$SCHEMATIC_ID" ]; then
wild-config-set "cluster.nodes.talos.schematicId" "$SCHEMATIC_ID"
else
print_error "Schematic ID required for installer image generation"
exit 1
fi
fi
print_info "Schematic ID: $SCHEMATIC_ID"
if [ -f "${WC_HOME}/config.yaml" ] && yq eval '.cluster.nodes.talos.schematic.customization.systemExtensions.officialExtensions' "${WC_HOME}/config.yaml" >/dev/null 2>&1; then
echo ""
print_info "Schematic includes:"
yq eval '.cluster.nodes.talos.schematic.customization.systemExtensions.officialExtensions[]' "${WC_HOME}/config.yaml" | sed 's/^/ - /' || true
echo ""
fi
# Generate installer image URL
INSTALLER_URL="factory.talos.dev/metal-installer/$SCHEMATIC_ID:$TALOS_VERSION"
print_success "Custom installer image URL generated!"
echo ""
print_info "Installer URL: $INSTALLER_URL"
# =============================================================================
# ASSET DOWNLOADING AND CACHING
# =============================================================================
print_header "Downloading and Caching PXE Boot Assets"
# Create cache directories
CACHE_DIR="${WC_HOME}/.wildcloud"
PXE_CACHE_DIR="${CACHE_DIR}/pxe"
IPXE_CACHE_DIR="${CACHE_DIR}/ipxe"
mkdir -p "$PXE_CACHE_DIR/amd64"
mkdir -p "$IPXE_CACHE_DIR"
# Download Talos kernel and initramfs for PXE boot
print_info "Downloading Talos PXE assets..."
KERNEL_URL="https://pxe.factory.talos.dev/image/${SCHEMATIC_ID}/${TALOS_VERSION}/kernel-amd64"
INITRAMFS_URL="https://pxe.factory.talos.dev/image/${SCHEMATIC_ID}/${TALOS_VERSION}/initramfs-amd64.xz"
KERNEL_PATH="${PXE_CACHE_DIR}/amd64/vmlinuz"
INITRAMFS_PATH="${PXE_CACHE_DIR}/amd64/initramfs.xz"
# Function to download with progress
download_asset() {
local url="$1"
local path="$2"
local description="$3"
if [ -f "$path" ]; then
print_info "$description already cached at $path"
return 0
fi
print_info "Downloading $description..."
print_info "URL: $url"
if command -v wget >/dev/null 2>&1; then
wget --progress=bar:force -O "$path" "$url"
elif command -v curl >/dev/null 2>&1; then
curl -L --progress-bar -o "$path" "$url"
else
print_error "Neither wget nor curl is available for downloading"
return 1
fi
# Verify download
if [ ! -f "$path" ] || [ ! -s "$path" ]; then
print_error "Download failed or file is empty: $path"
rm -f "$path"
return 1
fi
print_success "$description downloaded successfully"
}
# Download Talos PXE assets
download_asset "$KERNEL_URL" "$KERNEL_PATH" "Talos kernel"
download_asset "$INITRAMFS_URL" "$INITRAMFS_PATH" "Talos initramfs"
# Download iPXE bootloader files
print_info "Downloading iPXE bootloader assets..."
download_asset "http://boot.ipxe.org/ipxe.efi" "${IPXE_CACHE_DIR}/ipxe.efi" "iPXE EFI bootloader"
download_asset "http://boot.ipxe.org/undionly.kpxe" "${IPXE_CACHE_DIR}/undionly.kpxe" "iPXE BIOS bootloader"
download_asset "http://boot.ipxe.org/arm64-efi/ipxe.efi" "${IPXE_CACHE_DIR}/ipxe-arm64.efi" "iPXE ARM64 EFI bootloader"
echo ""
print_success "All assets downloaded and cached!"
echo ""
print_info "Cached assets:"
echo " Talos kernel: $KERNEL_PATH"
echo " Talos initramfs: $INITRAMFS_PATH"
echo " iPXE EFI: ${IPXE_CACHE_DIR}/ipxe.efi"
echo " iPXE BIOS: ${IPXE_CACHE_DIR}/undionly.kpxe"
echo " iPXE ARM64: ${IPXE_CACHE_DIR}/ipxe-arm64.efi"
echo ""
print_info "Use this URL for:"
echo " - PXE boot configuration (update boot.ipxe kernel line)"
echo " - ISO creation: curl -LO https://$INSTALLER_URL"
echo " - USB creation: dd if=talos-installer.iso of=/dev/sdX"
echo ""
print_success "Installer image generation and asset caching completed!"