-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
349 lines (301 loc) · 11.6 KB
/
makefile
File metadata and controls
349 lines (301 loc) · 11.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
343
344
345
346
347
348
349
.PHONY: help setup up down logs clean test lint format
PROD_COMPOSE = docker compose -f docker-compose.prod.yml
RAILWAY_COMPOSE = docker compose -f docker-compose.railway.yml
help:
@echo "Folio - Investment Tracking Platform"
@echo ""
@echo "Infrastructure Commands:"
@echo " make setup Setup environment (.env files from examples)"
@echo " make up Start all services (Docker Compose)"
@echo " make down Stop all services"
@echo " make restart Restart all services"
@echo " make logs View logs from all services"
@echo " make logs-api View API service logs"
@echo " make logs-web View Web service logs"
@echo " make logs-db View Database service logs"
@echo ""
@echo "Database Commands:"
@echo " make db-shell Access PostgreSQL shell"
@echo " make db-reset Reset database (drop and recreate)"
@echo " make db-migrate Run database migrations"
@echo " make db-seed Seed database with sample data"
@echo ""
@echo "API Commands:"
@echo " make api-shell Access API container shell"
@echo " make api-lint Lint Python code"
@echo " make api-format Format Python code (black, isort)"
@echo " make api-test Run API tests"
@echo " make api-repl Start Python REPL in API container"
@echo ""
@echo "Web Commands:"
@echo " make web-shell Access Web container shell"
@echo " make web-build Build production frontend"
@echo " make web-lint Lint TypeScript/Svelte code"
@echo " make web-test Run frontend tests"
@echo ""
@echo "Production Commands:"
@echo " make prod-up Start all services in production mode"
@echo " make prod-down Stop all production services"
@echo " make prod-restart Restart all production services"
@echo " make prod-logs Stream logs from production services"
@echo " make prod-migrate Run database migrations in production"
@echo " make prod-health Check production service health"
@echo " make prod-pull Pull latest images from GHCR"
@echo " make prod-seed Seed production database with sample data"
@echo ""
@echo "Railway Test Commands:"
@echo " make railway-up Build and start services using Railway Dockerfiles"
@echo " make railway-down Stop Railway test services"
@echo " make railway-logs Stream Railway test service logs"
@echo " make railway-seed Seed Railway test database with sample data"
@echo ""
@echo "Utility Commands:"
@echo " make setup Setup dev environment (.env files from examples)"
@echo " make prod-setup Setup prod environment (.env.prod files from examples)"
@echo " make clean Clean up containers and volumes"
@echo " make clean-hard Remove all containers, volumes, and images"
@echo " make health Check service health status"
@echo " make env Generate .env files from examples"
@echo " make validate Validate docker-compose configuration"
@echo ""
# Setup & Environment
setup:
@echo "Setting up dev environment..."
@cp -n api/.env.example api/.env && echo "✓ Created api/.env" || echo "ℹ api/.env already exists"
@cp -n web/.env.local.example web/.env.local && echo "✓ Created web/.env.local" || echo "ℹ web/.env.local already exists"
@echo ""
@echo "Setup complete! Next steps:"
@echo " 1. Review and customize api/.env if needed"
@echo " 2. Review and customize web/.env.local if needed"
@echo " 3. Run 'make up' to start services"
prod-setup:
@echo "Setting up production environment..."
@cp -n api/.env.prod.example api/.env.prod && echo "✓ Created api/.env.prod" || echo "ℹ api/.env.prod already exists"
@cp -n web/.env.local.example web/.env.local && echo "✓ Created web/.env.local" || echo "ℹ web/.env.local already exists"
@echo ""
@echo "Setup complete! Next steps:"
@echo " 1. Review and customize api/.env.prod with production values"
@echo " 2. Review and customize web/.env.local if needed"
@echo " 3. Run 'make prod-pull' then 'make prod-up' to start services"
env: setup
validate:
@echo "Validating docker-compose configuration..."
@which docker-compose > /dev/null || which docker > /dev/null && \
(docker compose config > /dev/null && echo "✓ Configuration is valid" || echo "✗ Configuration is invalid") || \
echo "✗ Docker/Docker Compose not found"
# Docker Compose Operations
up:
@echo "Starting services..."
@docker compose up -d --build
@echo ""
@echo "Services starting. Wait for health checks..."
@sleep 3
@make health
down:
@echo "Stopping services..."
@docker compose down
@echo "✓ Services stopped"
restart:
@echo "Restarting services..."
@docker compose restart
@echo "✓ Services restarted"
logs:
@docker compose logs -f
logs-api:
@docker compose logs -f api
logs-web:
@docker compose logs -f web
logs-db:
@docker compose logs -f db
health:
@echo "Checking service health..."
@echo ""
@echo "Database:"
@docker compose exec -T db pg_isready -U folio 2>/dev/null && echo " ✓ PostgreSQL healthy" || echo " ✗ PostgreSQL unhealthy"
@echo ""
@echo "API:"
@curl -s http://localhost:8000/health > /dev/null && echo " ✓ API healthy" || echo " ✗ API unhealthy"
@echo ""
@echo "Web:"
@curl -s http://localhost:3000 > /dev/null && echo " ✓ Web healthy" || echo " ✗ Web unhealthy"
# Database Operations
db-shell:
@echo "Connecting to PostgreSQL..."
@docker compose exec db psql -U folio -d folio
db-reset:
@echo "WARNING: This will delete all data in the database!"
@read -p "Continue? (y/n) " -n 1 -r; \
echo ""; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
docker compose exec -T db psql -U folio -d folio -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;" && \
docker compose exec api python -m alembic upgrade head && \
echo "✓ Database reset and migrated"; \
else \
echo "Cancelled"; \
fi
db-migrate:
@echo "Running database migrations..."
@docker compose exec api python -m alembic upgrade head
@echo "✓ Migrations complete"
db-seed:
@echo "Seeding database with sample data..."
@docker compose exec api python -m infrastructure.db.seed
@echo "✓ Database seeded"
# API Operations
api-shell:
@echo "Accessing API container..."
@docker compose exec api /bin/bash
api-lint:
@echo "Linting Python code..."
@docker compose exec api python -m flake8 api/
@echo "✓ Linting complete"
api-format:
@echo "Formatting Python code..."
@docker compose exec api black api/
@docker compose exec api isort api/
@echo "✓ Formatting complete"
api-test:
@echo "Running API tests..."
@docker compose exec api python -m pytest
@echo "✓ Tests complete"
api-repl:
@echo "Starting Python REPL in API container..."
@docker compose exec api python
# Web Operations
web-shell:
@echo "Accessing Web container..."
@docker compose exec web /bin/bash
web-build:
@echo "Building production frontend..."
@docker compose exec web npm run build
@echo "✓ Build complete"
web-lint:
@echo "Linting TypeScript/Svelte code..."
@docker compose exec web npm run lint
@echo "✓ Linting complete"
web-test:
@echo "Running frontend tests..."
@docker compose exec web npm test
@echo "✓ Tests complete"
# Production Operations
prod-up:
@echo "Starting production services..."
@$(PROD_COMPOSE) --env-file api/.env.prod --env-file web/.env.local up -d
@echo ""
@echo "Services starting. Wait for health checks..."
@sleep 3
@make prod-health
prod-down:
@echo "Stopping production services..."
@$(PROD_COMPOSE) down
@echo "✓ Production services stopped"
prod-restart:
@echo "Restarting production services..."
@$(PROD_COMPOSE) restart
@echo "✓ Production services restarted"
prod-logs:
@$(PROD_COMPOSE) logs -f
prod-migrate:
@echo "Running database migrations in production..."
@$(PROD_COMPOSE) --env-file api/.env.prod --env-file web/.env.local exec api python -m alembic upgrade head
@echo "✓ Migrations complete"
prod-health:
@echo "Checking production service health..."
@echo ""
@echo "Database:"
@$(PROD_COMPOSE) exec -T db pg_isready -U $${DB_USER:-folio} 2>/dev/null && echo " ✓ PostgreSQL healthy" || echo " ✗ PostgreSQL unhealthy"
@echo ""
@echo "API:"
@curl -s http://localhost:$${API_PORT:-8000}/health > /dev/null && echo " ✓ API healthy" || echo " ✗ API unhealthy"
@echo ""
@echo "Web:"
@curl -s http://localhost:$${WEB_PORT:-3000} > /dev/null && echo " ✓ Web healthy" || echo " ✗ Web unhealthy"
prod-pull:
@echo "Pulling latest images from GHCR..."
@$(PROD_COMPOSE) --env-file api/.env.prod --env-file web/.env.local pull api web
@echo "✓ Images updated"
prod-seed:
@echo "Seeding production database with sample data..."
@$(PROD_COMPOSE) exec api python -m infrastructure.db.seed
@echo "✓ Database seeded"
# Railway Test Operations
railway-up:
@echo "Building and starting services using Railway Dockerfiles..."
@$(RAILWAY_COMPOSE) --env-file api/.env --env-file web/.env.local up -d --build
@echo ""
@echo "Services starting on offset ports (api :8001, web :3001)..."
@sleep 5
@echo "API:"
@curl -s http://localhost:8001/health > /dev/null && echo " ✓ API healthy" || echo " ✗ API unhealthy (may still be starting)"
@echo "Web:"
@curl -s http://localhost:3001 > /dev/null && echo " ✓ Web healthy" || echo " ✗ Web unhealthy (may still be starting)"
railway-down:
@echo "Stopping Railway test services..."
@$(RAILWAY_COMPOSE) down
@echo "✓ Railway test services stopped"
railway-logs:
@$(RAILWAY_COMPOSE) logs -f
railway-seed:
@echo "Seeding Railway test database with sample data..."
@$(RAILWAY_COMPOSE) exec api python -m infrastructure.db.seed
@echo "✓ Database seeded"
# Cleanup Operations
clean:
@echo "Cleaning up containers and temporary files..."
@docker compose down
@docker system prune -f
@echo "✓ Cleanup complete"
clean-hard:
@echo "WARNING: This will remove all containers, volumes, and images!"
@read -p "Continue? (y/n) " -n 1 -r; \
echo ""; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
docker compose down -v; \
docker system prune -af; \
echo "✓ Hard cleanup complete"; \
else \
echo "Cancelled"; \
fi
# Additional Utilities
reset: clean setup up
@echo "✓ Full reset complete"
install-api-deps:
@echo "Installing API dependencies..."
@docker compose exec api pip install -r requirements.txt
@echo "✓ Dependencies installed"
install-web-deps:
@echo "Installing Web dependencies..."
@docker compose exec web npm install
@echo "✓ Dependencies installed"
status:
@echo "Service Status:"
@docker compose ps
TAG ?= v0.1.0
test_ci_workflow:
@command -v act > /dev/null 2>&1 || (echo "Error: 'act' is not installed or not in PATH. See https://github.com/nektos/act" && exit 1)
@if [ -z "$(GH_TOKEN)" ]; then \
echo "Error: Please provide a github token. Usage: make test_ci_workflow GH_TOKEN=<your-github-pat>"; \
exit 1; \
fi
@if [ -z "$(DHI_USERNAME)" ]; then \
echo "Error: Please provide a DHI username. Usage: make test_ci_workflow DHI_USERNAME=<your-dhi-username>"; \
exit 1; \
fi
@if [ -z "$(DHI_PATOKEN)" ]; then \
echo "Error: Please provide a DHI PAT token. Usage: make test_ci_workflow DHI_PATOKEN=<your-dhi-pat>"; \
exit 1; \
fi
@echo "Running CI workflow with tag $(TAG)..."
@printf '{"action":"published","release":{"tag_name":"$(TAG)","name":"$(TAG)","draft":false,"prerelease":false},"ref":"refs/tags/$(TAG)"}' > /tmp/act-release-event.json
@act release \
-e /tmp/act-release-event.json \
--secret GH_TOKEN=$(GH_TOKEN) \
--secret DHI_USERNAME=$(DHI_USERNAME) \
--secret DHI_PATOKEN=$(DHI_PATOKEN) \
--artifact-server-path /tmp/act-artifacts \
--dryrun
backup:
@echo "Backing up database..."
@docker compose exec -T db pg_dump -U folio folio > backups/folio_$(shell date +%Y%m%d_%H%M%S).sql
@echo "✓ Backup created in backups/"
ps: status