forked from i-dot-ai/redbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
201 lines (157 loc) · 6.26 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
-include .env
.PHONY: app reqs
reqs:
poetry install
run:
docker compose up -d elasticsearch kibana worker minio redis core-api db django-app
stop:
docker compose down
clean:
docker compose down -v --rmi all --remove-orphans
build:
docker compose build
rebuild:
docker compose build --no-cache
test-core-api:
poetry install --no-root --no-ansi --with api,dev,ai --without worker
poetry run pytest core_api/tests --cov=core_api/src -v --cov-report=term-missing --cov-fail-under=45
test-redbox:
poetry install --no-root --no-ansi --with api,dev --without ai,worker
poetry run pytest redbox/tests --cov=redbox -v --cov-report=term-missing --cov-fail-under=45
test-worker:
poetry install --no-root --no-ansi --with worker,dev --without ai,api
poetry run pytest worker/tests --cov=worker -v --cov-report=term-missing --cov-fail-under=40
test-django:
docker compose up -d --wait db minio
docker compose run django-app venv/bin/pytest tests/ --ds redbox_app.settings -v --cov=redbox_app.redbox_core --cov-fail-under 60 -o log_cli=true
test-integration:
docker compose down
cp .env .env.backup
cp .env.integration .env
docker compose build core-api worker minio
docker compose up -d core-api worker minio
poetry install --no-root --no-ansi --with dev --without ai,api,worker
sleep 10
poetry run pytest tests
cp .env.backup .env
rm .env.backup
collect-static:
docker compose run django-app venv/bin/django-admin collectstatic --noinput
lint:
poetry run ruff format . --check
poetry run ruff check .
format:
poetry run ruff format .
poetry run ruff check . --fix
safe:
poetry run bandit -ll -r ./redbox
poetry run bandit -ll -r ./django_app
poetry run mypy ./redbox --ignore-missing-imports
poetry run mypy ./django_app --ignore-missing-imports
checktypes:
poetry run mypy redbox worker --ignore-missing-imports
check-migrations:
docker compose build django-app
docker compose up -d --wait db minio
docker compose run django-app venv/bin/django-admin migrate
docker compose run django-app venv/bin/django-admin makemigrations --check
reset-db:
docker compose down db --volumes
docker compose up -d db
docs-serve:
poetry run mkdocs serve
docs-build:
poetry run mkdocs build
# Docker
AWS_REGION=eu-west-2
APP_NAME=redbox
ECR_URL=$(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
ECR_REPO_URL=$(ECR_URL)/$(ECR_REPO_NAME)
IMAGE=$(ECR_REPO_URL):$(IMAGE_TAG)
ECR_REPO_NAME=$(APP_NAME)
PREV_IMAGE_TAG=$$(git rev-parse HEAD~1)
IMAGE_TAG=$$(git rev-parse HEAD)
tf_build_args=-var "image_tag=$(IMAGE_TAG)"
DOCKER_SERVICES=$$(docker compose config --services | grep -v mlflow)
.PHONY: docker_login
docker_login:
aws ecr get-login-password --region $(AWS_REGION) | docker login --username AWS --password-stdin $(ECR_URL)
.PHONY: docker_build
docker_build: ## Build the docker container
@cp .env.example .env
# Fetching list of services defined in docker compose configuration
@echo "Services to update: $(DOCKER_SERVICES)"
# Enabling Docker BuildKit for better build performance
export DOCKER_BUILDKIT=1
@for service in $(DOCKER_SERVICES); do \
if grep -A 2 "^\s*$$service:" docker-compose.yml | grep -q 'build:'; then \
echo "Building $$service..."; \
PREV_IMAGE="$(ECR_REPO_URL)-$$service:$(PREV_IMAGE_TAG)"; \
echo "Pulling previous image: $$PREV_IMAGE"; \
docker pull $$PREV_IMAGE; \
docker compose build $$service; \
else \
echo "Skipping $$service uses default image"; \
fi; \
done
.PHONY: docker_push
docker_push:
@echo "Services to push: $(DOCKER_SERVICES)"
@for service in $(DOCKER_SERVICES); do \
if grep -A 2 "^\s*$$service:" docker-compose.yml | grep -q 'build:'; then \
echo "Pushing $$service..."; \
ECR_REPO_SERVICE_TAG=$(ECR_REPO_URL)-$$service:$(IMAGE_TAG); \
CURRENT_TAG=$$(grep -A 1 "^\s*$$service:" docker-compose.yml | grep 'image:' | sed 's/.*image:\s*//'); \
echo "Tagging $$service: $$CURRENT_TAG -> $$ECR_REPO_SERVICE_TAG"; \
docker tag $$CURRENT_TAG $$ECR_REPO_SERVICE_TAG; \
docker push $$ECR_REPO_SERVICE_TAG; \
else \
echo "Skipping $$service uses default image"; \
fi; \
done
.PHONY: docker_update_tag
docker_update_tag:
MANIFEST=$$(aws ecr batch-get-image --repository-name $(ECR_REPO_NAME) --image-ids imageTag=$(IMAGE_TAG) --query 'images[].imageManifest' --output text) && \
aws ecr put-image --repository-name $(ECR_REPO_NAME) --image-tag $(tag) --image-manifest "$$MANIFEST"
# Ouputs the value that you're after - usefx ul to get a value i.e. IMAGE_TAG out of the Makefile
.PHONY: docker_echo
docker_echo:
echo $($(value))
CONFIG_DIR=../../../redbox-copilot-infra-config
TF_BACKEND_CONFIG=$(CONFIG_DIR)/backend.hcl
tf_new_workspace:
terraform -chdir=./infrastructure/aws workspace new $(env)
tf_set_workspace:
terraform -chdir=./infrastructure/aws workspace select $(env)
tf_set_or_create_workspace:
make tf_set_workspace || make tf_new_workspace
.PHONY: tf_init
tf_init: ## Initialise terraform
terraform -chdir=./infrastructure/aws init -backend-config=$(TF_BACKEND_CONFIG)
.PHONY: tf_plan
tf_plan: ## Plan terraform
make tf_set_workspace && \
terraform -chdir=./infrastructure/aws plan -var-file=$(CONFIG_DIR)/${env}-input-params.tfvars ${tf_build_args}
.PHONY: tf_apply
tf_apply: ## Apply terraform
make tf_set_workspace && \
terraform -chdir=./infrastructure/aws apply -var-file=$(CONFIG_DIR)/${env}-input-params.tfvars ${tf_build_args}
.PHONY: tf_init_universal
tf_init_universal: ## Initialise terraform
terraform -chdir=./infrastructure/aws/universal init -backend-config=../$(TF_BACKEND_CONFIG)
.PHONY: tf_apply_universal
tf_apply_universal: ## Apply terraform
terraform -chdir=./infrastructure/aws workspace select prod && \
terraform -chdir=./infrastructure/aws/universal apply -var-file=../$(CONFIG_DIR)/prod-input-params.tfvars
.PHONY: tf_auto_apply
tf_auto_apply: ## Auto apply terraform
make tf_set_workspace && \
terraform -chdir=./infrastructure/aws apply -auto-approve -var-file=$(CONFIG_DIR)/${env}-input-params.tfvars ${tf_build_args}
.PHONY: tf_destroy
tf_destroy: ## Destroy terraform
make tf_set_workspace && \
terraform -chdir=./infrastructure/aws destroy -var-file=$(CONFIG_DIR)/${env}-input-params.tfvars ${tf_build_args}
# Release commands to deploy your app to AWS
.PHONY: release
release: ## Deploy app
chmod +x ./infrastructure/scripts/release.sh && ./infrastructure/scripts/release.sh $(env)