#!/bin/bash set -e set -o pipefail # Usage function usage() { echo "Usage: wild-service-setup [options]" echo "" echo "Set up a single cluster service with complete lifecycle management." echo "" echo "Arguments:" echo " service Service name to set up" echo "" echo "Options:" echo " --fetch Fetch fresh templates from repository before setup" echo " --no-deploy Configure only, skip deployment to cluster" echo " -h, --help Show this help message" echo "" echo "Examples:" echo " wild-service-setup cert-manager # Configure and deploy (most common)" echo " wild-service-setup cert-manager --fetch # Fetch fresh templates, configure, and deploy" echo " wild-service-setup cert-manager --no-deploy # Configure only, skip deployment" echo " wild-service-setup cert-manager --fetch --no-deploy # Fetch and configure, but don't deploy" echo "" echo "Available services:" echo " metallb, longhorn, traefik, coredns, cert-manager," echo " externaldns, kubernetes-dashboard, nfs, docker-registry" } # Parse arguments FETCH=false NO_DEPLOY=false SERVICE="" while [[ $# -gt 0 ]]; do case $1 in -h|--help) usage exit 0 ;; --fetch) FETCH=true shift ;; --no-deploy) NO_DEPLOY=true shift ;; -*) echo "Unknown option $1" usage exit 1 ;; *) if [ -z "$SERVICE" ]; then SERVICE="$1" else echo "Unexpected argument: $1" usage exit 1 fi shift ;; esac done # Validate required service argument if [ -z "$SERVICE" ]; then echo "Error: Service name is required" usage exit 1 fi # Initialize Wild Cloud environment if [ -z "${WC_ROOT}" ]; then echo "WC_ROOT is not set." exit 1 else source "${WC_ROOT}/scripts/common.sh" init_wild_env fi print_header "Setting up service: $SERVICE" # ============================================================================= # FETCH FUNCTION # ============================================================================= fetch_service_templates() { local reason="$1" print_info "$reason" local source_dir="${WC_ROOT}/setup/cluster-services" local dest_dir="${WC_HOME}/setup/cluster-services" local service_source_dir="$source_dir/$SERVICE" local service_dest_dir="$dest_dir/$SERVICE" local template_source_dir="$service_source_dir/kustomize.template" local template_dest_dir="$service_dest_dir/kustomize.template" # Check if source service exists if [ ! -d "$service_source_dir" ]; then print_error "Service '$SERVICE' not found in repository: $service_source_dir" print_info "Available services:" ls -1 "$source_dir" | grep -v README | tr '\n' ' ' echo exit 1 fi # Create destination directories mkdir -p "$service_dest_dir" mkdir -p "$template_dest_dir" # Copy service files cp -f "$service_source_dir/README.md" "$service_dest_dir/" 2>/dev/null || true if [ -f "$service_source_dir/configure.sh" ]; then cp -f "$service_source_dir/configure.sh" "$service_dest_dir/" fi if [ -f "$service_source_dir/install.sh" ]; then cp -f "$service_source_dir/install.sh" "$service_dest_dir/" fi if [ -d "$template_source_dir" ]; then cp -r "$template_source_dir/"* "$template_dest_dir/" fi print_success "Fetched templates for $SERVICE" } # ============================================================================= # FETCH PHASE (Optional) # ============================================================================= if [ "$FETCH" = true ]; then fetch_service_templates "Fetching fresh templates for $SERVICE..." fi # ============================================================================= # CONFIGURE PHASE (Always runs) # ============================================================================= print_info "Configuring $SERVICE..." CLUSTER_SETUP_DIR="${WC_HOME}/setup/cluster-services" SERVICE_DIR="$CLUSTER_SETUP_DIR/$SERVICE" # Check if service directory exists, fetch if missing if [ ! -d "$SERVICE_DIR" ]; then fetch_service_templates "Service directory not found, fetching templates automatically..." fi # Run service configuration script CONFIG_SCRIPT="$SERVICE_DIR/configure.sh" if [ -f "$CONFIG_SCRIPT" ]; then print_info "Running configuration for $SERVICE..." source "$CONFIG_SCRIPT" else print_info "No configuration script found for $SERVICE, skipping configuration prompts" fi # Compile templates SOURCE_TEMPLATE_DIR="$SERVICE_DIR/kustomize.template" DEST_TEMPLATE_DIR="$SERVICE_DIR/kustomize" if [ -d "$SOURCE_TEMPLATE_DIR" ]; then print_info "Compiling templates for $SERVICE..." wild-compile-template-dir --clean "$SOURCE_TEMPLATE_DIR" "$DEST_TEMPLATE_DIR" print_success "Templates compiled for $SERVICE" else print_warning "No templates found for $SERVICE at $SOURCE_TEMPLATE_DIR" fi # ============================================================================= # DEPLOY PHASE (Optional) # ============================================================================= if [ "$NO_DEPLOY" = true ]; then print_info "Skipping deployment for $SERVICE (--no-deploy specified)" print_success "Configuration complete for $SERVICE" print_info "To deploy later, run: wild-service-setup $SERVICE" else print_info "Deploying $SERVICE to cluster..." # Run service installation script INSTALL_SCRIPT="$SERVICE_DIR/install.sh" if [ -f "$INSTALL_SCRIPT" ]; then if "$INSTALL_SCRIPT"; then print_success "$SERVICE deployed successfully" else print_error "$SERVICE deployment failed" exit 1 fi else print_error "No installation script found for $SERVICE at $INSTALL_SCRIPT" exit 1 fi fi print_success "Service setup complete: $SERVICE"