#!/bin/bash # Deploy script for Calcolatore Prezzi Software # This script helps deploy the application to Kubernetes using Helm set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Configuration CHART_PATH="./helm-chart" RELEASE_NAME="calcolatore-prezzi" NAMESPACE="default" VALUES_FILE="" IMAGE_TAG="latest" # Functions print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } usage() { cat << EOF Usage: $0 [OPTIONS] Deploy Calcolatore Prezzi Software to Kubernetes using Helm OPTIONS: -h, --help Show this help message -n, --namespace NAME Kubernetes namespace (default: default) -r, --release NAME Helm release name (default: calcolatore-prezzi) -f, --values FILE Values file for Helm -t, --tag TAG Docker image tag (default: latest) -u, --upgrade Upgrade existing release -d, --dry-run Perform a dry run --uninstall Uninstall the release --build Build Docker image before deploy EXAMPLES: # Basic deployment $0 # Deploy with custom values $0 -f values-production.yaml -n production # Upgrade existing deployment $0 -u -t v1.2.3 -n production # Dry run $0 -d -f values-production.yaml # Build and deploy $0 --build -t v1.0.0 # Uninstall $0 --uninstall -n production EOF } check_requirements() { print_info "Checking requirements..." if ! command -v helm &> /dev/null; then print_error "Helm is not installed. Please install Helm 3.0+" exit 1 fi if ! command -v kubectl &> /dev/null; then print_error "kubectl is not installed. Please install kubectl" exit 1 fi print_info "All requirements satisfied" } update_dependencies() { print_info "Updating Helm dependencies..." cd "$CHART_PATH" helm dependency update cd - > /dev/null print_info "Dependencies updated" } build_image() { print_info "Building Docker image..." docker build -t "calcolatore-prezzi:${IMAGE_TAG}" . print_info "Docker image built successfully: calcolatore-prezzi:${IMAGE_TAG}" } deploy() { local action="install" local dry_run="" if [ "$UPGRADE" = true ]; then action="upgrade" fi if [ "$DRY_RUN" = true ]; then dry_run="--dry-run --debug" fi print_info "Preparing to ${action} release '${RELEASE_NAME}' in namespace '${NAMESPACE}'..." # Build helm command local helm_cmd="helm ${action} ${RELEASE_NAME} ${CHART_PATH}" helm_cmd="${helm_cmd} --namespace ${NAMESPACE}" helm_cmd="${helm_cmd} --create-namespace" if [ -n "$VALUES_FILE" ]; then helm_cmd="${helm_cmd} -f ${VALUES_FILE}" fi helm_cmd="${helm_cmd} --set image.tag=${IMAGE_TAG}" # Inject HTML files helm_cmd="${helm_cmd} --set-file configMaps.html-content.data.index\\.html=./index.html" helm_cmd="${helm_cmd} --set-file configMaps.html-content.data.project-mode\\.html=./project-mode.html" helm_cmd="${helm_cmd} --set-file configMaps.html-content.data.shop-mode\\.html=./shop-mode.html" if [ -n "$dry_run" ]; then helm_cmd="${helm_cmd} ${dry_run}" fi print_info "Executing: ${helm_cmd}" eval "$helm_cmd" if [ "$DRY_RUN" != true ]; then print_info "Deployment successful!" print_info "Checking deployment status..." kubectl rollout status deployment/${RELEASE_NAME} -n ${NAMESPACE} --timeout=5m || true fi } uninstall() { print_warn "Uninstalling release '${RELEASE_NAME}' from namespace '${NAMESPACE}'..." helm uninstall ${RELEASE_NAME} -n ${NAMESPACE} print_info "Release uninstalled successfully" } # Parse arguments UPGRADE=false DRY_RUN=false UNINSTALL=false BUILD=false while [[ $# -gt 0 ]]; do case $1 in -h|--help) usage exit 0 ;; -n|--namespace) NAMESPACE="$2" shift 2 ;; -r|--release) RELEASE_NAME="$2" shift 2 ;; -f|--values) VALUES_FILE="$2" shift 2 ;; -t|--tag) IMAGE_TAG="$2" shift 2 ;; -u|--upgrade) UPGRADE=true shift ;; -d|--dry-run) DRY_RUN=true shift ;; --uninstall) UNINSTALL=true shift ;; --build) BUILD=true shift ;; *) print_error "Unknown option: $1" usage exit 1 ;; esac done # Main execution print_info "===== Calcolatore Prezzi Software - Deploy Script =====" check_requirements if [ "$UNINSTALL" = true ]; then uninstall exit 0 fi if [ "$BUILD" = true ]; then build_image fi update_dependencies deploy print_info "===== Deployment Complete =====" if [ "$DRY_RUN" != true ]; then print_info "" print_info "To check the status:" print_info " kubectl get pods -n ${NAMESPACE} -l app.kubernetes.io/name=${RELEASE_NAME}" print_info "" print_info "To view logs:" print_info " kubectl logs -n ${NAMESPACE} -l app.kubernetes.io/name=${RELEASE_NAME} -f" print_info "" print_info "To access the application:" print_info " kubectl port-forward -n ${NAMESPACE} svc/${RELEASE_NAME} 8080:80" print_info " Then visit: http://localhost:8080" fi