-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy-dev.sh
More file actions
executable file
·746 lines (657 loc) · 23.4 KB
/
deploy-dev.sh
File metadata and controls
executable file
·746 lines (657 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#!/bin/bash
#
# ITSM Development Environment Deployment Script v3.0
#
# A production-quality dev script following open-source best practices:
# - Shared library for DRY code
# - Clean build output (suppress Docker noise by default)
# - Timed operations
# - Trap-based cleanup on SIGINT/SIGTERM
# - Idempotent (safe to re-run)
# - First-time init mode
# - Self-diagnostic (doctor) command
#
# Usage:
# ./scripts/deploy-dev.sh [command] [options]
#
# Commands:
# up Start all services (default)
# down Stop all services
# restart Restart all services
# status Show service status
# logs Tail service logs
# health Run health checks
# init First-time setup (install tools + start)
# doctor Diagnose common issues
# reset Stop and remove all containers + volumes
# help Show help
#
# Options:
# --local Force local development mode
# --docker Force Docker Compose mode
# --skip-deps Skip dependency installation
# --no-build Skip Docker image rebuild
# --verbose Show full Docker build output
# -h, --help Show help
#
set -euo pipefail
# ============================================================
# Bootstrap: load shared library
# ============================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/common.sh
source "${SCRIPT_DIR}/lib/common.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
# ============================================================
# Project-specific constants
# ============================================================
BACKEND_DIR="$PROJECT_ROOT/itsm-backend"
FRONTEND_DIR="$PROJECT_ROOT/itsm-frontend"
COMPOSE_DEV="$PROJECT_ROOT/docker-compose.dev.yml"
COMPOSE_OOB="$PROJECT_ROOT/docker-compose.yml"
BACKEND_URL="http://localhost:8090"
FRONTEND_URL="http://localhost:3000"
HEALTH_PATH="/api/v1/health"
DEFAULT_ADMIN_USER="admin"
DEFAULT_ADMIN_PASS="admin123"
LOG_DIR="$PROJECT_ROOT/logs"
PID_DIR="$PROJECT_ROOT/.pids"
# ============================================================
# Parse arguments
# ============================================================
COMMAND="up"
FORCE_MODE=""
SKIP_DEPS=false
NO_BUILD=false
VERBOSE=false
while [[ $# -gt 0 ]]; do
case "$1" in
up|down|restart|status|logs|health|init|doctor|reset|help)
COMMAND="$1"; shift ;;
--local) FORCE_MODE="local"; shift ;;
--docker) FORCE_MODE="docker"; shift ;;
--skip-deps) SKIP_DEPS=true; shift ;;
--no-build) NO_BUILD=true; shift ;;
--verbose) VERBOSE=true; shift ;;
-h|--help) COMMAND="help"; shift ;;
*)
log_error "Unknown option: $1"
COMMAND="help"
shift
;;
esac
done
# Export for dc() helper
export VERBOSE
# ============================================================
# Mode detection
# ============================================================
detect_mode() {
if [[ -n "$FORCE_MODE" ]]; then
echo "$FORCE_MODE"
return
fi
if [[ -f "$COMPOSE_DEV" ]] && command -v docker &>/dev/null && docker info &>/dev/null 2>&1; then
echo "docker"
else
echo "local"
fi
}
# ============================================================
# Cleanup on exit
# ============================================================
mkdir -p "$LOG_DIR" "$PID_DIR"
cleanup() {
# Remove stale PID files if we wrote them
:
}
on_exit "cleanup"
# ============================================================
# Docker Compose mode
# ============================================================
docker_up() {
local build_flag=""
$NO_BUILD || build_flag="--build"
log_step "Starting development environment (Docker Compose)"
# Ensure .env exists
if [[ ! -f "$PROJECT_ROOT/.env" ]] && [[ -f "$PROJECT_ROOT/.env.example" ]]; then
cp "$PROJECT_ROOT/.env.example" "$PROJECT_ROOT/.env"
log_info "Created .env from .env.example"
fi
# Phase 1: Infrastructure
log_step "[1/3] Starting PostgreSQL + Redis"
local start; start=$(timer_start)
dc -f "$COMPOSE_DEV" --profile dev up -d postgres redis
if wait_for_container_healthy "itsm-postgres-dev" 30; then
timer_end "$start" "Infrastructure"
else
log_error "PostgreSQL not healthy after 30s"
return 1
fi
# Phase 2: Backend
log_step "[2/3] Building and starting backend"
start=$(timer_start)
dc -f "$COMPOSE_DEV" --profile dev up -d $build_flag itsm-backend
log_info "Waiting for backend health check..."
if wait_for_http "${BACKEND_URL}${HEALTH_PATH}" "backend" 120; then
timer_end "$start" "Backend build + start"
log_success "Backend is healthy"
else
log_error "Backend failed to start. Run with --verbose for full logs."
log_info "Quick check: docker compose -f docker-compose.dev.yml --profile dev logs --tail=30 itsm-backend"
return 1
fi
# Phase 3: Frontend
log_step "[3/3] Building and starting frontend"
start=$(timer_start)
dc -f "$COMPOSE_DEV" --profile dev up -d $build_flag itsm-frontend
log_info "Waiting for frontend..."
if wait_for_http "$FRONTEND_URL" "frontend" 90; then
timer_end "$start" "Frontend build + start"
log_success "Frontend is healthy"
else
log_warn "Frontend may need more time. Check: docker compose -f docker-compose.dev.yml --profile dev logs itsm-frontend"
fi
print_summary
}
docker_down() {
log_step "Stopping development environment"
dc -f "$COMPOSE_DEV" --profile dev down
log_success "All services stopped"
}
docker_reset() {
log_step "Resetting development environment (removes volumes)"
log_warn "This will DELETE all data in PostgreSQL and Redis"
read -rp "Continue? [y/N] " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
dc -f "$COMPOSE_DEV" --profile dev down -v --remove-orphans
log_success "Environment reset complete"
else
log_info "Reset cancelled"
fi
}
docker_logs() {
local service="${1:-}"
if [[ -n "$service" ]]; then
dc -f "$COMPOSE_DEV" --profile dev logs -f "$service"
else
dc -f "$COMPOSE_DEV" --profile dev logs -f
fi
}
# ============================================================
# Local development mode
# ============================================================
local_up() {
log_step "Starting development environment (Local)"
local total_start; total_start=$(timer_start)
check_prerequisites
start_infrastructure
start_backend_local
start_frontend_local
run_health_checks
timer_end "$total_start" "Total startup"
print_summary
}
check_prerequisites() {
log_info "Checking prerequisites..."
local missing=()
command -v go &>/dev/null || missing+=("go")
command -v node &>/dev/null || missing+=("node")
command -v pnpm &>/dev/null || missing+=("pnpm")
command -v docker &>/dev/null || missing+=("docker")
if [[ ${#missing[@]} -gt 0 ]]; then
log_error "Missing prerequisites: ${missing[*]}"
log_info "Install missing tools or use --docker mode"
exit 1
fi
# Version checks
local go_ver node_ver
go_ver=$(go version 2>/dev/null | grep -oE 'go[0-9]+\.[0-9]+' | tr -d 'go' || echo "?")
node_ver=$(node --version 2>/dev/null | tr -d 'v' || echo "?")
log_success "Go ${go_ver}, Node ${node_ver}, pnpm $(pnpm --version 2>/dev/null || echo '?'), Docker $(docker --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo '?')"
}
start_infrastructure() {
log_step "Starting infrastructure (PostgreSQL + Redis)"
# PostgreSQL
if container_running "itsm-postgres-dev"; then
log_info "PostgreSQL already running"
elif port_in_use 5432; then
log_warn "Port 5432 in use (external PostgreSQL?)"
else
docker run -d --name itsm-postgres-dev \
-e POSTGRES_DB=itsm \
-e POSTGRES_USER=itsm \
-e POSTGRES_PASSWORD=itsm_password_2026 \
-p 5432:5432 \
--restart unless-stopped \
postgres:17-alpine
if wait_for_container_healthy "itsm-postgres-dev" 20; then
log_success "PostgreSQL ready on port 5432"
else
log_error "PostgreSQL failed to start"
return 1
fi
fi
# Redis
if container_running "itsm-redis-dev"; then
log_info "Redis already running"
elif port_in_use 6379; then
log_warn "Port 6379 in use (external Redis?)"
else
docker run -d --name itsm-redis-dev \
-p 6379:6379 \
--restart unless-stopped \
redis:7-alpine
if wait_for_container_healthy "itsm-redis-dev" 10; then
log_success "Redis ready on port 6379"
else
log_error "Redis failed to start"
return 1
fi
fi
}
start_backend_local() {
log_step "Starting backend service"
if wait_for_http "${BACKEND_URL}${HEALTH_PATH}" "backend" 2; then
log_info "Backend already running on port 8090"
return 0
fi
if port_in_use 8090; then
log_error "Port 8090 in use but health check failed. Kill stale process first."
return 1
fi
cd "$BACKEND_DIR"
# Environment defaults (only set if not already defined)
export DB_HOST="${DB_HOST:-localhost}"
export DB_PORT="${DB_PORT:-5432}"
export DB_USER="${DB_USER:-itsm_user}"
export DB_PASSWORD="${DB_PASSWORD:-dev123}"
export DB_NAME="${DB_NAME:-itsm}"
export DB_SSLMODE="${DB_SSLMODE:-disable}"
export REDIS_HOST="${REDIS_HOST:-localhost}"
export REDIS_PORT="${REDIS_PORT:-6379}"
export REDIS_PASSWORD="${REDIS_PASSWORD:-}"
export REDIS_DB="${REDIS_DB:-0}"
export JWT_SECRET="${JWT_SECRET:-dev-secret-key-placeholder-change-in-production-min-32-chars}"
export LOG_LEVEL="${LOG_LEVEL:-debug}"
export GIN_MODE="${GIN_MODE:-debug}"
export SERVER_ENV="${SERVER_ENV:-development}"
local start; start=$(timer_start)
log_info "Starting Go backend..."
nohup go run main.go > "$LOG_DIR/backend.log" 2>&1 &
local pid=$!
echo "$pid" > "$PID_DIR/backend.pid"
log_info "Backend PID: $pid"
if wait_for_http "${BACKEND_URL}${HEALTH_PATH}" "backend" 60; then
timer_end "$start" "Backend startup"
log_success "Backend is healthy"
else
log_error "Backend failed to start. Tail: $LOG_DIR/backend.log"
tail -20 "$LOG_DIR/backend.log" 2>/dev/null || true
return 1
fi
}
start_frontend_local() {
log_step "Starting frontend service"
if wait_for_http "$FRONTEND_URL" "frontend" 2; then
log_info "Frontend already running on port 3000"
return 0
fi
if port_in_use 3000; then
log_error "Port 3000 in use but frontend not responding"
return 1
fi
cd "$FRONTEND_DIR"
if [[ "$SKIP_DEPS" == "false" ]] && [[ ! -d "node_modules" ]]; then
log_info "Installing frontend dependencies..."
pnpm install --ignore-scripts
# Approve builds for native modules (sharp, etc.) to avoid pnpm blocking on startup
pnpm approve-builds --silent 2>/dev/null || true
fi
export NEXT_PUBLIC_API_URL="${NEXT_PUBLIC_API_URL:-http://localhost:8090}"
export NODE_ENV="${NODE_ENV:-development}"
local start; start=$(timer_start)
log_info "Starting Next.js dev server..."
# Use direct node_modules path to bypass pnpm wrapper overhead on each reload
nohup ./node_modules/.bin/next dev --port 3000 > "$LOG_DIR/frontend.log" 2>&1 &
local pid=$!
local pid=$!
echo "$pid" > "$PID_DIR/frontend.pid"
log_info "Frontend PID: $pid"
if wait_for_http "$FRONTEND_URL" "frontend" 90; then
timer_end "$start" "Frontend startup"
log_success "Frontend is healthy"
else
log_warn "Frontend may need more time. Check: $LOG_DIR/frontend.log"
fi
}
local_down() {
log_step "Stopping local services"
# Backend
if [[ -f "$PID_DIR/backend.pid" ]]; then
local pid; pid=$(cat "$PID_DIR/backend.pid")
if kill -0 "$pid" 2>/dev/null; then
kill "$pid" 2>/dev/null || true
log_success "Backend stopped (PID: $pid)"
fi
rm -f "$PID_DIR/backend.pid"
else
pkill -f "go run main.go" 2>/dev/null && log_success "Backend stopped" || true
fi
# Frontend
if [[ -f "$PID_DIR/frontend.pid" ]]; then
local pid; pid=$(cat "$PID_DIR/frontend.pid")
if kill -0 "$pid" 2>/dev/null; then
kill "$pid" 2>/dev/null || true
log_success "Frontend stopped (PID: $pid)"
fi
rm -f "$PID_DIR/frontend.pid"
else
pkill -f "pnpm dev" 2>/dev/null && log_success "Frontend stopped" || true
fi
# Optionally clean infrastructure
if [[ "${1:-}" == "--clean" ]]; then
log_info "Stopping infrastructure containers..."
docker stop itsm-postgres-dev itsm-redis-dev 2>/dev/null || true
docker rm itsm-postgres-dev itsm-redis-dev 2>/dev/null || true
log_success "Infrastructure containers removed"
fi
}
# ============================================================
# Health checks
# ============================================================
run_health_checks() {
log_step "Running health checks"
local all_ok=true
# Backend
if wait_for_http "${BACKEND_URL}${HEALTH_PATH}" "backend" 3; then
log_success "Backend: healthy"
else
log_error "Backend: unreachable"
all_ok=false
fi
# Frontend
if wait_for_http "$FRONTEND_URL" "frontend" 3; then
log_success "Frontend: healthy"
else
log_error "Frontend: unreachable"
all_ok=false
fi
# PostgreSQL
if container_running "itsm-postgres-dev"; then
if docker exec itsm-postgres-dev pg_isready -U itsm -d itsm &>/dev/null; then
log_success "PostgreSQL: healthy"
else
log_error "PostgreSQL: not ready"
all_ok=false
fi
elif port_in_use 5432; then
log_success "PostgreSQL: external (port 5432 in use)"
else
log_warn "PostgreSQL: not running"
fi
# Redis
if container_running "itsm-redis-dev"; then
if docker exec itsm-redis-dev redis-cli ping &>/dev/null; then
log_success "Redis: healthy"
else
log_error "Redis: not ready"
all_ok=false
fi
elif port_in_use 6379; then
log_success "Redis: external (port 6379 in use)"
else
log_warn "Redis: not running"
fi
$all_ok && log_success "All health checks passed" || log_warn "Some health checks failed"
}
# ============================================================
# Init (first-time setup)
# ============================================================
cmd_init() {
log_phase "First-Time Setup"
# 1. Check Docker
if ! command -v docker &>/dev/null; then
log_error "Docker is required. Install: https://docs.docker.com/get-docker/"
exit 1
fi
log_success "Docker: $(docker --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)"
# 2. Create .env
if [[ ! -f "$PROJECT_ROOT/.env" ]] && [[ -f "$PROJECT_ROOT/.env.example" ]]; then
cp "$PROJECT_ROOT/.env.example" "$PROJECT_ROOT/.env"
log_success "Created .env from .env.example"
else
log_info ".env already exists"
fi
# 3. Ensure log directory
mkdir -p "$LOG_DIR"
log_success "Log directory ready"
# 4. Install frontend deps
if [[ ! -d "$FRONTEND_DIR/node_modules" ]]; then
log_info "Installing frontend dependencies..."
cd "$FRONTEND_DIR"
if ! command -v pnpm &>/dev/null; then
npm install -g pnpm@9.0.0
fi
pnpm install --ignore-scripts
log_success "Frontend dependencies installed"
else
log_info "Frontend dependencies already installed"
fi
# 5. Download Go modules
if [[ ! -d "$BACKEND_DIR/vendor" ]]; then
log_info "Downloading Go modules..."
cd "$BACKEND_DIR"
go mod download
log_success "Go modules downloaded"
fi
# 6. Start services
log_info "Starting services..."
local mode; mode=$(detect_mode)
if [[ "$mode" == "docker" ]]; then
docker_up
else
local_up
fi
}
# ============================================================
# Doctor (self-diagnostic)
# ============================================================
cmd_doctor() {
log_phase "ITSM Development Environment Diagnostic"
local issues=0
# Docker
echo -e "${BOLD}Docker${NC}"
if command -v docker &>/dev/null; then
if docker info &>/dev/null 2>&1; then
log_success "Docker daemon running"
else
log_error "Docker installed but daemon not running"
issues=$((issues + 1))
fi
else
log_error "Docker not installed"
issues=$((issues + 1))
fi
# Docker Compose
echo ""
echo -e "${BOLD}Docker Compose${NC}"
if docker compose version &>/dev/null 2>&1; then
log_success "Docker Compose v2 available"
else
log_error "Docker Compose v2 not available"
issues=$((issues + 1))
fi
# Ports
echo ""
echo -e "${BOLD}Ports${NC}"
for port_desc in "8090:Backend" "3000:Frontend" "5432:PostgreSQL" "6379:Redis"; do
local port="${port_desc%%:*}"
local name="${port_desc##*:}"
if port_in_use "$port"; then
log_warn "Port $port ($name) in use"
else
log_success "Port $port ($name) available"
fi
done
# Containers
echo ""
echo -e "${BOLD}Containers${NC}"
for container in itsm-postgres-dev itsm-redis-dev itsm-backend-dev itsm-frontend-dev; do
if container_running "$container"; then
log_success "$container: running"
else
log_info "$container: not running"
fi
done
# Health endpoints
echo ""
echo -e "${BOLD}Health Endpoints${NC}"
if wait_for_http "${BACKEND_URL}${HEALTH_PATH}" "backend" 3; then
log_success "Backend: healthy"
else
log_warn "Backend: unreachable"
fi
if wait_for_http "$FRONTEND_URL" "frontend" 3; then
log_success "Frontend: healthy"
else
log_warn "Frontend: unreachable"
fi
# Disk space
echo ""
echo -e "${BOLD}Disk Space${NC}"
require_disk_space 2 || issues=$((issues + 1))
# Summary
echo ""
if [[ $issues -eq 0 ]]; then
log_success "No issues found"
else
log_error "$issues issue(s) found. Fix before proceeding."
fi
return "$issues"
}
# ============================================================
# Status and display
# ============================================================
print_summary() {
local backend_status="stopped"
local frontend_status="stopped"
wait_for_http "${BACKEND_URL}${HEALTH_PATH}" "backend" 3 && backend_status="running"
wait_for_http "$FRONTEND_URL" "frontend" 3 && frontend_status="running"
print_banner "ITSM Development Environment" \
"$(status_row "Backend" "$backend_status" "$BACKEND_URL")" \
"$(status_row "Frontend" "$frontend_status" "$FRONTEND_URL")" \
"" \
"${CYAN}Login:${NC} ${DEFAULT_ADMIN_USER} / ${DEFAULT_ADMIN_PASS}" \
"${CYAN}API Docs:${NC} ${BACKEND_URL}/swagger"
}
show_status() {
local mode; mode=$(detect_mode)
echo ""
echo -e "${BOLD}ITSM Development Environment [${mode} mode]${NC}"
echo "─────────────────────────────────────────"
if [[ "$mode" == "docker" ]]; then
dc -f "$COMPOSE_DEV" --profile dev ps 2>/dev/null || echo " No containers running"
else
# Backend
if wait_for_http "${BACKEND_URL}${HEALTH_PATH}" "backend" 3; then
status_row "Backend" "running" "$BACKEND_URL"
else
status_row "Backend" "stopped" "$BACKEND_URL"
fi
# Frontend
if wait_for_http "$FRONTEND_URL" "frontend" 3; then
status_row "Frontend" "running" "$FRONTEND_URL"
else
status_row "Frontend" "stopped" "$FRONTEND_URL"
fi
# Infrastructure
container_running "itsm-postgres-dev" && status_row "PostgreSQL" "running" "localhost:5432" || status_row "PostgreSQL" "stopped" "localhost:5432"
container_running "itsm-redis-dev" && status_row "Redis" "running" "localhost:6379" || status_row "Redis" "stopped" "localhost:6379"
fi
echo ""
}
show_logs() {
local mode; mode=$(detect_mode)
local service="${1:-}"
if [[ "$mode" == "docker" ]]; then
docker_logs "$service"
else
echo "Tailing logs (Ctrl+C to exit)"
echo "─────────────────────────────────────────"
[[ -f "$LOG_DIR/backend.log" ]] && tail -f "$LOG_DIR/backend.log" &
[[ -f "$LOG_DIR/frontend.log" ]] && tail -f "$LOG_DIR/frontend.log" &
wait
fi
}
# ============================================================
# Help
# ============================================================
show_help() {
print_banner "ITSM Development Deployment Script v${ITSM_SCRIPT_VERSION}"
cat <<EOF
Usage: $(basename "$0") [command] [options]
${BOLD}Commands:${NC}
up Start all services (default)
down Stop all services
restart Restart all services
status Show service status
logs [svc] Tail service logs (e.g. logs itsm-backend)
health Run health checks
init First-time setup (install deps + start)
doctor Diagnose common issues
reset Remove all containers and volumes
help Show this help
${BOLD}Options:${NC}
--local Force local development mode
--docker Force Docker Compose mode
--skip-deps Skip dependency installation
--no-build Skip Docker image rebuild (faster restart)
--verbose Show full Docker build output
-h, --help Show help
${BOLD}Quick Start:${NC}
./scripts/deploy-dev.sh init # First time: install + start
./scripts/deploy-dev.sh up # Start (auto-detect mode)
./scripts/deploy-dev.sh up --docker # Force Docker mode
./scripts/deploy-dev.sh up --no-build # Restart without rebuild
./scripts/deploy-dev.sh doctor # Diagnose issues
./scripts/deploy-dev.sh down # Stop everything
${BOLD}Access:${NC}
Frontend: http://localhost:3000
Backend: http://localhost:8090
API Docs: http://localhost:8090/swagger
Login: admin / admin123
EOF
}
# ============================================================
# Main
# ============================================================
main() {
local mode; mode=$(detect_mode)
case "$COMMAND" in
up)
if [[ "$mode" == "docker" ]]; then docker_up
else local_up; fi ;;
down)
if [[ "$mode" == "docker" ]]; then docker_down
else local_down "$@"; fi ;;
restart)
if [[ "$mode" == "docker" ]]; then docker_down && docker_up
else local_down && sleep 2 && local_up; fi ;;
status) show_status ;;
logs) show_logs ;;
health) run_health_checks ;;
init) cmd_init ;;
doctor) cmd_doctor ;;
reset)
if [[ "$mode" == "docker" ]]; then docker_reset
else local_down "--clean"; fi ;;
help) show_help ;;
*)
log_error "Unknown command: $COMMAND"
show_help
exit 1
;;
esac
}
main