-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtav
More file actions
executable file
·471 lines (376 loc) · 11.2 KB
/
mtav
File metadata and controls
executable file
·471 lines (376 loc) · 11.2 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
#!/usr/bin/env bash
# MTAV Stack - Unified development script
# Orchestrates backend API and frontend SPA services
set -e
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Change to script directory
cd "$SCRIPT_DIR"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Project configuration
PROJECT_NAME="mtav-stack"
COMPOSE_FILE="docker-compose.yml"
# Helper functions
info() {
echo -e "${BLUE}ℹ${NC} $1"
}
success() {
echo -e "${GREEN}✓${NC} $1"
}
error() {
echo -e "${RED}✗${NC} $1"
}
warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
# Check if Docker is running
check_docker() {
if ! docker info > /dev/null 2>&1; then
error "Docker is not running. Please start Docker and try again."
exit 1
fi
}
# Check submodules
check_submodules() {
if [ ! -f "api/manage.py" ] || [ ! -f "spa/package.json" ]; then
error "Submodules not initialized. Run: git submodule update --init --recursive"
exit 1
fi
}
# Show usage
usage() {
cat << EOF
${GREEN}MTAV Stack - Multi-Repo Development Tool${NC}
Usage: ./mtav <command> [options]
${YELLOW}Container Management:${NC}
up Start all services (first-time setup)
down Stop and remove all containers
stop Stop containers (keep data)
start Start stopped containers
restart Restart all containers
status Show running containers
logs [service] Show logs (optional: specific service)
${YELLOW}Development:${NC}
shell <service> Open shell (backend/frontend/db/redis)
backend <cmd> Run Django management command
api <cmd> Alias for backend
frontend <cmd> Run npm command in frontend
spa <cmd> Alias for frontend
${YELLOW}Database:${NC}
migrate Run database migrations
makemigrations Create new migrations
dbshell Open PostgreSQL shell
reset-db Drop and recreate database
${YELLOW}Testing:${NC}
test Run all tests (backend + frontend)
test-backend Run Django/pytest tests
test-api Alias for test-backend
test-frontend Run React/Vitest tests
test-spa Alias for test-frontend
coverage Run tests with coverage
${YELLOW}Maintenance:${NC}
fresh Nuclear option: rebuild everything
rebuild [svc] Rebuild containers (optional: specific service)
update Update dependencies (pip + npm)
clean Remove stopped containers and images
${YELLOW}Submodules:${NC}
submodule-init Initialize submodules
submodule-update Update submodules to latest
submodule-status Show submodule status
${YELLOW}Production:${NC}
build <tag> Build production images
deploy Deploy to production (placeholder)
${YELLOW}Utilities:${NC}
superuser Create Django superuser
install Install all dependencies
Examples:
./mtav up # Start everything
./mtav backend migrate # Run migrations
./mtav test # Run all tests
./mtav shell backend # Open backend shell
./mtav fresh # Fresh rebuild
EOF
}
# Main commands
cmd_up() {
info "Starting MTAV Stack development environment..."
check_docker
check_submodules
# Create .env if it doesn't exist
if [ ! -f .env ]; then
info "Creating .env from .env.example..."
cp .env.example .env
success ".env file created"
fi
# Build and start containers
docker-compose up -d --build
# Wait for database to be ready
info "Waiting for database to be ready..."
sleep 5
# Run migrations
info "Running database migrations..."
docker-compose exec -T backend python manage.py migrate
# Install frontend dependencies if needed
if [ ! -d "spa/node_modules" ]; then
info "Installing frontend dependencies..."
docker-compose exec -T frontend npm install
fi
success "MTAV Stack is ready!"
echo ""
info "Access points:"
echo " Frontend SPA: http://localhost:3000"
echo " Backend API: http://localhost:8000/api"
echo " API Docs: http://localhost:8000/api/docs/"
echo " Admin Panel: http://localhost:8000/admin"
echo " MailHog: http://localhost:8025"
echo ""
info "Run './mtav logs' to see output"
}
cmd_down() {
info "Stopping and removing containers..."
docker-compose down
success "Containers stopped and removed"
}
cmd_stop() {
info "Stopping containers..."
docker-compose stop
success "Containers stopped"
}
cmd_start() {
info "Starting containers..."
docker-compose start
success "Containers started"
}
cmd_restart() {
info "Restarting containers..."
docker-compose restart
success "Containers restarted"
}
cmd_status() {
docker-compose ps
}
cmd_logs() {
if [ -n "$1" ]; then
docker-compose logs -f "$1"
else
docker-compose logs -f
fi
}
cmd_shell() {
local service=${1:-backend}
case "$service" in
backend|api|django|python)
docker-compose exec backend bash
;;
frontend|spa|react|node)
docker-compose exec frontend sh
;;
db|database|postgres)
docker-compose exec db psql -U mtav -d mtav
;;
redis)
docker-compose exec redis redis-cli
;;
*)
error "Unknown service: $service"
info "Available: backend, frontend, db, redis"
exit 1
;;
esac
}
cmd_backend() {
docker-compose exec backend python manage.py "$@"
}
cmd_frontend() {
docker-compose exec frontend npm "$@"
}
cmd_migrate() {
info "Running migrations..."
docker-compose exec backend python manage.py migrate
success "Migrations complete"
}
cmd_makemigrations() {
info "Creating migrations..."
docker-compose exec backend python manage.py makemigrations "$@"
success "Migrations created"
}
cmd_dbshell() {
docker-compose exec db psql -U mtav -d mtav
}
cmd_reset_db() {
warning "This will DELETE ALL DATA. Are you sure? (y/N)"
read -r response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
info "Cancelled"
exit 0
fi
info "Dropping and recreating database..."
docker-compose exec db psql -U mtav -c "DROP DATABASE IF EXISTS mtav;"
docker-compose exec db psql -U mtav -c "CREATE DATABASE mtav;"
info "Running migrations..."
docker-compose exec backend python manage.py migrate
success "Database reset complete"
}
cmd_test() {
info "Running all tests..."
# Backend tests
info "Running backend tests..."
docker-compose exec backend pytest "$@"
# Frontend tests
info "Running frontend tests..."
docker-compose exec frontend npm test -- --run
success "All tests complete"
}
cmd_test_backend() {
docker-compose exec backend pytest "$@"
}
cmd_test_frontend() {
docker-compose exec frontend npm test "$@"
}
cmd_coverage() {
info "Running tests with coverage..."
docker-compose exec backend pytest --cov=apps --cov-report=html --cov-report=term
success "Coverage report generated in api/htmlcov/"
}
cmd_fresh() {
warning "This will DESTROY ALL DATA and rebuild from scratch. Continue? (y/N)"
read -r response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
info "Cancelled"
exit 0
fi
info "Nuclear option activated! Rebuilding everything..."
# Stop and remove everything
docker-compose down -v
# Remove Python cache
find api -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
find api -type f -name "*.pyc" -delete 2>/dev/null || true
# Remove node_modules
rm -rf spa/node_modules 2>/dev/null || true
# Rebuild and start
cmd_up
success "Fresh rebuild complete!"
}
cmd_rebuild() {
local service="$1"
if [ -n "$service" ]; then
info "Rebuilding $service..."
docker-compose up -d --build "$service"
else
info "Rebuilding all services..."
docker-compose up -d --build
fi
success "Rebuild complete"
}
cmd_update() {
info "Updating dependencies..."
# Update Python packages
info "Updating Python packages..."
docker-compose exec backend pip install --upgrade -r requirements.txt
# Update Node packages
info "Updating Node packages..."
docker-compose exec frontend npm update
success "Dependencies updated"
}
cmd_clean() {
info "Cleaning up Docker resources..."
docker-compose down
docker system prune -f
success "Cleanup complete"
}
cmd_submodule_init() {
info "Initializing submodules..."
git submodule update --init --recursive
success "Submodules initialized"
}
cmd_submodule_update() {
info "Updating submodules to latest..."
git submodule update --remote --merge
success "Submodules updated"
}
cmd_submodule_status() {
git submodule status
}
cmd_build() {
local tag=${1:-latest}
info "Building production images with tag: $tag..."
docker build -t mtav-api:$tag ./api
docker build -t mtav-spa:$tag ./spa
success "Production images built"
}
cmd_deploy() {
error "Deploy command not yet implemented"
info "This is a placeholder for production deployment"
exit 1
}
cmd_superuser() {
docker-compose exec backend python manage.py createsuperuser
}
cmd_install() {
info "Installing all dependencies..."
# Backend
info "Installing backend dependencies..."
docker-compose exec backend pip install -r requirements.txt
# Frontend
info "Installing frontend dependencies..."
docker-compose exec frontend npm install
success "All dependencies installed"
}
# Main script
main() {
local command="$1"
shift || true
case "$command" in
# Container management
up) cmd_up "$@" ;;
down) cmd_down "$@" ;;
stop) cmd_stop "$@" ;;
start) cmd_start "$@" ;;
restart) cmd_restart "$@" ;;
status) cmd_status "$@" ;;
logs) cmd_logs "$@" ;;
# Development
shell) cmd_shell "$@" ;;
backend|api) cmd_backend "$@" ;;
frontend|spa) cmd_frontend "$@" ;;
# Database
migrate) cmd_migrate "$@" ;;
makemigrations) cmd_makemigrations "$@" ;;
dbshell) cmd_dbshell "$@" ;;
reset-db) cmd_reset_db "$@" ;;
# Testing
test) cmd_test "$@" ;;
test-backend|test-api) cmd_test_backend "$@" ;;
test-frontend|test-spa) cmd_test_frontend "$@" ;;
coverage) cmd_coverage "$@" ;;
# Maintenance
fresh) cmd_fresh "$@" ;;
rebuild) cmd_rebuild "$@" ;;
update) cmd_update "$@" ;;
clean) cmd_clean "$@" ;;
# Submodules
submodule-init) cmd_submodule_init "$@" ;;
submodule-update) cmd_submodule_update "$@" ;;
submodule-status) cmd_submodule_status "$@" ;;
# Production
build) cmd_build "$@" ;;
deploy) cmd_deploy "$@" ;;
# Utilities
superuser) cmd_superuser "$@" ;;
install) cmd_install "$@" ;;
# Help
help|--help|-h|"") usage ;;
*)
error "Unknown command: $command"
echo ""
usage
exit 1
;;
esac
}
main "$@"