Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
219 lines (190 loc) · 5.97 KB
/
Copy pathMakefile
File metadata and controls
219 lines (190 loc) · 5.97 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
CLI_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(sort $(subst :,\:,$(CLI_ARGS))):;@:)
PRIMARY_GOAL := $(firstword $(MAKECMDGOALS))
ifeq ($(PRIMARY_GOAL),)
PRIMARY_GOAL := help
endif
include docker/.env
ifneq (,$(wildcard .env))
include .env
endif
QUEUE_DRIVER ?= amqp
EXTRACTOR_ADAPTER ?= kreuzberg
WORKERS ?= 2
LLM_ADAPTER ?= llamacpp
LLAMA_CPP_SERVICE ?= 1
export QUEUE_DRIVER
export EXTRACTOR_ADAPTER
export DATABASE_DSN
export DOCUMENT_STORAGE_DRIVER
export DOCUMENT_LOCAL_STORAGE_ROOT
export S3_ENDPOINT
export S3_REGION
export S3_BUCKET
export S3_ACCESS_KEY
export S3_SECRET_KEY
export S3_PATH_STYLE
export DOCUMENT_PROCESSING_LEASE_SECONDS
export AMQP_HOST
export AMQP_PORT
export AMQP_USER
export AMQP_PASSWORD
export AMQP_VHOST
export REDIS_HOST
export REDIS_PORT
export REDIS_TIMEOUT
export LLM_ADAPTER
export LLM_BASE_URL
export LLM_API_KEY
export LLM_MODEL
export LLAMA_CPP_SERVICE
export LLAMA_CPP_HF_REPO
export LLAMA_CPP_MODEL
export LLAMA_CPP_MODEL_URL
export LLAMA_CPP_CTX_SIZE
export LLAMA_CPP_PARALLEL
export LLAMA_CPP_N_PREDICT
# Current user ID and group ID except MacOS where it conflicts with Docker abilities
ifeq ($(shell uname), Darwin)
export UID=1000
export GID=1000
else
export UID=$(shell id -u)
export GID=$(shell id -g)
endif
export COMPOSE_PROJECT_NAME=${STACK_NAME}
DOCKER_COMPOSE_DEV := docker compose -f docker/compose.yml -f docker/dev/compose.yml
DOCKER_COMPOSE_DEV_ALL := $(DOCKER_COMPOSE_DEV) --profile worker --profile llm
DOCKER_COMPOSE_TEST := docker compose -f docker/compose.yml -f docker/test/compose.yml
DOCKER_COMPOSE_DEV_UP := $(DOCKER_COMPOSE_DEV)
DOCKER_COMPOSE_DEV_UP_OPTIONS := up -d --remove-orphans
LLAMA_CPP_SERVICE_ENABLED := 0
ifeq ($(LLM_ADAPTER),llamacpp)
ifneq (,$(filter 1 true yes,$(LLAMA_CPP_SERVICE)))
LLAMA_CPP_SERVICE_ENABLED := 1
DOCKER_COMPOSE_DEV_UP := $(DOCKER_COMPOSE_DEV_UP) -f docker/dev/llm.compose.yml --profile llm
endif
endif
ifneq ($(QUEUE_DRIVER),)
ifneq ($(QUEUE_DRIVER),sync)
DOCKER_COMPOSE_DEV_UP := $(DOCKER_COMPOSE_DEV_UP) --profile worker
DOCKER_COMPOSE_DEV_UP_OPTIONS := up -d --remove-orphans --scale worker=$(WORKERS)
endif
endif
#
# Development
#
ifeq ($(PRIMARY_GOAL),build)
build: ## Build docker images.
$(DOCKER_COMPOSE_DEV) build $(CLI_ARGS)
endif
ifeq ($(PRIMARY_GOAL),up)
up: ## Up the dev environment.
ifneq ($(LLAMA_CPP_SERVICE_ENABLED),1)
$(DOCKER_COMPOSE_DEV) --profile llm rm -sf llama
endif
$(DOCKER_COMPOSE_DEV_UP) $(DOCKER_COMPOSE_DEV_UP_OPTIONS)
endif
ifeq ($(PRIMARY_GOAL),down)
down: ## Down the dev environment.
$(DOCKER_COMPOSE_DEV_ALL) down --remove-orphans
endif
ifeq ($(PRIMARY_GOAL),stop)
stop: ## Stop the dev environment.
$(DOCKER_COMPOSE_DEV_ALL) stop
endif
ifeq ($(PRIMARY_GOAL),clear)
clear: ## Remove development docker containers and volumes.
$(DOCKER_COMPOSE_DEV_ALL) down --volumes --remove-orphans
endif
ifeq ($(PRIMARY_GOAL),shell)
shell: ## Get into container shell.
$(DOCKER_COMPOSE_DEV) exec app /bin/bash
endif
#
# Tools
#
ifeq ($(PRIMARY_GOAL),yii)
yii: ## Execute Yii command.
$(DOCKER_COMPOSE_DEV) run --rm app ./yii $(CLI_ARGS)
.PHONY: yii
endif
ifeq ($(PRIMARY_GOAL),composer)
composer: ## Run Composer.
$(DOCKER_COMPOSE_DEV) run --rm app composer $(CLI_ARGS)
endif
ifeq ($(PRIMARY_GOAL),rector)
rector: ## Run Rector.
$(DOCKER_COMPOSE_DEV) run --rm app ./vendor/bin/rector $(CLI_ARGS)
endif
ifeq ($(PRIMARY_GOAL),cs-fix)
cs-fix: ## Run PHP CS Fixer.
$(DOCKER_COMPOSE_DEV) run --rm app ./vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --diff
endif
#
# Tests and analysis
#
ifeq ($(PRIMARY_GOAL),test)
test: ## Run tests.
$(DOCKER_COMPOSE_TEST) run --rm app ./vendor/bin/testo $(CLI_ARGS)
endif
ifeq ($(PRIMARY_GOAL),test-coverage)
test-coverage: ## Run tests with coverage.
$(DOCKER_COMPOSE_TEST) run --rm -e TEST_COVERAGE=1 -e XDEBUG_MODE=coverage app ./vendor/bin/testo --coverage $(CLI_ARGS)
endif
ifeq ($(PRIMARY_GOAL),testo)
testo: ## Run Testo.
$(DOCKER_COMPOSE_TEST) run --rm app ./vendor/bin/testo $(CLI_ARGS)
endif
ifeq ($(PRIMARY_GOAL),psalm)
psalm: ## Run Psalm.
$(DOCKER_COMPOSE_DEV) run --rm app ./vendor/bin/psalm $(CLI_ARGS)
endif
ifeq ($(PRIMARY_GOAL),composer-dependency-analyser)
composer-dependency-analyser: ## Run Composer Dependency Analyser.
$(DOCKER_COMPOSE_DEV) run --rm app ./vendor/bin/composer-dependency-analyser --config=composer-dependency-analyser.php $(CLI_ARGS)
endif
#
# Production
#
ifeq ($(PRIMARY_GOAL),prod-build)
prod-build: ## Build an image.
docker build --file docker/Dockerfile --target prod --pull -t ${IMAGE}:${IMAGE_TAG} .
endif
ifeq ($(PRIMARY_GOAL),prod-push)
prod-push: ## Push image to repository.
docker push ${IMAGE}:${IMAGE_TAG}
endif
ifeq ($(PRIMARY_GOAL),prod-deploy)
prod-deploy: ## Deploy to production.
set -euo pipefail \
docker -H ${PROD_SSH} stack deploy --prune --detach=false --with-registry-auth -c docker/compose.yml -c docker/prod/compose.yml ${STACK_NAME} 2>&1 | tee deploy.log \
if grep -qiE 'rollback:|update rolled back|service update paused' deploy.log then \
FAILED_TASK_ID="$(grep -oiE 'task[[:space:]]+[a-z0-9]+' deploy.log | head -n 1 | awk '{print $2}')" \
if [ -n "${FAILED_TASK_ID}" ]; then \
echo "Docker Swarm update failed. Failed task ID: ${FAILED_TASK_ID}" \
echo "--- docker service logs (${FAILED_TASK_ID}) ---" \
docker -H ${PROD_SSH} service logs --timestamps --tail 500 "${FAILED_TASK_ID}" || true \
else \
echo 'Docker Swarm update failed. Failed task ID: not found in deploy output.' \
fi \
exit 1 \
fi
endif
#
# Other
#
ifeq ($(PRIMARY_GOAL),help)
help: ## This help.
@awk 'BEGIN { printf "\nUsage:\n make \033[36m<target>\033[0m\n" } \
/^#$$/ { blank = 1; next } \
blank && /^# [a-zA-Z]/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 3); blank = 0; next } \
/^[a-zA-Z_-]+:([^=]|$$)/ { \
split($$0, parts, "##"); \
target = parts[1]; sub(/:.*/, "", target); \
desc = parts[2]; \
gsub(/^[[:space:]]+|[[:space:]]+$$/, "", desc); \
printf " \033[36m%-25s\033[0m %s\n", target, desc; \
blank = 0; \
}' $(MAKEFILE_LIST)
endif