-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
167 lines (154 loc) · 6.34 KB
/
Makefile
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
# Colors for better visibility
GREEN := \033[0;32m
RED := \033[0;31m
YELLOW := \033[1;33m
NC := \033[0m # No Color
.PHONY: help init build run stop logs test integration-test clean integration-test-gateway test-coverage
help: ## Show this help message
@echo "CodeQuery Core - Quick Start Guide"
@echo ""
@echo "Prerequisites:"
@echo " - Docker"
@echo " - Make"
@echo " - curl"
@echo " - ngrok account (free tier is sufficient)"
@echo " Get your authtoken at https://dashboard.ngrok.com/get-started/your-authtoken"
@echo ""
@echo "Usage:"
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " make %-20s%s\n", $$1, $$2}' $(MAKEFILE_LIST)
init: ## Setup environment and configure API key
@if [ -f .env ]; then \
echo "$(YELLOW)Warning: .env file already exists.$(NC)"; \
read -p "Do you want to overwrite it? [y/N] " answer; \
if [ "$$answer" != "y" ] && [ "$$answer" != "Y" ]; then \
echo "Keeping existing .env file."; \
exit 0; \
fi; \
fi; \
cp template.env .env; \
echo "$(GREEN)Created .env file from template.$(NC)"; \
echo "$(GREEN)Next steps:$(NC)"; \
echo "1. Get your API key by running:"; \
echo " curl -X POST https://codequery.dev/api-keys/generate"; \
echo "2. Add your API key to .env as API_KEY=your_key"; \
echo "3. Set PROJECT_PATH in .env to the root of the project you want CodeQuery to help you with"; \
echo " Example: PROJECT_PATH=/path/to/your/project"; \
echo "4. Add your ngrok authtoken to .env as NGROK_AUTHTOKEN=your_token"; \
echo "5. Run 'make build'"
clean: ## Remove .env file and start fresh
@if [ -f .env ]; then \
echo "$(YELLOW)Warning: This will remove your .env file.$(NC)"; \
read -p "Are you sure? [y/N] " answer; \
if [ "$$answer" = "y" ] || [ "$$answer" = "Y" ]; then \
rm .env; \
echo "$(GREEN).env file removed. Run 'make init' to start fresh.$(NC)"; \
else \
echo "Operation cancelled."; \
fi; \
else \
echo "No .env file found."; \
fi
# Load environment variables after init
ifneq ($(wildcard .env),)
include .env
export
endif
build: ## Build the Docker image
@if [ ! -f .env ]; then \
echo "$(RED)Error: .env file not found. Run 'make init' first.$(NC)"; \
exit 1; \
fi
@if [ -z "$(NGROK_AUTHTOKEN)" ]; then \
echo "$(RED)Error: NGROK_AUTHTOKEN is not set in .env file$(NC)"; \
echo "Please edit .env and add your ngrok authtoken before building."; \
exit 1; \
fi
@if [ -z "$(API_KEY)" ]; then \
echo "$(RED)Error: API_KEY is not set in .env file$(NC)"; \
echo "Please edit .env and add your API key before building."; \
echo "You can generate one with: curl -X POST https://codequery.dev/api-keys/generate"; \
exit 1; \
fi
@echo "Building Docker image..."
docker build -t codequery_core --build-arg NGROK_AUTHTOKEN=$(NGROK_AUTHTOKEN) .
@echo "$(GREEN)Build completed.$(NC)"
run: ## Run the Core container
@echo "Starting Core container..."
@if lsof -i :5001 >/dev/null 2>&1; then \
kill -9 $$(lsof -t -i :5001); \
echo "Released port 5001"; \
fi
@docker run --rm -d -p 5001:5001 -p 4040:4040 --name codequery_core -v "$(shell pwd):/app" --env-file .env codequery_core
@echo "$(YELLOW)Waiting for ngrok and Gateway registration...$(NC)"
@for i in $$(seq 1 30); do \
sleep 1; \
if docker logs codequery_core 2>&1 | grep -q "ngrok tunnel is running and synchronized\|ngrok is running:"; then \
NGROK_URL=$$(docker logs codequery_core 2>&1 | grep "ngrok is running:" | grep -o "https://[^[:space:]]*"); \
echo "$(GREEN)✓ ngrok tunnel is running$(NC)"; \
echo "$(GREEN)✓ ngrok URL: $$NGROK_URL$(NC)"; \
echo "$(GREEN)✓ API Key: $$API_KEY$(NC)"; \
echo "\nContainer is ready! Use 'make logs' to view detailed logs."; \
exit 0; \
fi; \
if [ $$i -eq 30 ]; then \
echo "$(RED)Timeout waiting for ngrok setup. Check 'make logs' for details.$(NC)"; \
exit 1; \
fi; \
echo -n "."; \
done
stop: ## Stop the Core container
@echo "Stopping Core container..."
@docker stop codequery_core 2>/dev/null || true
@docker rm codequery_core 2>/dev/null || true
@echo "$(GREEN)Container stopped and removed.$(NC)"
logs: ## View container logs
docker logs -f codequery_core
test: ## Run tests (excluding integration)
docker run --rm --env-file .env codequery_core pytest core/tests -k "not integration"
integration-test: ## Run integration tests
@echo "Running integration tests..."
@if lsof -i :5001 >/dev/null 2>&1; then \
kill -9 $$(lsof -t -i :5001); \
echo "Released port 5001"; \
fi
@if lsof -i :4040 >/dev/null 2>&1; then \
kill -9 $$(lsof -t -i :4040); \
echo "Released port 4040"; \
fi
@echo "Starting Core service..."
docker run --rm -d -p 5001:5001 -p 4040:4040 --name codequery_core -v "$(shell pwd):/app" --env-file .env codequery_core
@echo "Waiting for Core service to start..."
@sleep 5
@echo "Running integration test..."
docker exec codequery_core python core/tests/integration_test.py
@echo "Stopping Core service..."
docker stop codequery_core
integration-test-gateway: ## Run integration tests including Gateway interaction
@echo "Running Gateway integration tests..."
@# Ensure cleanup of any existing containers
@docker stop codequery_core 2>/dev/null || true
@docker rm codequery_core 2>/dev/null || true
@# Kill any processes using our ports
@echo "Cleaning up ports..."
@for port in 5001 4040; do \
pid=$$(sudo ss -lptn "sport = :$$port" | grep LISTEN | sed -E 's/.*pid=([0-9]+).*/\1/'); \
if [ ! -z "$$pid" ]; then \
echo "Killing process $$pid using port $$port"; \
sudo kill -9 $$pid 2>/dev/null || true; \
fi \
done
@# Start Core service with proper error handling
@echo "Starting Core service..."
@docker run --rm -d -p 5001:5001 -p 4040:4040 --name codequery_core -v "$(shell pwd):/app" --env-file .env codequery_core || \
(echo "Failed to start Core service" && exit 1)
@echo "Waiting for Core service to start..."
@sleep 5
@echo "Running Gateway integration test..."
@docker exec codequery_core python core/tests/integration_test_with_gateway.py || \
(docker stop codequery_core && echo "Integration test failed" && exit 1)
@echo "Stopping Core service..."
@docker stop codequery_core || true
test-coverage: ## Run tests locally (outside Docker) with coverage report
@echo "$(GREEN)Running tests with coverage...$(NC)"
python -m pytest core/tests/ --cov=core/src --cov-report=term-missing:skip-covered -v
@echo "$(GREEN)Coverage report generated. Only showing files with missing coverage.$(NC)"