-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
248 lines (209 loc) · 8.47 KB
/
Copy pathMakefile
File metadata and controls
248 lines (209 loc) · 8.47 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
SHELL := bash
-include .env
export
.PHONY: init dev rebuild infra up down prod logs test format typecheck lint migration migrate reset-db clean shell db-shell ecr-login ecr-build ecr-push security security-bandit security-audit sonar-up sonar-down sonar-coverage sonar-scan sonar help
# ========================================
# Compose 파일 조합
# ========================================
LOCAL_COMPOSE = docker compose -f docker-compose.yml -f docker-compose.local.yml
# ========================================
# ECR 설정
# ========================================
AWS_ACCOUNT_ID := $(shell aws sts get-caller-identity --query Account --output text)
ECR_REGISTRY = $(AWS_ACCOUNT_ID).dkr.ecr.ap-northeast-2.amazonaws.com
ECR_REPO = mindlog-ai
# ========================================
# 환경 설정
# ========================================
# 프로젝트 초기화 (로컬)
init:
@echo "Initializing MindLog AI project..."
@if [ ! -f .env.local ]; then cp .env.example .env.local; echo ".env.local created. Please edit it with your API keys."; fi
cp .env.local .env
$(LOCAL_COMPOSE) up -d
@echo "Waiting for services to be healthy..."
@sleep 10
$(LOCAL_COMPOSE) exec api alembic upgrade head
@echo "Setup complete!"
@echo "API Docs: http://localhost:8000/docs"
@echo "Health Check: http://localhost:8000/health"
# 로컬: 인프라(db, redis) + 앱 함께 실행 (포그라운드)
dev:
$(LOCAL_COMPOSE) up
# 의존성 변경 시 캐시 없이 완전 재빌드 후 실행
rebuild:
$(LOCAL_COMPOSE) build --no-cache api
$(LOCAL_COMPOSE) up
# 로컬: 인프라만 실행 (앱은 IDE에서 직접 실행할 때)
infra:
cp .env.local .env
docker compose -f docker-compose.local.yml up -d
# 로컬: 백그라운드 실행
up:
cp .env.local .env
$(LOCAL_COMPOSE) up -d
# 서비스 중지
down:
$(LOCAL_COMPOSE) down
# 프로덕션: 앱만 실행 (인프라는 AWS RDS/ElastiCache)
prod:
cp .env.prod .env
docker compose up
# ========================================
# 개발 유틸리티
# ========================================
# 로그 확인
logs:
$(LOCAL_COMPOSE) logs -f api
# 전체 테스트 (커버리지 포함)
test:
$(LOCAL_COMPOSE) exec api pytest tests/ -v --cov=app --cov-report=term-missing --cov-report=xml:coverage.xml
# 코드 포맷팅 (black + isort)
format:
$(LOCAL_COMPOSE) exec api black app/ tests/
$(LOCAL_COMPOSE) exec api isort app/ tests/
# 타입 체크
typecheck:
$(LOCAL_COMPOSE) exec api mypy app/
# Lint (ruff: runs on local venv, not container)
lint:
@pip show ruff > /dev/null 2>&1 || pip install ruff -q
ruff check app/ tests/
ruff format --check app/ tests/
# ========================================
# Security Analysis
# ========================================
# Run all security checks
security: security-bandit security-audit
@echo ""
@echo "[OK] Security scan complete"
# Bandit: Python static security analysis (runs on local venv, not container)
security-bandit:
@echo "[1/2] Running Bandit security scan (app/)..."
@pip show bandit > /dev/null 2>&1 || pip install bandit[toml] -q
bandit -r app/ -f txt -ll -ii
@echo ""
# pip-audit: dependency CVE vulnerability check (runs on local venv, not container)
security-audit:
@echo "[2/2] Running pip-audit for dependency CVEs..."
@pip show pip-audit > /dev/null 2>&1 || pip install pip-audit -q
pip-audit -r requirements.txt
@echo ""
# DB 마이그레이션 생성
migration:
@if [ -z "$(msg)" ]; then echo "Usage: make migration msg='your message'"; exit 1; fi
$(LOCAL_COMPOSE) exec api alembic revision --autogenerate -m "$(msg)"
# DB 마이그레이션 적용
migrate:
$(LOCAL_COMPOSE) exec api alembic upgrade head
# DB 초기화 (⚠️ 모든 데이터 삭제)
reset-db:
@echo "⚠️ This will delete all data. Are you sure? [y/N] " && read ans && [ $${ans:-N} = y ]
$(LOCAL_COMPOSE) down -v
$(LOCAL_COMPOSE) up -d db
@sleep 5
$(LOCAL_COMPOSE) up -d api
@sleep 5
$(LOCAL_COMPOSE) exec api alembic upgrade head
@echo "Database reset complete"
# 전체 정리 (볼륨 포함)
clean:
$(LOCAL_COMPOSE) down -v
rm -rf __pycache__ .pytest_cache .coverage htmlcov/ .mypy_cache/
find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
# API 컨테이너 쉘 접속
shell:
$(LOCAL_COMPOSE) exec api bash
# DB 쉘 접속
db-shell:
$(LOCAL_COMPOSE) exec db psql -U mindlog -d mindlog_dev
# ========================================
# SonarQube 코드 분석
# ========================================
SONAR_COMPOSE = docker compose -f docker-compose.sonar.yml
# SonarQube + DB 시작 (처음 기동 시 1-2분 소요)
sonar-up:
@echo "SonarQube 기동 중... (최초 시작 시 1-2분 소요)"
$(SONAR_COMPOSE) up -d sonarqube sonar-db
@echo "SonarQube UI: http://localhost:9000"
@echo "초기 로그인: admin / admin (이후 비밀번호 변경 필요)"
@echo "프로젝트 토큰 발급 후 .env에 SONAR_TOKEN=<token> 추가하세요"
# SonarQube 중지 (분석 완료 후 메모리 절약)
sonar-down:
$(SONAR_COMPOSE) down
@echo "SonarQube 종료 완료"
# 커버리지 리포트 생성 (sonar-scan 전 실행)
sonar-coverage:
@echo "테스트 커버리지 리포트 생성 중..."
$(LOCAL_COMPOSE) exec api pytest tests/ \
--cov=app \
--cov-report=xml:coverage.xml \
--cov-report=term-missing \
-q
@echo "coverage.xml 생성 완료"
# SonarQube 분석 실행 (sonar-up + sonar-coverage 먼저 실행 필요)
sonar-scan:
ifndef SONAR_TOKEN
$(error SONAR_TOKEN is not set. Go to SonarQube UI -> My Account -> Security -> Generate Token, then add SONAR_TOKEN=<token> to .env)
endif
@echo "SonarQube 코드 분석 시작..."
$(SONAR_COMPOSE) run --rm sonar-scanner
@echo "분석 완료 → http://localhost:9000/dashboard?id=mindlog-ai"
# 원스톱: 커버리지 생성 + SonarQube 분석 (sonar-up은 별도 실행 필요)
sonar: sonar-coverage sonar-scan
# ========================================
# ECR 배포
# ========================================
# ECR 로그인
ecr-login:
aws ecr get-login-password --region ap-northeast-2 | \
docker login --username AWS --password-stdin $(ECR_REGISTRY)
# linux/amd64 이미지 빌드 (Apple Silicon Mac 포함 모든 환경)
ecr-build:
docker build --platform linux/amd64 -t $(ECR_REPO):latest .
# 빌드 + 태그 + ECR 푸시 (최초: ECR_REGISTRY 변수에 account-id 입력 필요)
ecr-push: ecr-login ecr-build
docker tag $(ECR_REPO):latest $(ECR_REGISTRY)/$(ECR_REPO):latest
docker push $(ECR_REGISTRY)/$(ECR_REPO):latest
# 도움말
help:
@echo "MindLog AI - Available Commands:"
@echo ""
@echo " [환경 설정]"
@echo " make init - 프로젝트 초기화 및 로컬 실행"
@echo " make dev - 로컬: 인프라 + 앱 실행 (포그라운드)"
@echo " make infra - 로컬: 인프라만 실행 (IDE에서 앱 직접 실행 시)"
@echo " make up - 로컬: 서비스 백그라운드 실행"
@echo " make down - 서비스 중지"
@echo " make prod - 프로덕션: 앱만 실행 (AWS RDS/ElastiCache 사용)"
@echo ""
@echo " [개발 도구]"
@echo " make logs - API 로그 확인"
@echo " make test - 테스트 실행 (커버리지 포함)"
@echo " make format - 코드 포맷팅 (black + isort)"
@echo " make lint - 코드 린트 (ruff)"
@echo " make typecheck - 타입 체크 (mypy)"
@echo " make migration - DB 마이그레이션 생성 (msg='메시지')"
@echo " make migrate - DB 마이그레이션 적용"
@echo " make reset-db - DB 초기화 (데이터 삭제)"
@echo " make clean - 전체 정리"
@echo " make shell - API 컨테이너 쉘 접속"
@echo " make db-shell - PostgreSQL 쉘 접속"
@echo ""
@echo " [보안 분석]"
@echo " make security - 전체 보안 검사 (bandit + pip-audit)"
@echo " make security-bandit - Python 코드 취약점 정적 분석"
@echo " make security-audit - 의존성 CVE 취약점 검사"
@echo ""
@echo " [SonarQube]"
@echo " make sonar-up - SonarQube 서버 시작 (http://localhost:9000)"
@echo " make sonar-down - SonarQube 서버 중지"
@echo " make sonar-coverage - 커버리지 리포트 생성"
@echo " make sonar-scan - 코드 분석 실행 (SONAR_TOKEN 필요)"
@echo " make sonar - 커버리지 생성 + 분석 원스톱"
@echo ""
@echo " [ECR 배포]"
@echo " make ecr-login - ECR 도커 로그인"
@echo " make ecr-build - linux/amd64 이미지 빌드"
@echo " make ecr-push - 빌드 + ECR 푸시 (ECR_REGISTRY 설정 필요)"
@echo " make help - 이 도움말 표시"