-
Notifications
You must be signed in to change notification settings - Fork 7
/
Makefile
349 lines (287 loc) · 10.7 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
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
.PHONY: \
bootstrap createPythonEnvironment installPythonRequirements \
createTypeScriptEnvironment installTypeScriptRequirements \
deploy destroy \
clean cleanTypeScript cleanPython cleanCfn cleanMisc \
help dockerCheck dockerLogin listStacks modelCheck buildEcsDeployer
#################################################################################
# GLOBALS #
#################################################################################
PROJECT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
HEADLESS = false
DOCKER_CMD ?= $(or $(CDK_DOCKER),docker)
# Arguments defined through command line or config.yaml
# PROFILE (optional argument)
ifeq (${PROFILE},)
TEMP_PROFILE := $(shell cat $(PROJECT_DIR)/config-custom.yaml | yq .profile)
ifneq ($(TEMP_PROFILE), null)
PROFILE := ${TEMP_PROFILE}
else
$(warning profile is not set in the command line using PROFILE variable or config files, attempting deployment without this variable)
endif
endif
# DEPLOYMENT_NAME
ifeq (${DEPLOYMENT_NAME},)
DEPLOYMENT_NAME := $(shell cat $(PROJECT_DIR)/config-custom.yaml | yq .deploymentName)
endif
ifeq (${DEPLOYMENT_NAME}, null)
DEPLOYMENT_NAME := $(shell cat $(PROJECT_DIR)/config-base.yaml | yq .deploymentName)
endif
ifeq (${DEPLOYMENT_NAME}, null)
DEPLOYMENT_NAME := prod
endif
# ACCOUNT_NUMBER
ifeq (${ACCOUNT_NUMBER},)
ACCOUNT_NUMBER := $(shell cat $(PROJECT_DIR)/config-custom.yaml | yq .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 := $(shell cat $(PROJECT_DIR)/config-custom.yaml | yq .region)
endif
ifeq (${REGION},)
$(error region must be set in command line using REGION variable or config files)
endif
# URL_SUFFIX - used for the docker login
ifeq ($(findstring iso,${REGION}),)
URL_SUFFIX := amazonaws.com
else
URL_SUFFIX := c2s.ic.gov
endif
# Arguments defined through config files
# APP_NAME
APP_NAME := $(shell cat $(PROJECT_DIR)/config-custom.yaml | yq .appName)
ifeq (${APP_NAME}, null)
APP_NAME := $(shell cat $(PROJECT_DIR)/config-base.yaml | yq .appName)
endif
ifeq (${APP_NAME}, null)
APP_NAME := lisa
endif
# DEPLOYMENT_STAGE
DEPLOYMENT_STAGE := $(shell cat $(PROJECT_DIR)/config-custom.yaml | yq .deploymentStage)
ifeq (${DEPLOYMENT_STAGE}, null)
DEPLOYMENT_STAGE := $(shell cat $(PROJECT_DIR)/config-base.yaml | yq .deploymentStage)
endif
ifeq (${DEPLOYMENT_STAGE}, null)
DEPLOYMENT_STAGE := prod
endif
# ACCOUNT_NUMBERS_ECR - AWS account numbers that need to be logged into with Docker CLI to use ECR
ifneq ($(shell cat $(PROJECT_DIR)/config-custom.yaml | yq '.accountNumbersEcr'), null)
ACCOUNT_NUMBERS_ECR := $(shell cat $(PROJECT_DIR)/config-custom.yaml | yq .accountNumbersEcr[])
endif
# 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
ifneq ($(shell cat $(PROJECT_DIR)/config-custom.yaml | yq '.ecsModels'), null)
MODEL_IDS := $(shell cat $(PROJECT_DIR)/config-custom.yaml | yq '.ecsModels[].modelName')
endif
# MODEL_BUCKET - S3 bucket containing model artifacts
MODEL_BUCKET := $(shell cat $(PROJECT_DIR)/config-custom.yaml | yq '.s3BucketModels')
#################################################################################
# COMMANDS #
#################################################################################
## Bootstrap AWS Account with CDK bootstrap
bootstrap:
@printf "Bootstrapping: $(ACCOUNT_NUMBER) | $(REGION)\n"
ifdef PROFILE
@cdk bootstrap \
--profile $(PROFILE) \
aws://$(ACCOUNT_NUMBER)/$(REGION) \
--cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess
else
@cdk bootstrap \
aws://$(ACCOUNT_NUMBER)/$(REGION) \
--cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess
endif
## Set up Python interpreter environment
createPythonEnvironment:
python3 -m venv .venv
@printf ">>> New virtual environment created. To activate run: 'source .venv/bin/activate'"
## Install Python dependencies for development
installPythonRequirements:
pip3 install pip --upgrade
pip3 install -r requirements-dev.txt
## Set up TypeScript interpreter environment
createTypeScriptEnvironment:
npm init
## Install TypeScript dependencies for development
installTypeScriptRequirements:
npm install
## 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:
@$(foreach MODEL_ID,$(MODEL_IDS), \
$(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; \
echo "What is your huggingface access token? "; \
read -s access_token; \
echo "Converting and uploading safetensors for model: $(MODEL_ID)"; \
tgiImage=$$(yq -r '[.ecsModels[] | select(.inferenceContainer == "tgi") | .baseImage] | first' $(PROJECT_DIR)/config-custom.yaml); \
echo $$tgiImage; \
$(PROJECT_DIR)/scripts/convert-and-upload-model.sh -m $(MODEL_ID) -s $(MODEL_BUCKET) -a $$access_token -t $$tgiImage -d $$localModelDir; \
fi; \
fi; \
)
## 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 {} +
## 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
## 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}.${URL_SUFFIX} >/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}.${URL_SUFFIX} >/dev/null 2>&1; \
)
endif
listStacks:
@npx cdk list
buildEcsDeployer:
@cd ./ecs_model_deployer && npm install && npm run build
define print_config
@printf "\n \
DEPLOYING $(STACK) STACK APP INFRASTRUCTURE \n \
-----------------------------------\n \
Account Number $(ACCOUNT_NUMBER)\n \
Region $(REGION)\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: dockerCheck dockerLogin cleanMisc modelCheck buildEcsDeployer
$(call print_config)
ifneq (,$(findstring true, $(HEADLESS)))
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)
@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;
#################################################################################
# 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')