Extracts common library from wild scripts.

This commit is contained in:
2025-06-28 08:18:11 -07:00
parent 0399bd54cf
commit bcccb150e3
11 changed files with 324 additions and 804 deletions

View File

@@ -3,102 +3,11 @@
set -e
set -o pipefail
# Get WC_ROOT (where this script and templates live)
WC_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
export WC_ROOT
# Source common utilities
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/wild-common.sh"
# Set up cloud directory (WC_HOME is where user's cloud will be)
WC_HOME="$(pwd)"
export WC_HOME
# =============================================================================
# HELPER FUNCTIONS
# =============================================================================
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Helper functions
print_header() {
echo -e "\n${BLUE}=== $1 ===${NC}\n"
}
print_info() {
echo -e "${BLUE}INFO:${NC} $1"
}
print_warning() {
echo -e "${YELLOW}WARNING:${NC} $1"
}
print_success() {
echo -e "${GREEN}SUCCESS:${NC} $1"
}
print_error() {
echo -e "${RED}ERROR:${NC} $1"
}
# Function to prompt for input with default value
prompt_with_default() {
local prompt="$1"
local default="$2"
local current_value="$3"
local result
if [ -n "${current_value}" ] && [ "${current_value}" != "null" ]; then
printf "%s [current: %s]: " "${prompt}" "${current_value}" >&2
read -r result
if [ -z "${result}" ]; then
result="${current_value}"
fi
elif [ -n "${default}" ]; then
printf "%s [default: %s]: " "${prompt}" "${default}" >&2
read -r result
if [ -z "${result}" ]; then
result="${default}"
fi
else
printf "%s: " "${prompt}" >&2
read -r result
while [ -z "${result}" ]; do
printf "This value is required. Please enter a value: " >&2
read -r result
done
fi
echo "${result}"
}
# Function to get current config value safely
get_current_config() {
local key="$1"
if [ -f "${WC_HOME}/config.yaml" ]; then
set +e
result=$(wild-config "${key}" 2>/dev/null)
set -e
echo "${result}"
else
echo ""
fi
}
# Function to get current secret value safely
get_current_secret() {
local key="$1"
if [ -f "${WC_HOME}/secrets.yaml" ]; then
set +e
result=$(wild-secret "${key}" 2>/dev/null)
set -e
echo "${result}"
else
echo ""
fi
}
# Initialize Wild-Cloud environment
init_wild_env
# Phase tracking variables
SKIP_INSTALLER=false
@@ -125,17 +34,12 @@ while [[ $# -gt 0 ]]; do
echo ""
echo "Set up Kubernetes cluster infrastructure (Phases 1-3)."
echo ""
echo "Phase Control Options:"
echo " --skip-installer Skip Phase 1 (Installer image generation)"
echo " --skip-hardware Skip Phase 2 (Node hardware detection)"
echo " --skip-configs Skip Phase 3 (Machine config generation)"
echo "Control Options:"
echo " --skip-installer Skip Installer image generation"
echo " --skip-hardware Skip Node hardware detection"
echo " --skip-configs Skip Machine config generation"
echo " -h, --help Show this help message"
echo ""
echo "Phases:"
echo " 1. Installer image - Generate custom Talos installer URLs"
echo " 2. Hardware detection - Discover node interfaces and disks"
echo " 3. Machine configs - Generate Talos machine configurations"
echo ""
echo "Prerequisites:"
echo " - Run 'wild-setup-scaffold' first to initialize the cloud"
echo ""
@@ -159,45 +63,37 @@ while [[ $# -gt 0 ]]; do
done
# Check if we're in a wild-cloud directory
if [ ! -d ".wildcloud" ]; then
print_error "You must run this script from a wild-cloud directory"
print_info "Run 'wild-setup-scaffold' first to initialize a wild-cloud project"
exit 1
fi
check_wild_directory
# Check basic configuration
if [ -z "$(get_current_config "operator.email")" ]; then
print_error "Basic configuration is missing"
print_info "Run 'wild-setup-scaffold' first to configure basic settings"
exit 1
fi
check_basic_config
print_header "Wild-Cloud Cluster Setup"
print_info "Setting up Kubernetes cluster infrastructure (Phases 1-3)"
print_info "Setting up cluster infrastructure"
echo ""
# =============================================================================
# PHASE 1: Talos asset download
# Talos asset download
# =============================================================================
if [ "${SKIP_INSTALLER}" = false ]; then
print_header "Phase 1: Installer Image Generation"
print_header "Installer Image Generation"
print_info "Running wild-cluster-node-image-create..."
wild-cluster-node-image-create
print_success "Phase 1 completed: Installer image generated"
print_success "Installer image generated"
echo ""
else
print_info "Skipping Phase 1: Installer Image Generation"
print_info "Skipping: Installer Image Generation"
fi
# =============================================================================
# PHASE 2: Node Hardware Detection
# Node Hardware Detection
# =============================================================================
if [ "${SKIP_HARDWARE}" = false ]; then
print_header "Phase 2: Node Hardware Detection"
print_header "Node Hardware Detection"
# Configure network settings
if [ -z "$(get_current_config "cloud.router.ip")" ]; then
@@ -335,10 +231,10 @@ if [ "${SKIP_HARDWARE}" = false ]; then
echo ""
fi
# Generate initial cluster configuration
print_header "Cluster Configuration Generation"
print_info "Generating base cluster configuration with talosctl gen config..."
wild-cluster-config-generate
# # Generate initial cluster configuration
# print_header "Cluster Configuration Generation"
# print_info "Generating base cluster configuration with talosctl gen config..."
# wild-cluster-config-generate
# Detect and register control plane nodes
print_header "Control Plane Node Registration"
@@ -366,7 +262,7 @@ if [ "${SKIP_HARDWARE}" = false ]; then
print_info " - Interface: $existing_interface"
print_info " - Disk: $(get_current_config "cluster.nodes.active.\"${TARGET_IP}\".disk")"
# Still generate machine config if it doesn't exist
# Generate machine config for this node if necessary.
NODE_SETUP_DIR="${WC_HOME}/setup/cluster-nodes"
CONFIG_FILE="${NODE_SETUP_DIR}/final/${TARGET_IP}.yaml"
if [ ! -f "$CONFIG_FILE" ]; then
@@ -388,7 +284,8 @@ if [ "${SKIP_HARDWARE}" = false ]; then
continue
fi
# First try to detect at target IP
# Register node in config.yaml.
# First try to detect at target IP.
print_info "Attempting detection at target IP $TARGET_IP..."
DETECTION_IP="$TARGET_IP"
NODE_INFO=""
@@ -446,7 +343,7 @@ if [ "${SKIP_HARDWARE}" = false ]; then
print_info "Selected disk: $SELECTED_DISK"
fi
# Update config.yaml with hardware info
# Update config.yaml with hardware info.
print_info "Updating configuration for $TARGET_IP..."
wild-config-set "cluster.nodes.active.\"${TARGET_IP}\".interface" "$INTERFACE"
wild-config-set "cluster.nodes.active.\"${TARGET_IP}\".disk" "$SELECTED_DISK"
@@ -454,7 +351,7 @@ if [ "${SKIP_HARDWARE}" = false ]; then
print_success "Node $TARGET_IP registered successfully"
# Generate machine config immediately
# Generate machine config.
print_info "Generating machine configuration for $TARGET_IP..."
if wild-cluster-node-machine-config-generate "$TARGET_IP"; then
print_success "Machine configuration generated for $TARGET_IP"
@@ -581,98 +478,10 @@ if [ "${SKIP_HARDWARE}" = false ]; then
fi
done
print_success "Phase 2 completed: Node hardware detection"
print_success "Completed Node hardware detection"
echo ""
else
print_info "Skipping Phase 2: Node Hardware Detection"
fi
# =============================================================================
# PHASE 3: Machine Config Summary and Verification
# =============================================================================
if [ "${SKIP_CONFIGS}" = false ]; then
print_header "Phase 3: Machine Config Summary and Verification"
# Get all registered nodes from cluster.nodes.active
REGISTERED_NODES=()
CONTROL_NODES=()
WORKER_NODES=()
if yq eval '.cluster.nodes.active // {}' "${WC_HOME}/config.yaml" | grep -q "interface"; then
ALL_NODE_IPS=$(yq eval '.cluster.nodes.active | keys | .[]' "${WC_HOME}/config.yaml" 2>/dev/null || echo "")
for NODE_IP in $ALL_NODE_IPS; do
# Remove quotes from yq output
NODE_IP=$(echo "$NODE_IP" | tr -d '"')
REGISTERED_NODES+=("$NODE_IP")
# Check if it's a control node
IS_CONTROL=$(yq eval ".cluster.nodes.active.\"${NODE_IP}\".control" "${WC_HOME}/config.yaml" 2>/dev/null)
if [ "$IS_CONTROL" = "true" ]; then
CONTROL_NODES+=("$NODE_IP")
else
WORKER_NODES+=("$NODE_IP")
fi
done
fi
if [ ${#REGISTERED_NODES[@]} -eq 0 ]; then
print_warning "No nodes have been registered yet."
print_info "Run Phase 2 (Hardware Detection) first to register nodes"
else
print_success "Machine configuration summary:"
echo ""
print_info "Registered nodes: ${#REGISTERED_NODES[@]}"
print_info " Control plane nodes: ${#CONTROL_NODES[@]}"
print_info " Worker nodes: ${#WORKER_NODES[@]}"
echo ""
# Check for any nodes that might need machine config generation
MISSING_CONFIGS=()
NODE_SETUP_DIR="${WC_HOME}/setup/cluster-nodes"
if [ -d "$NODE_SETUP_DIR/final" ]; then
for NODE_IP in "${REGISTERED_NODES[@]}"; do
CONFIG_FILE="$NODE_SETUP_DIR/final/${NODE_IP}.yaml"
if [ ! -f "$CONFIG_FILE" ]; then
MISSING_CONFIGS+=("$NODE_IP")
fi
done
else
MISSING_CONFIGS=("${REGISTERED_NODES[@]}")
fi
if [ ${#MISSING_CONFIGS[@]} -gt 0 ]; then
print_warning "Some nodes are missing machine configurations:"
for NODE_IP in "${MISSING_CONFIGS[@]}"; do
print_info "Generating missing config for $NODE_IP..."
wild-cluster-node-machine-config-generate "$NODE_IP"
done
else
print_success "All registered nodes have machine configurations"
fi
echo ""
print_info "Machine configuration files:"
for NODE_IP in "${REGISTERED_NODES[@]}"; do
CONFIG_FILE="$NODE_SETUP_DIR/final/${NODE_IP}.yaml"
if [ -f "$CONFIG_FILE" ]; then
NODE_TYPE=$(yq eval ".cluster.nodes.active.\"${NODE_IP}\".control" "${WC_HOME}/config.yaml" 2>/dev/null)
if [ "$NODE_TYPE" = "true" ]; then
print_success " ✓ $CONFIG_FILE (control plane)"
else
print_success " ✓ $CONFIG_FILE (worker)"
fi
fi
done
fi
print_success "Phase 3 completed: Machine config summary and verification"
echo ""
else
print_info "Skipping Phase 3: Machine Config Summary and Verification"
print_info "Skipping Node Hardware Detection"
fi
# =============================================================================
@@ -683,46 +492,3 @@ print_header "Wild-Cloud Cluster Setup Complete!"
print_success "Cluster infrastructure setup completed!"
echo ""
print_info "What was accomplished:"
if [ "${SKIP_INSTALLER}" = false ]; then
print_info "✅ Phase 1: Installer image generated"
else
print_info "⏸️ Phase 1: Installer image generation (skipped)"
fi
if [ "${SKIP_HARDWARE}" = false ]; then
print_info "✅ Phase 2: Node hardware detection completed"
else
print_info "⏸️ Phase 2: Node hardware detection (skipped)"
fi
if [ "${SKIP_CONFIGS}" = false ]; then
print_info "✅ Phase 3: Machine configs generated"
else
print_info "⏸️ Phase 3: Machine config generation (skipped)"
fi
echo ""
print_info "Configuration files:"
echo " - ${WC_HOME}/config.yaml"
echo " - ${WC_HOME}/secrets.yaml"
if [ -d "${WC_HOME}/setup/cluster-nodes/final" ] && [ "$(ls -A ${WC_HOME}/setup/cluster-nodes/final 2>/dev/null)" ]; then
echo ""
print_info "Machine configurations:"
for config_file in "${WC_HOME}/setup/cluster-nodes/final"/*.yaml; do
if [ -f "$config_file" ]; then
echo " - $config_file"
fi
done
fi
echo ""
print_info "Next steps:"
echo " 1. Apply machine configurations to your nodes"
echo " 2. Bootstrap your cluster and verify it's running"
echo " 3. Install cluster services:"
echo " wild-setup-services"
print_success "Ready for cluster services installation!"