#!/bin/bash set -e # Default values SERVICE_NAME="" DRY_RUN=false # Source environment variables from load-env.sh SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_DIR="$(dirname "$SCRIPT_DIR")" if [ -f "$REPO_DIR/load-env.sh" ]; then source "$REPO_DIR/load-env.sh" fi function show_help { echo "Usage: $0 SERVICE_NAME [options]" echo "" echo "Arguments:" echo " SERVICE_NAME Name of the service to deploy (directory name in services/)" echo "" echo "Optional arguments:" echo " --dry-run Preview the processed configuration without applying" echo " --help Show this help message" echo "" echo "Examples:" echo " $0 example-app" echo " $0 blog --dry-run" exit 1 } # Legacy mode check for type-based commands if [[ "$1" == "--type" ]]; then echo "Warning: Using legacy mode (generate and deploy in one step)" echo "Consider using generate-service followed by deploy-service instead." echo "Continuing with legacy mode..." echo "" # Capture all arguments ALL_ARGS="$@" # Extract service name from arguments while [[ $# -gt 0 ]]; do key="$1" case $key in --name) SERVICE_NAME_LEGACY="$2" break ;; *) shift ;; esac done # Generate the service configuration first TMP_DIR=$(mktemp -d) TMP_FILE="$TMP_DIR/service.yaml" $SCRIPT_DIR/generate-service $ALL_ARGS --output "$TMP_DIR" # Now deploy it using the service name if [[ -n "$SERVICE_NAME_LEGACY" ]]; then exec $0 "$SERVICE_NAME_LEGACY" else echo "Error: Legacy mode requires --name parameter" exit 1 fi exit $? fi # Parse command-line arguments while [[ $# -gt 0 ]]; do key="$1" case $key in --dry-run) DRY_RUN=true shift ;; --help) show_help ;; -*) echo "Unknown option: $1" show_help ;; *) # First non-option argument is the service name SERVICE_NAME="$1" shift ;; esac done # Validate service name if [[ -z "$SERVICE_NAME" ]]; then echo "Error: SERVICE_NAME must be provided" show_help fi # Construct the service file path SERVICE_FILE="$REPO_DIR/services/$SERVICE_NAME/service.yaml" if [[ ! -f "$SERVICE_FILE" ]]; then echo "Error: Service file not found for $SERVICE_NAME at $SERVICE_FILE" exit 1 fi # Create temporary file for the processed manifest TEMP_FILE=$(mktemp) # Ensure DOMAIN is exported for template substitution export DOMAIN="$DOMAIN" # Process the service file with variable substitution echo "Processing service file: $SERVICE_FILE" cat "$SERVICE_FILE" | envsubst > "$TEMP_FILE" # Handle dry run mode if [[ "$DRY_RUN" == "true" ]]; then cat "$TEMP_FILE" rm "$TEMP_FILE" exit 0 fi # Extract namespace from the processed file (for creating it if needed) NAMESPACE=$(grep -o "namespace: [a-zA-Z0-9_-]\+" "$TEMP_FILE" | head -1 | cut -d' ' -f2) if [[ -n "$NAMESPACE" ]]; then # Create the namespace if it doesn't exist (using kubectl create which is idempotent with --dry-run=client) echo "Creating namespace $NAMESPACE if it doesn't exist..." kubectl create namespace "$NAMESPACE" --dry-run=client | kubectl create -f - 2>/dev/null || true # Copy certificates to the namespace copy-secret cert-manager:wildcard-internal-sovereign-cloud-tls $NAMESPACE copy-secret cert-manager:wildcard-sovereign-cloud-tls $NAMESPACE fi # Apply the service echo "Applying service configuration..." kubectl apply -f "$TEMP_FILE" rm "$TEMP_FILE" echo "✅ Service deployed successfully!"