-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathMakefile
More file actions
448 lines (382 loc) · 15.9 KB
/
Makefile
File metadata and controls
448 lines (382 loc) · 15.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
REQUIRED_BUILD_BINS := uv
SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c
# Project variables
PACKAGE_NAME = cedarpolicyplugin
PROJECT_NAME = cedarpolicyplugin
TARGET ?= cedarpolicyplugin
# Virtual-environment variables
VENV_DIR ?= $(CURDIR)/.venv
# =============================================================================
# Linters
# =============================================================================
black:
@echo "🎨 black $(TARGET)..." && $(VENV_DIR)/bin/black -l 200 $(TARGET)
black-check:
@echo "🎨 black --check $(TARGET)..." && $(VENV_DIR)/bin/black -l 200 --check --diff $(TARGET)
ruff:
@echo "⚡ ruff $(TARGET)..." && $(VENV_DIR)/bin/ruff check $(TARGET) && $(VENV_DIR)/bin/ruff format $(TARGET)
ruff-check:
@echo "⚡ ruff check $(TARGET)..." && $(VENV_DIR)/bin/ruff check $(TARGET)
ruff-fix:
@echo "⚡ ruff check --fix $(TARGET)..." && $(VENV_DIR)/bin/ruff check --fix $(TARGET)
ruff-format:
@echo "⚡ ruff format $(TARGET)..." && $(VENV_DIR)/bin/ruff format $(TARGET)
# =============================================================================
# Container runtime configuration and operations
# =============================================================================
# Container resource limits
CONTAINER_MEMORY = 2048m
CONTAINER_CPUS = 2
# Auto-detect container runtime if not specified - DEFAULT TO DOCKER
CONTAINER_RUNTIME ?= $(shell command -v docker >/dev/null 2>&1 && echo docker || echo podman)
# Alternative: Always default to docker unless explicitly overridden
# CONTAINER_RUNTIME ?= docker
# Container port
CONTAINER_PORT ?= 8000
CONTAINER_INTERNAL_PORT ?= 8000
print-runtime:
@echo Using container runtime: $(CONTAINER_RUNTIME)
# Base image name (without any prefix)
IMAGE_BASE ?= mcpgateway/$(PROJECT_NAME)
IMAGE_TAG ?= latest
# Handle runtime-specific image naming
ifeq ($(CONTAINER_RUNTIME),podman)
# Podman adds localhost/ prefix for local builds
IMAGE_LOCAL := localhost/$(IMAGE_BASE):$(IMAGE_TAG)
IMAGE_LOCAL_DEV := localhost/$(IMAGE_BASE)-dev:$(IMAGE_TAG)
IMAGE_PUSH := $(IMAGE_BASE):$(IMAGE_TAG)
else
# Docker doesn't add prefix
IMAGE_LOCAL := $(IMAGE_BASE):$(IMAGE_TAG)
IMAGE_LOCAL_DEV := $(IMAGE_BASE)-dev:$(IMAGE_TAG)
IMAGE_PUSH := $(IMAGE_BASE):$(IMAGE_TAG)
endif
print-image:
@echo "🐳 Container Runtime: $(CONTAINER_RUNTIME)"
@echo "Using image: $(IMAGE_LOCAL)"
@echo "Development image: $(IMAGE_LOCAL_DEV)"
@echo "Push image: $(IMAGE_PUSH)"
# Function to get the actual image name as it appears in image list
define get_image_name
$(shell $(CONTAINER_RUNTIME) images --format "{{.Repository}}:{{.Tag}}" | grep -E "(localhost/)?$(IMAGE_BASE):$(IMAGE_TAG)" | head -1)
endef
# Function to normalize image name for operations
define normalize_image
$(if $(findstring localhost/,$(1)),$(1),$(if $(filter podman,$(CONTAINER_RUNTIME)),localhost/$(1),$(1)))
endef
# Containerfile to use (can be overridden)
#CONTAINER_FILE ?= Containerfile
CONTAINER_FILE ?= $(shell [ -f "Containerfile" ] && echo "Containerfile" || echo "Dockerfile")
# Define COMMA for the conditional Z flag
COMMA := ,
container-info:
@echo "🐳 Container Runtime Configuration"
@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
@echo "Runtime: $(CONTAINER_RUNTIME)"
@echo "Base Image: $(IMAGE_BASE)"
@echo "Tag: $(IMAGE_TAG)"
@echo "Local Image: $(IMAGE_LOCAL)"
@echo "Push Image: $(IMAGE_PUSH)"
@echo "Actual Image: $(call get_image_name)"
@echo "Container File: $(CONTAINER_FILE)"
@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# Auto-detect platform based on uname
PLATFORM ?= linux/$(shell uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
container-build:
@echo "🔨 Building with $(CONTAINER_RUNTIME) for platform $(PLATFORM)..."
$(CONTAINER_RUNTIME) build \
--platform=$(PLATFORM) \
-f $(CONTAINER_FILE) \
--tag $(IMAGE_BASE):$(IMAGE_TAG) \
.
@echo "✅ Built image: $(call get_image_name)"
$(CONTAINER_RUNTIME) images $(IMAGE_BASE):$(IMAGE_TAG)
container-run: container-check-image
@echo "🚀 Running with $(CONTAINER_RUNTIME)..."
-$(CONTAINER_RUNTIME) stop $(PROJECT_NAME) 2>/dev/null || true
-$(CONTAINER_RUNTIME) rm $(PROJECT_NAME) 2>/dev/null || true
$(CONTAINER_RUNTIME) run --name $(PROJECT_NAME) \
--env-file=.env \
-p $(CONTAINER_PORT):$(CONTAINER_INTERNAL_PORT) \
--restart=always \
--memory=$(CONTAINER_MEMORY) --cpus=$(CONTAINER_CPUS) \
--health-cmd="curl --fail http://localhost:$(CONTAINER_INTERNAL_PORT)/health || exit 1" \
--health-interval=1m --health-retries=3 \
--health-start-period=30s --health-timeout=10s \
-d $(call get_image_name)
@sleep 2
@echo "✅ Container started"
@echo "🔍 Health check status:"
@$(CONTAINER_RUNTIME) inspect $(PROJECT_NAME) --format='{{.State.Health.Status}}' 2>/dev/null || echo "No health check configured"
container-run-host: container-check-image
@echo "🚀 Running with $(CONTAINER_RUNTIME)..."
-$(CONTAINER_RUNTIME) stop $(PROJECT_NAME) 2>/dev/null || true
-$(CONTAINER_RUNTIME) rm $(PROJECT_NAME) 2>/dev/null || true
$(CONTAINER_RUNTIME) run --name $(PROJECT_NAME) \
--env-file=.env \
--network=host \
-p $(CONTAINER_PORT):$(CONTAINER_INTERNAL_PORT) \
--restart=always \
--memory=$(CONTAINER_MEMORY) --cpus=$(CONTAINER_CPUS) \
--health-cmd="curl --fail http://localhost:$(CONTAINER_INTERNAL_PORT)/health || exit 1" \
--health-interval=1m --health-retries=3 \
--health-start-period=30s --health-timeout=10s \
-d $(call get_image_name)
@sleep 2
@echo "✅ Container started"
@echo "🔍 Health check status:"
@$(CONTAINER_RUNTIME) inspect $(PROJECT_NAME) --format='{{.State.Health.Status}}' 2>/dev/null || echo "No health check configured"
container-push: container-check-image
@echo "📤 Preparing to push image..."
@# For Podman, we need to remove localhost/ prefix for push
@if [ "$(CONTAINER_RUNTIME)" = "podman" ]; then \
actual_image=$$($(CONTAINER_RUNTIME) images --format "{{.Repository}}:{{.Tag}}" | grep -E "$(IMAGE_BASE):$(IMAGE_TAG)" | head -1); \
if echo "$$actual_image" | grep -q "^localhost/"; then \
echo "🏷️ Tagging for push (removing localhost/ prefix)..."; \
$(CONTAINER_RUNTIME) tag "$$actual_image" $(IMAGE_PUSH); \
fi; \
fi
$(CONTAINER_RUNTIME) push $(IMAGE_PUSH)
@echo "✅ Pushed: $(IMAGE_PUSH)"
container-check-image:
@echo "🔍 Checking for image..."
@if [ "$(CONTAINER_RUNTIME)" = "podman" ]; then \
if ! $(CONTAINER_RUNTIME) image exists $(IMAGE_LOCAL) 2>/dev/null && \
! $(CONTAINER_RUNTIME) image exists $(IMAGE_BASE):$(IMAGE_TAG) 2>/dev/null; then \
echo "❌ Image not found: $(IMAGE_LOCAL)"; \
echo "💡 Run 'make container-build' first"; \
exit 1; \
fi; \
else \
if ! $(CONTAINER_RUNTIME) images -q $(IMAGE_LOCAL) 2>/dev/null | grep -q . && \
! $(CONTAINER_RUNTIME) images -q $(IMAGE_BASE):$(IMAGE_TAG) 2>/dev/null | grep -q .; then \
echo "❌ Image not found: $(IMAGE_LOCAL)"; \
echo "💡 Run 'make container-build' first"; \
exit 1; \
fi; \
fi
@echo "✅ Image found"
container-stop:
@echo "🛑 Stopping container..."
-$(CONTAINER_RUNTIME) stop $(PROJECT_NAME) 2>/dev/null || true
-$(CONTAINER_RUNTIME) rm $(PROJECT_NAME) 2>/dev/null || true
@echo "✅ Container stopped and removed"
container-logs:
@echo "📜 Streaming logs (Ctrl+C to exit)..."
$(CONTAINER_RUNTIME) logs -f $(PROJECT_NAME)
container-shell:
@echo "🔧 Opening shell in container..."
@if ! $(CONTAINER_RUNTIME) ps -q -f name=$(PROJECT_NAME) | grep -q .; then \
echo "❌ Container $(PROJECT_NAME) is not running"; \
echo "💡 Run 'make container-run' first"; \
exit 1; \
fi
@$(CONTAINER_RUNTIME) exec -it $(PROJECT_NAME) /bin/bash 2>/dev/null || \
$(CONTAINER_RUNTIME) exec -it $(PROJECT_NAME) /bin/sh
container-health:
@echo "🏥 Checking container health..."
@if ! $(CONTAINER_RUNTIME) ps -q -f name=$(PROJECT_NAME) | grep -q .; then \
echo "❌ Container $(PROJECT_NAME) is not running"; \
exit 1; \
fi
@echo "Status: $$($(CONTAINER_RUNTIME) inspect $(PROJECT_NAME) --format='{{.State.Health.Status}}' 2>/dev/null || echo 'No health check')"
@echo "Logs:"
@$(CONTAINER_RUNTIME) inspect $(PROJECT_NAME) --format='{{range .State.Health.Log}}{{.Output}}{{end}}' 2>/dev/null || true
container-build-multi:
@echo "🔨 Building multi-architecture image..."
@if [ "$(CONTAINER_RUNTIME)" = "docker" ]; then \
if ! docker buildx inspect $(PROJECT_NAME)-builder >/dev/null 2>&1; then \
echo "📦 Creating buildx builder..."; \
docker buildx create --name $(PROJECT_NAME)-builder; \
fi; \
docker buildx use $(PROJECT_NAME)-builder; \
docker buildx build \
--platform=linux/amd64,linux/arm64 \
-f $(CONTAINER_FILE) \
--tag $(IMAGE_BASE):$(IMAGE_TAG) \
--push \
.; \
elif [ "$(CONTAINER_RUNTIME)" = "podman" ]; then \
echo "📦 Building manifest with Podman..."; \
$(CONTAINER_RUNTIME) build --platform=linux/amd64,linux/arm64 \
-f $(CONTAINER_FILE) \
--manifest $(IMAGE_BASE):$(IMAGE_TAG) \
.; \
echo "💡 To push: podman manifest push $(IMAGE_BASE):$(IMAGE_TAG)"; \
else \
echo "❌ Multi-arch builds require Docker buildx or Podman"; \
exit 1; \
fi
# Helper targets for debugging image issues
image-list:
@echo "📋 Images matching $(IMAGE_BASE):"
@$(CONTAINER_RUNTIME) images --format "table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.Created}}\t{{.Size}}" | \
grep -E "(IMAGE|$(IMAGE_BASE))" || echo "No matching images found"
image-clean:
@echo "🧹 Removing all $(IMAGE_BASE) images..."
@$(CONTAINER_RUNTIME) images --format "{{.Repository}}:{{.Tag}}" | \
grep -E "(localhost/)?$(IMAGE_BASE)" | \
xargs $(XARGS_FLAGS) $(CONTAINER_RUNTIME) rmi -f 2>/dev/null
@echo "✅ Images cleaned"
# Fix image naming issues
image-retag:
@echo "🏷️ Retagging images for consistency..."
@if [ "$(CONTAINER_RUNTIME)" = "podman" ]; then \
if $(CONTAINER_RUNTIME) image exists $(IMAGE_BASE):$(IMAGE_TAG) 2>/dev/null; then \
$(CONTAINER_RUNTIME) tag $(IMAGE_BASE):$(IMAGE_TAG) $(IMAGE_LOCAL) 2>/dev/null || true; \
fi; \
else \
if $(CONTAINER_RUNTIME) images -q $(IMAGE_LOCAL) 2>/dev/null | grep -q .; then \
$(CONTAINER_RUNTIME) tag $(IMAGE_LOCAL) $(IMAGE_BASE):$(IMAGE_TAG) 2>/dev/null || true; \
fi; \
fi
@echo "✅ Images retagged" # This always shows success
# Runtime switching helpers
use-docker:
@echo "export CONTAINER_RUNTIME=docker"
@echo "💡 Run: export CONTAINER_RUNTIME=docker"
use-podman:
@echo "export CONTAINER_RUNTIME=podman"
@echo "💡 Run: export CONTAINER_RUNTIME=podman"
show-runtime:
@echo "Current runtime: $(CONTAINER_RUNTIME)"
@echo "Detected from: $$(command -v $(CONTAINER_RUNTIME) || echo 'not found')" # Added
@echo "To switch: make use-docker or make use-podman"
# =============================================================================
# Targets
# =============================================================================
.PHONY: venv
venv:
@rm -Rf "$(VENV_DIR)"
@mkdir -p "$(VENV_DIR)"
@python3 -m venv "$(VENV_DIR)"
@/bin/bash -c "source $(VENV_DIR)/bin/activate && python3 -m pip install --upgrade pip setuptools pdm uv"
@echo -e "✅ Virtual env created.\n💡 Enter it with:\n . $(VENV_DIR)/bin/activate\n"
.PHONY: install
install: venv
$(foreach bin,$(REQUIRED_BUILD_BINS), $(if $(shell command -v $(bin) 2> /dev/null),,$(error Couldn't find `$(bin)`)))
@/bin/bash -c "source $(VENV_DIR)/bin/activate && python3 -m uv pip install ."
.PHONY: install-dev
install-dev: venv
$(foreach bin,$(REQUIRED_BUILD_BINS), $(if $(shell command -v $(bin) 2> /dev/null),,$(error Couldn't find `$(bin)`)))
@/bin/bash -c "source $(VENV_DIR)/bin/activate && python3 -m uv pip install -e .[dev]"
.PHONY: install-editable
install-editable: venv
$(foreach bin,$(REQUIRED_BUILD_BINS), $(if $(shell command -v $(bin) 2> /dev/null),,$(error Couldn't find `$(bin)`)))
@/bin/bash -c "source $(VENV_DIR)/bin/activate && python3 -m uv pip install -e .[dev]"
.PHONY: uninstall
uninstall:
pip uninstall $(PACKAGE_NAME)
.PHONY: dist
dist: clean ## Build wheel + sdist into ./dist
@test -d "$(VENV_DIR)" || $(MAKE) --no-print-directory venv
@/bin/bash -eu -c "\
source $(VENV_DIR)/bin/activate && \
python3 -m pip install --quiet --upgrade pip build && \
python3 -m build"
@echo '🛠 Wheel & sdist written to ./dist'
.PHONY: wheel
wheel: ## Build wheel only
@test -d "$(VENV_DIR)" || $(MAKE) --no-print-directory venv
@/bin/bash -eu -c "\
source $(VENV_DIR)/bin/activate && \
python3 -m pip install --quiet --upgrade pip build && \
python3 -m build -w"
@echo '🛠 Wheel written to ./dist'
.PHONY: sdist
sdist: ## Build source distribution only
@test -d "$(VENV_DIR)" || $(MAKE) --no-print-directory venv
@/bin/bash -eu -c "\
source $(VENV_DIR)/bin/activate && \
python3 -m pip install --quiet --upgrade pip build && \
python3 -m build -s"
@echo '🛠 Source distribution written to ./dist'
.PHONY: verify
verify: dist ## Build, run metadata & manifest checks
@/bin/bash -c "source $(VENV_DIR)/bin/activate && \
twine check dist/* && \
check-manifest && \
pyroma -d ."
@echo "✅ Package verified - ready to publish."
.PHONY: lint-fix
lint-fix:
@# Handle file arguments
@target_file="$(word 2,$(MAKECMDGOALS))"; \
if [ -n "$$target_file" ] && [ "$$target_file" != "" ]; then \
actual_target="$$target_file"; \
else \
actual_target="$(TARGET)"; \
fi; \
for target in $$(echo $$actual_target); do \
if [ ! -e "$$target" ]; then \
echo "❌ File/directory not found: $$target"; \
exit 1; \
fi; \
done; \
echo "🔧 Fixing lint issues in $$actual_target..."; \
$(MAKE) --no-print-directory black TARGET="$$actual_target"; \
$(MAKE) --no-print-directory ruff-fix TARGET="$$actual_target"
.PHONY: lint-check
lint-check:
@# Handle file arguments
@target_file="$(word 2,$(MAKECMDGOALS))"; \
if [ -n "$$target_file" ] && [ "$$target_file" != "" ]; then \
actual_target="$$target_file"; \
else \
actual_target="$(TARGET)"; \
fi; \
for target in $$(echo $$actual_target); do \
if [ ! -e "$$target" ]; then \
echo "❌ File/directory not found: $$target"; \
exit 1; \
fi; \
done; \
echo "🔧 Fixing lint issues in $$actual_target..."; \
$(MAKE) --no-print-directory black-check TARGET="$$actual_target"; \
$(MAKE) --no-print-directory ruff-check TARGET="$$actual_target"
.PHONY: lock
lock:
$(foreach bin,$(REQUIRED_BUILD_BINS), $(if $(shell command -v $(bin) 2> /dev/null),,$(error Couldn't find `$(bin)`. Please run `make init`)))
uv lock
.PHONY: test
test:
pytest tests
.PHONY: serve
serve:
@echo "Implement me."
.PHONY: build
build:
@$(MAKE) container-build
.PHONY: start
start:
@$(MAKE) container-run
.PHONY: stop
stop:
@$(MAKE) container-stop
.PHONY: clean
clean:
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete
rm -rf *.egg-info .pytest_cache tests/.pytest_cache build dist .ruff_cache .coverage
.PHONY: help
help:
@echo "This Makefile is offered for convenience."
@echo ""
@echo "The following are the valid targets for this Makefile:"
@echo "...install Install package from sources"
@echo "...install-dev Install package from sources with dev packages"
@echo "...install-editable Install package from sources in editabled mode"
@echo "...uninstall Uninstall package"
@echo "...dist Clean-build wheel *and* sdist into ./dist"
@echo "...wheel Build wheel only"
@echo "...sdist Build source distribution only"
@echo "...verify Build + twine + check-manifest + pyroma (no upload)"
@echo "...serve Start API server locally"
@echo "...build Build API server container image"
@echo "...start Start the API server container"
@echo "...start Stop the API server container"
@echo "...lock Lock dependencies"
@echo "...lint-fix Check and fix lint errors"
@echo "...lint-check Check for lint errors"
@echo "...test Run all tests"
@echo "...clean Remove all artifacts and builds"