#!/bin/bash # ============================================================================= # ERP-SUITE - Script de Despliegue Multi-Vertical # ============================================================================= # Uso: # ./scripts/deploy.sh [vertical|all] [staging|production] # ./scripts/deploy.sh erp-core production # ./scripts/deploy.sh construccion staging # ./scripts/deploy.sh all production # ============================================================================= set -e # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${GREEN}[INFO]${NC} $1"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; } log_step() { echo -e "${BLUE}[STEP]${NC} $1"; } # ============================================================================= # CONFIGURACIÓN # ============================================================================= DOCKER_REGISTRY="${DOCKER_REGISTRY:-72.60.226.4:5000}" DEPLOY_SERVER="${DEPLOY_SERVER:-72.60.226.4}" DEPLOY_USER="${DEPLOY_USER:-deploy}" DEPLOY_PATH="/opt/apps/erp-suite" VERSION="${VERSION:-$(date +%Y%m%d%H%M%S)}" # Verticales disponibles declare -A VERTICALS=( ["erp-core"]="apps/erp-core:3010:3011" ["construccion"]="apps/verticales/construccion:3020:3021" ["vidrio-templado"]="apps/verticales/vidrio-templado:3030:3031" ["mecanicas-diesel"]="apps/verticales/mecanicas-diesel:3040:3041" ["retail"]="apps/verticales/retail:3050:3051" ["clinicas"]="apps/verticales/clinicas:3060:3061" ["pos-micro"]="apps/products/pos-micro:3070:3071" ) # Verticales activas (con código) ACTIVE_VERTICALS=("erp-core" "construccion" "mecanicas-diesel") # ============================================================================= # FUNCIONES # ============================================================================= show_usage() { echo "Uso: $0 [vertical|all] [staging|production]" echo "" echo "Verticales disponibles:" for v in "${!VERTICALS[@]}"; do if [[ " ${ACTIVE_VERTICALS[@]} " =~ " ${v} " ]]; then echo " - ${v} (activo)" else echo " - ${v} (reservado)" fi done echo " - all (todas las activas)" echo "" echo "Ejemplos:" echo " $0 erp-core production" echo " $0 construccion staging" echo " $0 all production" } get_vertical_config() { local vertical=$1 echo "${VERTICALS[$vertical]}" } build_vertical() { local vertical=$1 local config=$(get_vertical_config "$vertical") local path=$(echo "$config" | cut -d: -f1) log_step "Building ${vertical}..." # Build backend if [ -d "${path}/backend" ]; then log_info "Building backend..." docker build -t ${DOCKER_REGISTRY}/erp-${vertical}-backend:${VERSION} \ -t ${DOCKER_REGISTRY}/erp-${vertical}-backend:latest \ ${path}/backend/ fi # Build frontend local frontend_path="${path}/frontend" if [ -d "${path}/frontend/web" ]; then frontend_path="${path}/frontend/web" fi if [ -d "${frontend_path}" ] && [ -f "${frontend_path}/Dockerfile" ]; then log_info "Building frontend..." docker build -t ${DOCKER_REGISTRY}/erp-${vertical}-frontend:${VERSION} \ -t ${DOCKER_REGISTRY}/erp-${vertical}-frontend:latest \ ${frontend_path}/ fi } push_vertical() { local vertical=$1 log_step "Pushing ${vertical} images..." docker push ${DOCKER_REGISTRY}/erp-${vertical}-backend:${VERSION} 2>/dev/null || true docker push ${DOCKER_REGISTRY}/erp-${vertical}-backend:latest 2>/dev/null || true docker push ${DOCKER_REGISTRY}/erp-${vertical}-frontend:${VERSION} 2>/dev/null || true docker push ${DOCKER_REGISTRY}/erp-${vertical}-frontend:latest 2>/dev/null || true } deploy_vertical() { local vertical=$1 local env=$2 log_step "Deploying ${vertical} to ${env}..." ssh -o StrictHostKeyChecking=no ${DEPLOY_USER}@${DEPLOY_SERVER} << EOF set -e cd ${DEPLOY_PATH}/${vertical} echo "📦 Pulling images..." docker-compose -f docker-compose.prod.yml pull || true echo "🔄 Stopping containers..." docker-compose -f docker-compose.prod.yml down --remove-orphans || true echo "🚀 Starting containers..." docker-compose -f docker-compose.prod.yml up -d echo "🧹 Cleanup..." docker system prune -f echo "✅ ${vertical} deployed!" EOF } health_check() { local vertical=$1 local config=$(get_vertical_config "$vertical") local backend_port=$(echo "$config" | cut -d: -f3) log_info "Health check for ${vertical} (port ${backend_port})..." for i in {1..5}; do if curl -sf "http://${DEPLOY_SERVER}:${backend_port}/health" > /dev/null; then log_info "✅ ${vertical} is healthy" return 0 fi log_warn "Attempt ${i}/5 failed, waiting..." sleep 10 done log_error "${vertical} health check failed!" } # ============================================================================= # MAIN # ============================================================================= main() { local vertical=${1:-help} local env=${2:-staging} if [ "$vertical" == "help" ] || [ "$vertical" == "-h" ]; then show_usage exit 0 fi echo "=============================================================================" echo "ERP-SUITE - Deployment Script" echo "=============================================================================" echo "Vertical: ${vertical}" echo "Environment: ${env}" echo "Version: ${VERSION}" echo "=============================================================================" # Determinar verticales a desplegar local verticals_to_deploy=() if [ "$vertical" == "all" ]; then verticals_to_deploy=("${ACTIVE_VERTICALS[@]}") else if [ -z "${VERTICALS[$vertical]}" ]; then log_error "Vertical '${vertical}' no existe" fi verticals_to_deploy=("$vertical") fi # Desplegar erp-core primero si está en la lista if [[ " ${verticals_to_deploy[@]} " =~ " erp-core " ]]; then build_vertical "erp-core" push_vertical "erp-core" deploy_vertical "erp-core" "$env" health_check "erp-core" # Remover erp-core de la lista verticals_to_deploy=(${verticals_to_deploy[@]/erp-core}) fi # Desplegar resto de verticales for v in "${verticals_to_deploy[@]}"; do build_vertical "$v" push_vertical "$v" deploy_vertical "$v" "$env" health_check "$v" done echo "" echo "=============================================================================" echo -e "${GREEN}DESPLIEGUE COMPLETADO${NC}" echo "=============================================================================" } main "$@"