Files
zeekay 1943f1c093 Hanzo Commerce
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans,
usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0.

Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
2026-09-23 21:52:16 -07:00

156 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
# Hanzo Commerce - Blue-Green Deployment Switch Script
#
# Usage:
# ./deploy/scripts/switch-active.sh [blue|green]
# ./deploy/scripts/switch-active.sh status
#
# This script updates the nginx upstream to point to the specified deployment.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEPLOY_DIR="$(dirname "$SCRIPT_DIR")"
NGINX_CONF="$DEPLOY_DIR/nginx/conf.d/active.conf"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Get current active deployment
get_active() {
if [ -f "$NGINX_CONF" ]; then
grep -oP 'server commerce-\K(blue|green)' "$NGINX_CONF" 2>/dev/null | head -1 || echo "unknown"
else
echo "blue" # Default
fi
}
# Check deployment health
check_health() {
local color=$1
local container="commerce-${color}"
if docker inspect "$container" &>/dev/null; then
local health=$(docker inspect --format='{{.State.Health.Status}}' "$container" 2>/dev/null || echo "unknown")
echo "$health"
else
echo "not_running"
fi
}
# Display status
show_status() {
local active=$(get_active)
local blue_health=$(check_health "blue")
local green_health=$(check_health "green")
echo ""
echo "============================================"
echo " Hanzo Commerce - Deployment Status"
echo "============================================"
echo ""
echo " Active Deployment: ${active^^}"
echo ""
echo " Blue Deployment:"
if [ "$blue_health" == "healthy" ]; then
echo -e " Status: ${GREEN}HEALTHY${NC}"
elif [ "$blue_health" == "not_running" ]; then
echo -e " Status: ${YELLOW}NOT RUNNING${NC}"
else
echo -e " Status: ${RED}${blue_health^^}${NC}"
fi
echo ""
echo " Green Deployment:"
if [ "$green_health" == "healthy" ]; then
echo -e " Status: ${GREEN}HEALTHY${NC}"
elif [ "$green_health" == "not_running" ]; then
echo -e " Status: ${YELLOW}NOT RUNNING${NC}"
else
echo -e " Status: ${RED}${green_health^^}${NC}"
fi
echo ""
echo "============================================"
}
# Switch active deployment
switch_to() {
local target=$1
local current=$(get_active)
log_info "Switching from ${current} to ${target}..."
# Check target health
local target_health=$(check_health "$target")
if [ "$target_health" != "healthy" ]; then
log_error "Target deployment '${target}' is not healthy (status: ${target_health})"
log_error "Cannot switch to unhealthy deployment. Please check the container."
exit 1
fi
# Create/update nginx upstream config
mkdir -p "$(dirname "$NGINX_CONF")"
cat > "$NGINX_CONF" << EOF
# Active deployment configuration
# Generated by switch-active.sh at $(date -Iseconds)
# Active: ${target}
upstream commerce_active {
server commerce-${target}:8001 max_fails=3 fail_timeout=30s;
keepalive 32;
}
EOF
log_info "Updated nginx configuration"
# Reload nginx
if docker exec commerce-nginx nginx -t &>/dev/null; then
docker exec commerce-nginx nginx -s reload
log_success "Nginx reloaded successfully"
else
log_error "Nginx configuration test failed!"
exit 1
fi
# Verify switch
sleep 2
local new_active=$(get_active)
if [ "$new_active" == "$target" ]; then
log_success "Successfully switched to ${target^^} deployment"
else
log_error "Switch verification failed"
exit 1
fi
}
# Main
main() {
case "${1:-status}" in
blue|green)
switch_to "$1"
;;
status)
show_status
;;
*)
echo "Usage: $0 [blue|green|status]"
echo ""
echo "Commands:"
echo " blue - Switch traffic to blue deployment"
echo " green - Switch traffic to green deployment"
echo " status - Show current deployment status"
exit 1
;;
esac
}
main "$@"