generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMakefile
More file actions
482 lines (409 loc) · 15.8 KB
/
Makefile
File metadata and controls
482 lines (409 loc) · 15.8 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
.PHONY: \
bootstrap createPythonEnvironment installPythonRequirements \
createTypeScriptEnvironment installTypeScriptRequirements \
deploy destroy \
clean cleanTypeScript cleanPython cleanCfn cleanMisc \
help dockerCheck dockerLogin listStacks modelCheck buildNpmModules \
test test-coverage test-lambda test-mcp-workbench test-sdk test-rest-api test-sdk-integ test-integ test-rag-integ test-metadata-integ \
lock-poetry validate-deps
#################################################################################
# GLOBALS #
#################################################################################
PROJECT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
HEADLESS = false
DOCKER_CMD ?= $(or $(CDK_DOCKER),docker)
# Function to read config with fallback to base config and default value
# Usage: VAR := $(call get_config,property,default_value)
define get_config
$(shell test -f $(PROJECT_DIR)/config-custom.yaml && yq -r $(1) $(PROJECT_DIR)/config-custom.yaml 2>/dev/null | grep -v '^null$$' || \
(test -f $(PROJECT_DIR)/config-base.yaml && yq -r $(1) $(PROJECT_DIR)/config-base.yaml 2>/dev/null | grep -v '^null$$') || \
echo "$(2)")
endef
# PROFILE (optional argument)
ifeq (${PROFILE},)
PROFILE := $(call get_config,.profile,)
ifeq ($(PROFILE),)
$(warning profile is not set in command line using PROFILE variable or config files, attempting deployment without this variable)
endif
endif
# DEPLOYMENT_NAME
ifeq (${DEPLOYMENT_NAME},)
DEPLOYMENT_NAME := $(call get_config,.deploymentName,prod)
endif
# ACCOUNT_NUMBER
ifeq (${ACCOUNT_NUMBER},)
ACCOUNT_NUMBER := $(call get_config,.accountNumber,)
endif
ifeq (${ACCOUNT_NUMBER},)
$(error accountNumber must be set in command line using ACCOUNT_NUMBER variable or config files)
endif
# REGION
ifeq (${REGION},)
REGION := $(call get_config,.region,)
endif
ifeq (${REGION},)
$(error region must be set in command line using REGION variable or config files)
endif
# PARTITION
ifeq (${PARTITION},)
PARTITION := $(call get_config,.partition,aws)
endif
# DOMAIN - used for the docker login
ifeq (${DOMAIN},)
ifeq ($(findstring isob,${REGION}),isob)
DOMAIN := sc2s.sgov.gov
else ifeq ($(findstring iso,${REGION}),iso)
DOMAIN := c2s.ic.gov
else
DOMAIN := amazonaws.com
endif
endif
# Arguments defined through config files
# APP_NAME
APP_NAME := $(call get_config,.appName,lisa)
# DEPLOYMENT_STAGE
DEPLOYMENT_STAGE := $(call get_config,.deploymentStage,prod)
# ACCOUNT_NUMBERS_ECR - AWS account numbers that need to be logged into with Docker CLI to use ECR
ACCOUNT_NUMBERS_ECR := $(shell test -f $(PROJECT_DIR)/config-custom.yaml && yq '.accountNumbersEcr[]' $(PROJECT_DIR)/config-custom.yaml 2>/dev/null || echo "")
# Append deployed account number to array for dockerLogin rule
ACCOUNT_NUMBERS_ECR := $(ACCOUNT_NUMBERS_ECR) $(ACCOUNT_NUMBER)
# STACK
ifeq ($(STACK),)
STACK := $(DEPLOYMENT_STAGE)/*
endif
ifneq ($(findstring $(DEPLOYMENT_STAGE),$(STACK)),$(DEPLOYMENT_STAGE))
override STACK := $(DEPLOYMENT_STAGE)/$(STACK)
endif
# MODEL_IDS - IDs of models to deploy
MODEL_IDS := $(shell test -f $(PROJECT_DIR)/config-custom.yaml && yq '.ecsModels[].modelName' $(PROJECT_DIR)/config-custom.yaml 2>/dev/null || echo "")
# MODEL_BUCKET - S3 bucket containing model artifacts
MODEL_BUCKET := $(call get_config,.s3BucketModels,)
# BASE_URL - Base URL for web UI assets based on domain name and deployment stage
DOMAIN_NAME := $(call get_config,.apiGatewayConfig.domainName,)
ifeq ($(DOMAIN_NAME),)
BASE_URL := /$(DEPLOYMENT_STAGE)/
else
BASE_URL := /
endif
#################################################################################
# COMMANDS #
#################################################################################
## Bootstrap AWS Account with CDK bootstrap
bootstrap:
@printf "Bootstrapping: $(ACCOUNT_NUMBER) | $(REGION) | $(PARTITION)\n"
ifdef PROFILE
@npx cdk bootstrap \
--profile $(PROFILE) \
aws://$(ACCOUNT_NUMBER)/$(REGION) \
--partition $(PARTITION) \
--cloudformation-execution-policies arn:$(PARTITION):iam::aws:policy/AdministratorAccess
else
@npx cdk bootstrap \
aws://$(ACCOUNT_NUMBER)/$(REGION) \
--partition $(PARTITION) \
--cloudformation-execution-policies arn:$(PARTITION):iam::aws:policy/AdministratorAccess
endif
## Set up Python interpreter environment to match LISA deployed version
createPythonEnvironment:
python3.13 -m venv .venv
@printf ">>> New virtual environment created. To activate run: 'source .venv/bin/activate'"
## Install Python dependencies for development
installPythonRequirements:
CC=/usr/bin/gcc10-gcc CXX=/usr/bin/gcc10-g++ pip3 install pip --upgrade
CC=/usr/bin/gcc10-gcc CXX=/usr/bin/gcc10-g++ pip3 install --prefer-binary -r requirements-dev.txt
CC=/usr/bin/gcc10-gcc CXX=/usr/bin/gcc10-g++ pip3 install -e lisa-sdk
CC=/usr/bin/gcc10-gcc CXX=/usr/bin/gcc10-g++ pip3 install -e lib/serve/mcp-workbench
## Set up TypeScript interpreter environment
createTypeScriptEnvironment:
npm init
## Install TypeScript dependencies for development
installTypeScriptRequirements:
npm install
install: installPythonRequirements installTypeScriptRequirements
## Make sure Docker is running
dockerCheck:
@cmd_output=$$($(DOCKER_CMD) ps); \
if [ $$? != 0 ]; then \
echo "Process $(DOCKER_CMD) is not running. Exiting..."; \
exit 1; \
fi; \
## Check if models are uploaded
modelCheck:
@access_token=""; \
for MODEL_ID in $(MODEL_IDS); do \
$(PROJECT_DIR)/scripts/check-for-models.sh -m $$MODEL_ID -s $(MODEL_BUCKET); \
if [ $$? != 0 ]; then \
localModelDir="./models"; \
if [ ! -d "$$localModelDir" ]; then \
mkdir "$$localModelDir"; \
fi; \
echo; \
echo "Preparing to download, convert, and upload safetensors for model: $$MODEL_ID"; \
echo "Local directory: '$$localModelDir' will be used to store downloaded and converted model weights"; \
echo "Note: sudo privileges required to remove model dir due to docker mount using root"; \
echo "Would you like to continue? [y/N] "; \
read confirm_download; \
if [ $${confirm_download:-'N'} = 'y' ]; then \
mkdir -p $$localModelDir; \
if [ -z "$$access_token" ]; then \
if [ -n "$$HUGGINGFACE_TOKEN" ]; then \
access_token="$$HUGGINGFACE_TOKEN"; \
elif [ -f ".hf_token_cache" ]; then \
access_token=$$(cat .hf_token_cache); \
else \
echo "What is your huggingface access token? "; \
read access_token; \
echo "$$access_token" > .hf_token_cache; \
fi; \
fi; \
echo "Converting and uploading safetensors for model: $$MODEL_ID"; \
tgiImage=$$(yq -r '[.ecsModels[] | select(.inferenceContainer == "tgi") | .baseImage] | first' $(PROJECT_DIR)/config-custom.yaml); \
if [ "$$tgiImage" = "null" ] || [ -z "$$tgiImage" ]; then \
tgiImage="ghcr.io/huggingface/text-generation-inference:latest"; \
fi; \
echo "Using TGI image: $$tgiImage"; \
$(PROJECT_DIR)/scripts/convert-and-upload-model.sh -m $$MODEL_ID -s $(MODEL_BUCKET) -a $$access_token -t $$tgiImage -d $$localModelDir; \
fi; \
fi; \
done
## Run all clean commands
clean: cleanTypeScript cleanPython cleanCfn cleanMisc
## Delete all compiled Python files and related artifacts
cleanPython:
@find . -type f -name "*.py[co]" -delete
@find . -type d -name "__pycache__" -exec rm -rf {} +
@find . -type d -name ".pytest_cache" -exec rm -rf {} +
@find . -type d -name "*.egg-info" -exec rm -rf {} +
@find . -type d -name "dist" -exec rm -rf {} +
@find . -type d -name ".mypy_cache" -exec rm -rf {} +
@find . -type d -name ".tox" -exec rm -rf {} +
## Delete TypeScript artifacts and related folders
cleanTypeScript:
@find . -type f -name "*.js.map" -delete
@find . -type d -name "dist" -exec rm -rf {} +
@find . -type d -name "build" -exec rm -rf {} +
@find . -type d -name ".tscache" -exec rm -rf {} +
@find . -type d -name ".jest_cache" -exec rm -rf {} +
@find . -type d -name "node_modules" -exec rm -rf {} +
@find . -type d -name "cdk.out" -exec rm -rf {} +
@find . -type d -name "coverage" -exec rm -rf {} +
## Delete CloudFormation outputs
cleanCfn:
@find . -type d -name "cdk.out" -exec rm -rf {} +
## Delete all misc files
cleanMisc:
@find . -type f -name "*.DS_Store" -delete
@rm -f .hf_token_cache
## Login Docker CLI to Amazon Elastic Container Registry
dockerLogin: dockerCheck
ifdef PROFILE
@$(foreach ACCOUNT,$(ACCOUNT_NUMBERS_ECR), \
aws ecr get-login-password --region ${REGION} --profile ${PROFILE} | $(DOCKER_CMD) login --username AWS --password-stdin ${ACCOUNT}.dkr.ecr.${REGION}.${DOMAIN} >/dev/null 2>&1; \
)
else
@$(foreach ACCOUNT,$(ACCOUNT_NUMBERS_ECR), \
aws ecr get-login-password --region ${REGION} | $(DOCKER_CMD) login --username AWS --password-stdin ${ACCOUNT}.dkr.ecr.${REGION}.${DOMAIN} >/dev/null 2>&1; \
)
endif
listStacks:
@npx cdk list
buildNpmModules:
BASE_URL=$(BASE_URL) npm run build
buildArchive:
BUILD_ASSETS=true npm run build
define print_config
@printf "\n \
DEPLOYING $(STACK) STACK APP INFRASTRUCTURE \n \
-----------------------------------\n \
Account Number $(ACCOUNT_NUMBER)\n \
Region $(REGION)\n \
Partition $(PARTITION)\n \
Domain $(DOMAIN)\n \
App Name $(APP_NAME)\n \
Deployment Stage $(DEPLOYMENT_STAGE)\n \
Deployment Name $(DEPLOYMENT_NAME)"
@if [ -n "$(PROFILE)" ]; then \
printf "\n Deployment Profile $(PROFILE)"; \
fi
@printf "\n-----------------------------------\n"
endef
## Deploy all infrastructure
deploy: install dockerCheck dockerLogin cleanMisc modelCheck buildNpmModules
$(call print_config)
ifeq ($(HEADLESS),true)
npx cdk deploy ${STACK} $(if $(PROFILE),--profile ${PROFILE}) --require-approval never -c ${ENV}='$(shell echo '${${ENV}}')';
else
@printf "Is the configuration correct? [y/N] "\
&& read confirm_config &&\
if [ $${confirm_config:-'N'} = 'y' ]; then \
npx cdk deploy ${STACK} $(if $(PROFILE),--profile ${PROFILE}) -c ${ENV}='$(shell echo '${${ENV}}')'; \
fi;
endif
## Tear down all infrastructure
destroy: cleanMisc
$(call print_config)
ifeq ($(HEADLESS),true)
npx cdk destroy ${STACK} --force $(if $(PROFILE),--profile ${PROFILE});
else
@printf "Is the configuration correct? [y/N] "\
&& read confirm_config &&\
if [ $${confirm_config:-'N'} = 'y' ]; then \
npx cdk destroy ${STACK} --force $(if $(PROFILE),--profile ${PROFILE}); \
fi;
endif
#################################################################################
# SELF DOCUMENTING COMMANDS #
#################################################################################
.DEFAULT_GOAL := help
# Inspired by <http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html>
# sed script explained:
# /^##/:
# * save line in hold space
# * purge line
# * Loop:
# * append newline + line to hold space
# * go to next line
# * if line starts with doc comment, strip comment character off and loop
# * remove target prerequisites
# * append hold space (+ newline) to line
# * replace newline plus comments by `---`
# * print line
# Separate expressions are necessary because labels cannot be delimited by
# semicolon; see <http://stackoverflow.com/a/11799865/1968>
help:
@echo "$$(tput bold)Available rules:$$(tput sgr0)"
@echo
@sed -n -e "/^## / { \
h; \
s/.*//; \
:doc" \
-e "H; \
n; \
s/^## //; \
t doc" \
-e "s/:.*//; \
G; \
s/\\n## /---/; \
s/\\n/ /g; \
p; \
}" ${MAKEFILE_LIST} \
| LC_ALL='C' sort --ignore-case \
| awk -F '---' \
-v ncol=$$(tput cols) \
-v indent=35 \
-v col_on="$$(tput setaf 6)" \
-v col_off="$$(tput sgr0)" \
'{ \
printf "%s%*s%s ", col_on, -indent, $$1, col_off; \
n = split($$2, words, " "); \
line_length = ncol - indent; \
for (i = 1; i <= n; i++) { \
line_length -= length(words[i]) + 1; \
if (line_length <= 0) { \
line_length = ncol - indent - length(words[i]) - 1; \
printf "\n%*s ", -indent, " "; \
} \
printf "%s ", words[i]; \
} \
printf "\n"; \
}' \
| more $(shell test $(shell uname) = Darwin && echo '--no-init --raw-control-chars')
## Run all Python unit tests (non-integration) with coverage report
test-coverage:
@echo "Running lambda tests with coverage..."
@pytest test/lambda --verbose \
--cov lambda \
--cov-report term-missing \
--cov-report html:build/coverage \
--cov-report xml:build/coverage/coverage.xml \
--cov-fail-under 83
@echo ""
@echo "Running MCP Workbench tests with coverage..."
@pytest test/mcp-workbench --verbose \
--cov lib/serve/mcp-workbench/src \
--cov-report term-missing \
--cov-report html:build/coverage-mcp \
--cov-report xml:build/coverage-mcp/coverage.xml \
--cov-append \
--cov-fail-under 83
@echo ""
@echo "Running SDK tests with coverage..."
@pytest test/sdk --verbose \
--cov lisa-sdk/lisapy \
--cov-report term-missing \
--cov-report html:build/coverage-sdk \
--cov-report xml:build/coverage-sdk/coverage.xml \
--cov-append \
--cov-fail-under 80
@echo ""
@echo "Running REST API tests with coverage..."
@pytest test/rest-api --verbose \
--cov lib/serve/rest-api/src \
--cov-config lib/serve/rest-api/.coveragerc \
--cov-report term-missing \
--cov-report html:build/coverage-rest-api \
--cov-report xml:build/coverage-rest-api/coverage.xml \
--cov-append \
--cov-fail-under 80
## Run all Python unit tests (non-integration) without coverage
test:
@echo "Running lambda tests..."
@pytest test/lambda --verbose
@echo ""
@echo "Running MCP Workbench tests..."
@pytest test/mcp-workbench --verbose
@echo ""
@echo "Running SDK tests..."
@pytest test/sdk --verbose
@echo ""
@echo "Running REST API tests..."
@pytest test/rest-api --verbose
## Run lambda tests only
test-lambda:
pytest test/lambda --verbose
## Run MCP Workbench tests only
test-mcp-workbench:
pytest test/mcp-workbench --verbose
## Run LISA SDK unit tests only
test-sdk:
pytest test/sdk --verbose
## Run REST API unit tests only
test-rest-api:
pytest test/rest-api --verbose
## Run LISA SDK integration tests (requires deployed LISA environment)
test-sdk-integ:
@echo "Running LISA SDK integration tests..."
@echo "Note: These tests require a deployed LISA environment with:"
@echo " - --api or --url argument for API endpoint"
@echo " - --region, --deployment, --profile arguments"
@echo " - AWS credentials configured"
@echo ""
@echo "Example: pytest test/integration/sdk --api https://your-api.com --region us-west-2"
@echo ""
pytest test/integration/sdk --verbose
## Run integration tests (Python-based)
test-integ:
pytest test/python --verbose
## Run RAG integration tests (requires deployed LISA environment)
test-rag-integ:
@echo "Running RAG integration tests..."
@echo "Note: These tests require a deployed LISA environment with:"
@echo " - LISA_API_URL environment variable set"
@echo " - LISA_DEPLOYMENT_NAME environment variable set"
@echo " - AWS credentials configured"
@echo ""
pytest test/integration --verbose
## Run repository metadata preservation integration tests
test-metadata-integ:
pytest test/integration/test_repository_update_metadata_preservation.py --verbose
## Regenerate all Poetry lock files
lock-poetry:
@echo "Regenerating Poetry lock files..."
@cd lisa-sdk && poetry lock && echo "✓ lisa-sdk/poetry.lock updated"
## Validate all requirements files can be installed
validate-deps:
@echo "Validating requirements files..."
@for req in $$(find . -name "requirements*.txt" -not -path "./node_modules/*" -not -path "./.venv/*"); do \
echo "Checking $$req..."; \
pip-compile --dry-run --quiet $$req 2>&1 | grep -i "error\|conflict" && echo "✗ $$req has conflicts" || echo "✓ $$req is valid"; \
done