-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
342 lines (282 loc) · 13.6 KB
/
Copy pathMakefile
File metadata and controls
342 lines (282 loc) · 13.6 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
.PHONY: help dev-up dev-down dev-restart dev-logs dev-build dev-clean prod-up prod-down prod-restart prod-logs db-seed db-seed-reset frontend-up frontend-down frontend-build frontend-logs frontend-install frontend-shell
# Colors for output
BLUE := \033[0;34m
GREEN := \033[0;32m
YELLOW := \033[0;33m
RED := \033[0;31m
NC := \033[0m # No Color
COMPOSE ?= docker compose
help: ## Show this help message
@echo "$(BLUE)Model Maestro - Docker Management$(NC)"
@echo ""
@echo "$(GREEN)Development Commands:$(NC)"
@grep -E '^dev-[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " $(BLUE)%-20s$(NC) %s\n", $$1, $$2}'
@echo ""
@echo "$(GREEN)Production Commands:$(NC)"
@grep -E '^prod-[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " $(BLUE)%-20s$(NC) %s\n", $$1, $$2}'
@echo ""
@echo "$(GREEN)Database Commands:$(NC)"
@grep -E '^db-[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " $(BLUE)%-20s$(NC) %s\n", $$1, $$2}'
@echo ""
@echo "$(GREEN)Frontend Commands:$(NC)"
@grep -E '^frontend-[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " $(BLUE)%-20s$(NC) %s\n", $$1, $$2}'
@echo ""
@echo "$(GREEN)Monitoring Commands:$(NC)"
@grep -E '^logs-[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " $(BLUE)%-20s$(NC) %s\n", $$1, $$2}'
# ============================================================================
# Development Environment
# ============================================================================
dev-up: ## Start development environment (PostgreSQL, Redis, API, Celery)
@echo "$(GREEN)Starting development environment...$(NC)"
$(COMPOSE) -f docker-compose.dev.yml up -d
@echo "$(GREEN)✓ Development environment started$(NC)"
@echo ""
@echo "$(YELLOW)Services:$(NC)"
@echo " API: http://localhost:8000"
@echo " Docs: http://localhost:8000/api/docs"
@echo " Frontend: http://localhost:3000"
@echo " PostgreSQL: localhost:5432"
@echo " Redis: localhost:6379"
@echo ""
@echo "$(YELLOW)Next steps:$(NC)"
@echo " 1. Initialize database: make db-init"
@echo " 2. View logs: make dev-logs"
@echo " 3. Check status: make status"
dev-down: ## Stop development environment
@echo "$(YELLOW)Stopping development environment...$(NC)"
$(COMPOSE) -f docker-compose.dev.yml down --remove-orphans
@echo "$(GREEN)✓ Development environment stopped$(NC)"
dev-restart: ## Restart development environment
@echo "$(YELLOW)Restarting development environment...$(NC)"
$(COMPOSE) -f docker-compose.dev.yml restart
@echo "$(GREEN)✓ Development environment restarted$(NC)"
dev-logs: ## Show all development logs (follow)
$(COMPOSE) -f docker-compose.dev.yml logs -f
dev-build: ## Rebuild development containers
@echo "$(YELLOW)Rebuilding development containers...$(NC)"
$(COMPOSE) -f docker-compose.dev.yml build --no-cache
@echo "$(GREEN)✓ Containers rebuilt$(NC)"
dev-clean: ## Stop and remove all development containers and volumes
@echo "$(RED)Cleaning development environment (removing volumes)...$(NC)"
@echo "$(RED)WARNING: This will delete all data including PostgreSQL and Redis!$(NC)"
@read -p "Are you sure? (yes/no): " confirm; \
if [ "$$confirm" = "yes" ]; then \
$(COMPOSE) -f docker-compose.dev.yml down -v; \
echo "$(GREEN)✓ Development environment cleaned$(NC)"; \
else \
echo "$(YELLOW)Cancelled$(NC)"; \
fi
dev-shell: ## Open shell in API container
docker exec -it maestro /bin/bash
# ============================================================================
# Production Environment
# ============================================================================
prod-up: ## Start production environment
@echo "$(GREEN)Starting production environment...$(NC)"
$(COMPOSE) -f docker-compose.yml up -d
@echo "$(GREEN)✓ Production environment started$(NC)"
prod-down: ## Stop production environment
@echo "$(YELLOW)Stopping production environment...$(NC)"
$(COMPOSE) -f docker-compose.yml down --remove-orphans
@echo "$(GREEN)✓ Production environment stopped$(NC)"
prod-restart: ## Restart production environment
@echo "$(YELLOW)Restarting production environment...$(NC)"
$(COMPOSE) -f docker-compose.yml restart
@echo "$(GREEN)✓ Production environment restarted$(NC)"
prod-logs: ## Show production logs (follow)
$(COMPOSE) -f docker-compose.yml logs -f
prod-build: ## Rebuild production containers
@echo "$(YELLOW)Rebuilding production containers...$(NC)"
$(COMPOSE) -f docker-compose.yml build --no-cache
@echo "$(GREEN)✓ Containers rebuilt$(NC)"
# ============================================================================
# Database Management
# ============================================================================
db-init: ## Initialize database (run migrations)
@echo "$(YELLOW)Waiting for PostgreSQL to be ready...$(NC)"
@sleep 5
@echo "$(GREEN)Running database migrations...$(NC)"
docker exec maestro alembic upgrade head
@echo "$(GREEN)✓ Database initialized$(NC)"
db-migrate: ## Create new migration (provide MESSAGE="migration message")
@if [ -z "$(MESSAGE)" ]; then \
echo "$(RED)Error: Please provide MESSAGE=\"your migration message\"$(NC)"; \
exit 1; \
fi
@echo "$(YELLOW)Creating new migration: $(MESSAGE)$(NC)"
docker exec maestro alembic revision --autogenerate -m "$(MESSAGE)"
@echo "$(GREEN)✓ Migration created$(NC)"
db-upgrade: ## Upgrade database to latest migration
@echo "$(GREEN)Upgrading database...$(NC)"
docker exec maestro alembic upgrade head
@echo "$(GREEN)✓ Database upgraded$(NC)"
db-downgrade: ## Downgrade database by one migration
@echo "$(YELLOW)Downgrading database...$(NC)"
docker exec maestro alembic downgrade -1
@echo "$(GREEN)✓ Database downgraded$(NC)"
db-reset: ## Reset database (drops and recreates)
@echo "$(RED)WARNING: This will delete ALL data in the database!$(NC)"
@read -p "Are you sure? (yes/no): " confirm; \
if [ "$$confirm" = "yes" ]; then \
docker exec maestro-postgres psql -U maestro_user -d postgres -c "DROP DATABASE IF EXISTS maestro;"; \
docker exec maestro-postgres psql -U maestro_user -d postgres -c "CREATE DATABASE maestro;"; \
docker exec maestro alembic upgrade head; \
echo "$(GREEN)✓ Database reset complete$(NC)"; \
else \
echo "$(YELLOW)Cancelled$(NC)"; \
fi
db-fresh: ## Drop DB, recreate, run all migrations and seeders (no confirmation)
@echo "$(RED)Terminating connections and dropping database...$(NC)"
@docker exec maestro-postgres psql -U maestro_user -d postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'maestro' AND pid <> pg_backend_pid();" 2>/dev/null || true
@docker exec maestro-postgres psql -U maestro_user -d postgres -c "DROP DATABASE IF EXISTS maestro;"
@echo "$(GREEN)Creating fresh database...$(NC)"
@docker exec maestro-postgres psql -U maestro_user -d postgres -c "CREATE DATABASE maestro;"
@echo "$(GREEN)Running migrations...$(NC)"
@docker exec maestro alembic upgrade head
@echo "$(GREEN)Running seeders...$(NC)"
@docker exec maestro python -m app.seeder
@echo "$(GREEN)✓ Database fresh: migrations + seeds complete$(NC)"
db-seed: ## Seed database with pending seeds (migration-style)
@echo "$(GREEN)Running pending seeds...$(NC)"
docker exec maestro python -m app.seeder
@echo "$(GREEN)✓ Database seeded$(NC)"
db-seed-status: ## Show seed status (which seeds have been applied)
docker exec maestro python -m app.seeder --status
db-seed-reset: ## Reset seed history (data stays, seeds will re-run)
@echo "$(RED)WARNING: This will reset seed history - all seeds will re-run on next db-seed!$(NC)"
@read -p "Are you sure? (yes/no): " confirm; \
if [ "$$confirm" = "yes" ]; then \
docker exec maestro python -m app.seeder --reset; \
echo "$(GREEN)✓ Seed history reset$(NC)"; \
else \
echo "$(YELLOW)Cancelled$(NC)"; \
fi
db-seed-reset-all: ## Reset seed history AND remove seeded data
@echo "$(RED)WARNING: This will remove ALL seeded data and reset history!$(NC)"
@read -p "Are you sure? (yes/no): " confirm; \
if [ "$$confirm" = "yes" ]; then \
docker exec maestro python -m app.seeder --reset-all; \
echo "$(GREEN)✓ Seed data and history reset$(NC)"; \
else \
echo "$(YELLOW)Cancelled$(NC)"; \
fi
db-shell: ## Open PostgreSQL shell
@echo "$(YELLOW)Opening PostgreSQL shell...$(NC)"
@echo "Database: maestro"
@echo "User: maestro_user"
@echo ""
docker exec -it maestro-postgres psql -U maestro_user -d maestro
db-backup: ## Backup database to backups/ folder
@echo "$(GREEN)Creating database backup...$(NC)"
@mkdir -p backups
docker exec maestro-postgres pg_dump -U maestro_user maestro > backups/backup_$$(date +%Y%m%d_%H%M%S).sql
@echo "$(GREEN)✓ Backup created in backups/$(NC)"
db-restore: ## Restore database from backup (provide BACKUP_FILE=path/to/backup.sql)
@if [ -z "$(BACKUP_FILE)" ]; then \
echo "$(RED)Error: Please provide BACKUP_FILE=path/to/backup.sql$(NC)"; \
exit 1; \
fi
@echo "$(YELLOW)Restoring database from $(BACKUP_FILE)...$(NC)"
docker exec -i maestro-postgres psql -U maestro_user maestro < $(BACKUP_FILE)
@echo "$(GREEN)✓ Database restored$(NC)"
# ============================================================================
# Redis Management
# ============================================================================
redis-cli: ## Open Redis CLI
docker exec -it maestro-redis redis-cli
redis-flush: ## Flush all Redis cache
@echo "$(RED)WARNING: This will clear ALL Redis cache!$(NC)"
@read -p "Are you sure? (yes/no): " confirm; \
if [ "$$confirm" = "yes" ]; then \
docker exec maestro-redis redis-cli FLUSHALL; \
echo "$(GREEN)✓ Redis cache cleared$(NC)"; \
else \
echo "$(YELLOW)Cancelled$(NC)"; \
fi
redis-info: ## Show Redis info
docker exec maestro-redis redis-cli INFO
redis-keys: ## List all Redis keys
docker exec maestro-redis redis-cli KEYS "*"
# ============================================================================
# Monitoring & Logs
# ============================================================================
status: ## Show status of all services
@echo "$(BLUE)Service Status:$(NC)"
$(COMPOSE) -f docker-compose.dev.yml ps
logs-api: ## Show API container logs
$(COMPOSE) -f docker-compose.dev.yml logs -f maestro
logs-celery: ## Show Celery worker logs
$(COMPOSE) -f docker-compose.dev.yml logs -f celery-worker
logs-beat: ## Show Celery beat logs
$(COMPOSE) -f docker-compose.dev.yml logs -f celery-beat
logs-db: ## Show PostgreSQL logs
$(COMPOSE) -f docker-compose.dev.yml logs -f postgres
logs-redis: ## Show Redis logs
$(COMPOSE) -f docker-compose.dev.yml logs -f redis
stats: ## Show container resource usage
docker stats maestro maestro-postgres maestro-redis celery-worker celery-beat
health: ## Check health of all services
@echo "$(BLUE)Health Check:$(NC)"
@echo ""
@echo "$(YELLOW)API:$(NC)"
@curl -s http://localhost:8000/health | jq . || echo "$(RED)✗ API not responding$(NC)"
@echo ""
@echo "$(YELLOW)PostgreSQL:$(NC)"
@docker exec maestro-postgres pg_isready -U maestro_user -d maestro || echo "$(RED)✗ PostgreSQL not ready$(NC)"
@echo ""
@echo "$(YELLOW)Redis:$(NC)"
@docker exec maestro-redis redis-cli ping || echo "$(RED)✗ Redis not responding$(NC)"
# ============================================================================
# Quick Setup
# ============================================================================
setup: dev-up db-init ## Complete setup (start containers + initialize database)
@echo ""
@echo "$(GREEN)✓ Setup complete!$(NC)"
@echo ""
@echo "$(YELLOW)Environment is ready:$(NC)"
@echo " API: http://localhost:8000"
@echo " Docs: http://localhost:8000/api/docs (user: admin, pass: dev-docs-password)"
@echo ""
@echo "$(YELLOW)Create users via API:$(NC)"
@echo " POST /admin/users"
@echo " Authorization: Bearer <ADMIN_TOKEN from .env.dev>"
@echo ""
@echo "$(YELLOW)Useful commands:$(NC)"
@echo " View logs: make dev-logs"
@echo " Check status: make status"
@echo " Health check: make health"
# ============================================================================
# Maintenance
# ============================================================================
restart-api: ## Restart only API container
$(COMPOSE) -f docker-compose.dev.yml restart maestro
restart-celery: ## Restart Celery worker and beat
$(COMPOSE) -f docker-compose.dev.yml restart celery-worker celery-beat
restart-db: ## Restart PostgreSQL
$(COMPOSE) -f docker-compose.dev.yml restart postgres
restart-redis: ## Restart Redis
$(COMPOSE) -f docker-compose.dev.yml restart redis
rebuild: dev-build dev-up ## Rebuild and restart all containers
@echo "$(GREEN)✓ Rebuild complete$(NC)"
# ============================================================================
# Frontend
# ============================================================================
frontend-up: ## Start frontend container
@echo "$(GREEN)Starting frontend...$(NC)"
$(COMPOSE) -f docker-compose.dev.yml up -d frontend
@echo "$(GREEN)✓ Frontend started at http://localhost:3000$(NC)"
frontend-down: ## Stop frontend container
$(COMPOSE) -f docker-compose.dev.yml stop frontend
@echo "$(GREEN)✓ Frontend stopped$(NC)"
frontend-build: ## Rebuild frontend container
@echo "$(YELLOW)Rebuilding frontend...$(NC)"
$(COMPOSE) -f docker-compose.dev.yml build --no-cache frontend
@echo "$(GREEN)✓ Frontend rebuilt$(NC)"
frontend-logs: ## Show frontend logs
$(COMPOSE) -f docker-compose.dev.yml logs -f frontend
frontend-install: ## Install npm dependencies in frontend container
@echo "$(YELLOW)Installing frontend dependencies...$(NC)"
docker exec maestro-frontend npm install
@echo "$(GREEN)✓ Dependencies installed$(NC)"
frontend-shell: ## Open shell in frontend container
docker exec -it maestro-frontend /bin/sh