-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
284 lines (233 loc) · 8.9 KB
/
Makefile
File metadata and controls
284 lines (233 loc) · 8.9 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
# ============================================
# AnimAI - Developer Makefile
# ============================================
.PHONY: all format lint test tests test_watch integration_tests docker_tests help extended_tests
.PHONY: install dev docker-build docker-run docker-stop deploy logs health clean
# Configuration
PROJECT_ID := anim-482714
REGION := us-central1
SERVICE_NAME := animai-api
IMAGE_NAME := animai
REPO_NAME := animai-repo
IMAGE_URL := $(REGION)-docker.pkg.dev/$(PROJECT_ID)/$(REPO_NAME)/$(IMAGE_NAME):latest
# Colors
GREEN := \033[0;32m
YELLOW := \033[1;33m
RED := \033[0;31m
NC := \033[0m
# Default target executed when no arguments are given to make.
all: help
# Define a variable for the test file path.
TEST_FILE ?= tests/unit_tests/
# ============================================
# LOCAL DEVELOPMENT
# ============================================
install:
@echo "$(GREEN)Installing dependencies...$(NC)"
pip install -r requirements.txt
dev:
@echo "$(GREEN)Starting local development server...$(NC)"
PYTHONPATH=src uvicorn src.api.main:app --reload --host 0.0.0.0 --port 8000
dev-port:
@echo "$(GREEN)Starting on port $(PORT)...$(NC)"
PYTHONPATH=src uvicorn src.api.main:app --reload --host 0.0.0.0 --port $(PORT)
# ============================================
# TESTING
# ============================================
test:
python -m pytest $(TEST_FILE)
integration_tests:
python -m pytest tests/integration_tests
test_watch:
python -m ptw --snapshot-update --now . -- -vv tests/unit_tests
test_profile:
python -m pytest -vv tests/unit_tests/ --profile-svg
extended_tests:
python -m pytest --only-extended $(TEST_FILE)
# ============================================
# LINTING AND FORMATTING
# ============================================
# Define a variable for Python and notebook files.
PYTHON_FILES=src/
MYPY_CACHE=.mypy_cache
lint format: PYTHON_FILES=.
lint_diff format_diff: PYTHON_FILES=$(shell git diff --name-only --diff-filter=d main | grep -E '\.py$$|\.ipynb$$')
lint_package: PYTHON_FILES=src
lint_tests: PYTHON_FILES=tests
lint_tests: MYPY_CACHE=.mypy_cache_test
lint lint_diff lint_package lint_tests:
python -m ruff check .
[ "$(PYTHON_FILES)" = "" ] || python -m ruff format $(PYTHON_FILES) --diff
[ "$(PYTHON_FILES)" = "" ] || python -m ruff check --select I $(PYTHON_FILES)
[ "$(PYTHON_FILES)" = "" ] || python -m mypy --strict $(PYTHON_FILES)
[ "$(PYTHON_FILES)" = "" ] || mkdir -p $(MYPY_CACHE) && python -m mypy --strict $(PYTHON_FILES) --cache-dir $(MYPY_CACHE)
format format_diff:
ruff format $(PYTHON_FILES)
ruff check --select I --fix $(PYTHON_FILES)
spell_check:
codespell --toml pyproject.toml
spell_fix:
codespell --toml pyproject.toml -w
# ============================================
# DOCKER
# ============================================
docker-build:
@echo "$(GREEN)Building Docker image...$(NC)"
docker build -t $(IMAGE_NAME) .
docker tag $(IMAGE_NAME) $(IMAGE_URL)
docker-run:
@echo "$(GREEN)Running Docker container...$(NC)"
docker run -p 8000:8000 --env-file .env --name $(SERVICE_NAME) -d $(IMAGE_NAME)
@echo "$(GREEN)Container running at http://localhost:8000$(NC)"
docker-stop:
@echo "$(YELLOW)Stopping container...$(NC)"
-docker stop $(SERVICE_NAME)
-docker rm $(SERVICE_NAME)
docker-logs:
docker logs -f $(SERVICE_NAME)
docker-shell:
docker exec -it $(SERVICE_NAME) /bin/bash
docker-clean:
@echo "$(YELLOW)Cleaning Docker images...$(NC)"
-docker rmi $(IMAGE_NAME)
-docker rmi $(IMAGE_URL)
# ============================================
# TERRAFORM
# ============================================
tf-init:
@echo "$(GREEN)Initializing Terraform...$(NC)"
cd terraform && terraform init
tf-plan:
@echo "$(GREEN)Planning Terraform changes...$(NC)"
cd terraform && terraform plan
tf-apply:
@echo "$(GREEN)Applying Terraform changes...$(NC)"
cd terraform && terraform apply -lock=false
tf-apply-auto:
@echo "$(GREEN)Applying Terraform (auto-approve)...$(NC)"
cd terraform && terraform apply -lock=false -auto-approve
tf-destroy:
@echo "$(RED)Destroying infrastructure...$(NC)"
cd terraform && terraform destroy -lock=false
tf-output:
@cd terraform && terraform output
tf-unlock:
@echo "$(YELLOW)Removing Terraform locks...$(NC)"
cd terraform && rm -f .terraform.tfstate.lock.info terraform.tfstate.lock.info
# ============================================
# GCP DEPLOYMENT
# ============================================
gcp-auth:
@echo "$(GREEN)Authenticating with GCP...$(NC)"
gcloud auth login
gcloud auth application-default login
gcloud auth configure-docker $(REGION)-docker.pkg.dev
push:
@echo "$(GREEN)Pushing image to Artifact Registry...$(NC)"
docker push $(IMAGE_URL)
update:
@echo "$(GREEN)Updating Cloud Run service...$(NC)"
gcloud run services update $(SERVICE_NAME) \
--region=$(REGION) \
--image=$(IMAGE_URL)
deploy: docker-build push update
@echo "$(GREEN)Deployment complete!$(NC)"
@make url
deploy-full: docker-build push tf-apply
@echo "$(GREEN)Full deployment complete!$(NC)"
@make url
url:
@echo "$(GREEN)Service URL:$(NC)"
@cd terraform && terraform output -raw service_url
# ============================================
# MONITORING & LOGS
# ============================================
logs:
gcloud run services logs read $(SERVICE_NAME) --region=$(REGION) --limit=100
logs-follow:
gcloud run services logs tail $(SERVICE_NAME) --region=$(REGION)
describe:
gcloud run services describe $(SERVICE_NAME) --region=$(REGION)
# ============================================
# API TESTING
# ============================================
health:
@SERVICE_URL=$$(cd terraform && terraform output -raw service_url 2>/dev/null) && \
echo "$(GREEN)Testing: $$SERVICE_URL/health$(NC)" && \
curl -s $$SERVICE_URL/health | jq .
test-run:
@SERVICE_URL=$$(cd terraform && terraform output -raw service_url 2>/dev/null) && \
echo "$(GREEN)Testing: $$SERVICE_URL/run$(NC)" && \
curl -s -X POST $$SERVICE_URL/run \
-H "Content-Type: application/json" \
-d '{"prompt": "create a simple bouncing ball animation"}' | jq .
test-local:
@echo "$(GREEN)Testing local health endpoint...$(NC)"
curl -s http://localhost:8000/health | jq .
test-local-run:
@echo "$(GREEN)Testing local /run endpoint...$(NC)"
curl -s -X POST http://localhost:8000/run \
-H "Content-Type: application/json" \
-d '{"prompt": "create a bouncing ball"}' | jq .
benchmark:
@echo "$(GREEN)Running load test (10 requests, 2 concurrent)...$(NC)"
@SERVICE_URL=$$(cd terraform && terraform output -raw service_url 2>/dev/null) && \
hey -n 10 -c 2 $$SERVICE_URL/health
# ============================================
# CLEANUP
# ============================================
clean:
@echo "$(YELLOW)Cleaning local artifacts...$(NC)"
find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
find . -type f -name "*.pyc" -delete 2>/dev/null || true
find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
rm -rf .mypy_cache 2>/dev/null || true
clean-all: clean docker-clean
@echo "$(GREEN)All cleaned!$(NC)"
# ============================================
# HELP
# ============================================
help:
@echo ''
@echo '$(GREEN)AnimAI - Available Commands$(NC)'
@echo ''
@echo '$(YELLOW)Local Development:$(NC)'
@echo ' make install - Install Python dependencies'
@echo ' make dev - Run API locally with hot-reload'
@echo ''
@echo '$(YELLOW)Docker:$(NC)'
@echo ' make docker-build - Build Docker image'
@echo ' make docker-run - Run Docker container locally'
@echo ' make docker-stop - Stop running container'
@echo ' make docker-logs - View container logs'
@echo ''
@echo '$(YELLOW)Terraform/GCP:$(NC)'
@echo ' make tf-init - Initialize Terraform'
@echo ' make tf-plan - Plan Terraform changes'
@echo ' make tf-apply - Apply Terraform changes'
@echo ' make tf-destroy - Destroy all infrastructure'
@echo ' make tf-unlock - Remove stale Terraform locks'
@echo ''
@echo '$(YELLOW)Deployment:$(NC)'
@echo ' make deploy - Full deploy (build + push + update)'
@echo ' make push - Push image to Artifact Registry'
@echo ' make update - Update Cloud Run service'
@echo ' make url - Get service URL'
@echo ''
@echo '$(YELLOW)Monitoring:$(NC)'
@echo ' make logs - View Cloud Run logs'
@echo ' make logs-follow - Stream Cloud Run logs'
@echo ' make describe - Describe Cloud Run service'
@echo ''
@echo '$(YELLOW)Testing:$(NC)'
@echo ' make health - Test health endpoint (deployed)'
@echo ' make test-run - Test /run endpoint (deployed)'
@echo ' make test-local - Test local health endpoint'
@echo ' make benchmark - Run load test with hey'
@echo ' make test - Run unit tests'
@echo ' make lint - Run linters'
@echo ' make format - Run code formatters'
@echo ''
@echo '$(YELLOW)Cleanup:$(NC)'
@echo ' make clean - Clean local artifacts'
@echo ' make clean-all - Clean everything including Docker'