#!/bin/bash # DSS Services Manager # Start, stop, and manage all DSS development services # # Usage: scripts/dss-services.sh [--service NAME] # # Actions: # start - Start all services (or specific service) # stop - Stop all services (or specific service) # status - Show status of all services # restart - Restart all services (or specific service) # logs - Show service logs set -e DSS_ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$DSS_ROOT" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # Service configuration (DSS Ports: API=6220, Admin=6221, MCP=6222, Storybook=6226) declare -A SERVICES=( ["api"]="6220" ["admin-ui"]="6221" ["storybook"]="6226" ) declare -A SERVICE_CMDS=( ["api"]="uvicorn apps.api.server:app --host 0.0.0.0 --port 6220 --reload" ["admin-ui"]="npm run dev" ["storybook"]="npm run storybook" ) declare -A SERVICE_DIRS=( ["api"]="$DSS_ROOT" ["admin-ui"]="$DSS_ROOT/admin-ui" ["storybook"]="$DSS_ROOT/admin-ui" ) declare -A SERVICE_LOGS=( ["api"]="/tmp/dss-api.log" ["admin-ui"]="/tmp/dss-admin-ui.log" ["storybook"]="/tmp/dss-storybook.log" ) # Logging log_info() { echo -e "${CYAN}[INFO]${NC} $1"; } log_ok() { echo -e "${GREEN}[OK]${NC} $1"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; } # Check if port is in use is_port_active() { local port=$1 lsof -i ":$port" &>/dev/null } # Get PID using port get_port_pid() { local port=$1 lsof -ti ":$port" 2>/dev/null | head -1 } # Start a single service start_service() { local service=$1 local port=${SERVICES[$service]} local cmd=${SERVICE_CMDS[$service]} local dir=${SERVICE_DIRS[$service]} local log=${SERVICE_LOGS[$service]} if is_port_active "$port"; then log_warn "$service already running on port $port" return 0 fi log_info "Starting $service on port $port..." cd "$dir" if [ "$service" = "api" ]; then # Activate venv for API server source "$DSS_ROOT/.venv/bin/activate" nohup $cmd > "$log" 2>&1 & else nohup $cmd > "$log" 2>&1 & fi local pid=$! cd "$DSS_ROOT" # Wait for service to start local max_wait=30 local waited=0 while [ $waited -lt $max_wait ]; do if is_port_active "$port"; then log_ok "$service started (PID: $(get_port_pid $port), port: $port)" return 0 fi sleep 1 ((waited+=1)) done log_error "$service failed to start (check $log)" return 1 } # Stop a single service stop_service() { local service=$1 local port=${SERVICES[$service]} if ! is_port_active "$port"; then log_info "$service not running" return 0 fi local pid=$(get_port_pid "$port") log_info "Stopping $service (PID: $pid)..." kill "$pid" 2>/dev/null || true sleep 1 # Force kill if still running if is_port_active "$port"; then kill -9 "$(get_port_pid $port)" 2>/dev/null || true fi log_ok "$service stopped" } # Show status of all services show_status() { echo "" echo "╔══════════════════════════════════════════════════════════════╗" echo "║ DSS SERVICES STATUS ║" echo "╚══════════════════════════════════════════════════════════════╝" echo "" printf " %-12s %-8s %-8s %-30s\n" "SERVICE" "PORT" "STATUS" "URL" echo " ──────────────────────────────────────────────────────────────" for service in api admin-ui storybook; do local port=${SERVICES[$service]} local status="STOPPED" local color=$RED local url="-" if is_port_active "$port"; then status="RUNNING" color=$GREEN url="http://localhost:$port" fi printf " %-12s %-8s ${color}%-8s${NC} %-30s\n" "$service" "$port" "$status" "$url" done echo "" echo " Logs:" for service in api admin-ui storybook; do echo " $service: ${SERVICE_LOGS[$service]}" done echo "" } # Show logs for a service show_logs() { local service=$1 local log=${SERVICE_LOGS[$service]} if [ -z "$log" ]; then log_error "Unknown service: $service" return 1 fi if [ ! -f "$log" ]; then log_warn "No log file found: $log" return 0 fi echo "=== $service logs ($log) ===" tail -50 "$log" } # Main command handling ACTION=${1:-status} SPECIFIC_SERVICE="" # Parse arguments shift || true while [[ $# -gt 0 ]]; do case $1 in --service|-s) SPECIFIC_SERVICE="$2" shift 2 ;; api|admin-ui|storybook) SPECIFIC_SERVICE="$1" shift ;; *) shift ;; esac done case $ACTION in start) echo "" echo "╔══════════════════════════════════════════════════════════════╗" echo "║ STARTING DSS SERVICES ║" echo "╚══════════════════════════════════════════════════════════════╝" echo "" if [ -n "$SPECIFIC_SERVICE" ]; then start_service "$SPECIFIC_SERVICE" else for service in api admin-ui storybook; do start_service "$service" done fi echo "" show_status ;; stop) echo "" echo "╔══════════════════════════════════════════════════════════════╗" echo "║ STOPPING DSS SERVICES ║" echo "╚══════════════════════════════════════════════════════════════╝" echo "" if [ -n "$SPECIFIC_SERVICE" ]; then stop_service "$SPECIFIC_SERVICE" else for service in api admin-ui storybook; do stop_service "$service" done fi echo "" show_status ;; restart) echo "" echo "╔══════════════════════════════════════════════════════════════╗" echo "║ RESTARTING DSS SERVICES ║" echo "╚══════════════════════════════════════════════════════════════╝" echo "" if [ -n "$SPECIFIC_SERVICE" ]; then stop_service "$SPECIFIC_SERVICE" sleep 1 start_service "$SPECIFIC_SERVICE" else for service in api admin-ui storybook; do stop_service "$service" done sleep 2 for service in api admin-ui storybook; do start_service "$service" done fi echo "" show_status ;; status) show_status ;; logs) if [ -n "$SPECIFIC_SERVICE" ]; then show_logs "$SPECIFIC_SERVICE" else for service in api admin-ui storybook; do show_logs "$service" echo "" done fi ;; *) echo "DSS Services Manager" echo "" echo "Usage: $0 [--service NAME]" echo "" echo "Actions:" echo " start Start all services (or specific service)" echo " stop Stop all services (or specific service)" echo " status Show status of all services" echo " restart Restart all services (or specific service)" echo " logs Show service logs" echo "" echo "Services: api, admin-ui, storybook" echo "" echo "Examples:" echo " $0 start # Start all services" echo " $0 start --service api # Start only API server" echo " $0 stop storybook # Stop Storybook" echo " $0 logs admin-ui # Show admin-ui logs" ;; esac