File size: 9,525 Bytes
67e46ef
 
 
 
 
 
 
69e3f65
67e46ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
IMAGE_NAME := Akcom/test_clip

RELEASE_BRANCH := release
MAINLINE_BRANCH := master
CURRENT_BRANCH := $(shell git branch | grep \* | cut -d ' ' -f2)

VERSION := 0.0.1
PYTHON_VER := 3.8

comma := ,
eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\
                                $(findstring $(2),$(1))),1)



###########
# Aliases #
###########

up: docker.up
down: docker.down



# Build all project artifacts from sources.
#
# Usage:
#	make build

build: | docker.build



down: docker.down


up: docker.up


# Resolve all project dependencies.
#
# Usage:
#	make deps [dev=(yes|no)]

#deps: | deps.python.wheel deps.python.install
deps: | deps.python.install
ifeq ($(wildcard my.env),)
	cp my.dist.env my.env
endif


###################
# Python commands #
###################

# Resolve Python project dependencies.
#
# Optional 'cmd' parameter may be used for handy usage of docker-wrapped
# pip, for example: make deps.python cmd='install diffusers'
#
# Usage:
#	make deps.python [cmd=(install|<pip-cmd>)]
#	                   [dev=(yes|no)]
#	                   [dockerized=(yes|no)]

pip-cmd = $(if $(call eq,$(cmd),),install --upgrade -r /tmp/requirements.txt,$(cmd))

# Think about freeze requirements
#pip freeze -r /tmp/requirements.txt > /tmp/requirements.txt &&
deps.python.install:
ifneq ($(dockerized),no)
	docker run --rm --network=host \
			   -e PYTHONPATH=/app/volume/ppath \
			   -v "$(PWD)"/docker/rootfs/etc/pip.conf:/etc/pip.conf \
			   -v "$(PWD)"/requirements.txt:/tmp/requirements.txt \
			   -v "$(PWD)"/volume/.cache/:/app/volume/.cache/ \
	           -e CI_SERVER='$(CI_SERVER)' \
		python:$(PYTHON_VER)-slim \
			pip $(pip-cmd)
else
	pip $(pip-cmd)
endif





###################
# Docker commands #
###################

# Authenticate to GitLab Container Registry where project Docker images
# are stored.
#
# Usage:
#   make docker.auth [user=<gitlab-username>]
#	 			     [pass-stdin=(no|yes)]
#                    [cluster=runpod]
ifeq ($(cluster),runpod)
docker_hub_provider = registry.hub.docker.com
else
docker_hub_provider = $(word 1,$(subst /, ,$(IMAGE_NAME)))
endif

docker.auth:
	docker login $(docker_hub_provider) \
		$(if $(call eq,$(user),),,--username=$(user)) \
		$(if $(call eq,$(pass-stdin),yes),--password-stdin,)


# Stop project in Dockerized development environment
# and remove all related containers.
#
# Usage:
#   make docker.down

docker.down:
	docker-compose down --rmi=local -v


# Build project Docker image.
#
# Usage:
#	make docker.build [IMAGE=(<empty>|artifacts|<docker-image-postfix>)]
#	                  [TAG=(dev|<tag>)]
#	                  [target=(debug|runtime)] [no-cache=(no|yes)]
#                     [cluster=runpod]

build-docker-dir = .
build-dockerfile-dir = ./docker/Dockerfile_tensor_gpu
ifeq ($(cluster),runpod)
build-docker-image-name = andreyklumchyk/trdbt$(if $(call eq,$(IMAGE),),,/$(IMAGE))
else
build-docker-image-name = $(IMAGE_NAME)$(if $(call eq,$(IMAGE),),,/$(IMAGE))
endif
ifeq ($(IMAGE),artifacts)
build-docker-dir = build/artifacts
endif

docker.build:
	docker build --network=host --force-rm \
		-f $(build-dockerfile-dir) \
		$(if $(call eq,$(no-cache),yes),--no-cache,) \
		$(if $(call eq,$(target),),,--target=$(target)) \
		--build-arg VERSION=$(VERSION) \
		-t $(build-docker-image-name):$(if $(call eq,$(TAG),),dev,$(TAG)) \
		$(build-docker-dir)/


# Push project Docker images to GitLab Container Registry.
#
# Usage:
#   make docker.push [IMAGE=(<empty>|artifacts|<docker-image-postfix>)]
#                    [TAGS=(dev|<t1>[,<t2>...])]
#                    [cluster=runpod]

docker-push-tags = $(if $(call eq,$(TAGS),),dev,$(TAGS))
ifeq ($(cluster),runpod)
docker-push-image-name = andreyklumchyk/trdbt$(if $(call eq,$(IMAGE),),,/$(IMAGE))
else
docker-push-image-name = $(IMAGE_NAME)$(if $(call eq,$(IMAGE),),,/$(IMAGE))
endif

docker.push:
	$(foreach tag,$(subst $(comma), ,$(docker-push-tags)),\
		$(call docker.push.do,$(tag)))
define docker.push.do
	$(eval tag := $(strip $(1)))
	docker push $(docker-push-image-name):$(tag)
endef


# Pull project Docker images from GitLab Container Registry.
#
# Usage:
#   make docker.pull [IMAGE=(<empty>|artifacts|<docker-images-postfix>)]
#                    [TAGS=(dev|@all|<t1>[,<t2>...])]

docker-pull-image-name = $(IMAGE_NAME)$(if $(call eq,$(IMAGE),),,/$(IMAGE))
docker-pull-tags = $(if $(call eq,$(TAGS),),dev,$(TAGS))

docker.pull:
ifeq ($(docker-pull-tags),@all)
	docker pull $(docker-pull-image-name) --all-tags
else
	$(foreach tag,$(subst $(comma), ,$(docker-pull-tags)),\
		$(call docker.pull.do,$(tag)))
endif
define docker.pull.do
	$(eval tag := $(strip $(1)))
	docker pull $(docker-pull-image-name):$(tag)
endef


# Tag project Docker image with given tags.
#
# Usage:
#   make docker.tag [IMAGE=(<empty>|artifacts|<docker-image-postfix>)]
#                   [of=(dev|<of-tag>)] [TAGS=(dev|<with-t1>[,<with-t2>...])]

docker-tag-image-name = $(IMAGE_NAME)$(if $(call eq,$(IMAGE),),,/$(IMAGE))
docker-tag-of = $(if $(call eq,$(of),),dev,$(of))
docker-tag-with = $(if $(call eq,$(TAGS),),dev,$(TAGS))

docker.tag:
	$(foreach tag,$(subst $(comma), ,$(docker-tag-with)),\
		$(call docker.tag.do,$(docker-tag-of),$(tag)))
define docker.tag.do
	$(eval from := $(strip $(1)))
	$(eval to := $(strip $(2)))
	docker tag $(docker-tag-image-name):$(from) $(docker-tag-image-name):$(to)
endef


# Save project Docker images in a tarball file.
#
# Usage:
#   make docker.tar [IMAGE=(<empty>|review|artifacts)]
#                   [TAGS=(dev|<t1>[,<t2>...])]

docker-tar-image-name = $(IMAGE_NAME)$(if $(call eq,$(IMAGE),),,/$(IMAGE))
docker-tar-dir = .cache/docker/$(docker-tar-image-name)
docker-tar-tags = $(if $(call eq,$(TAGS),),dev,$(TAGS))

docker.tar:
	@mkdir -p $(docker-tar-dir)/
	$(docker-env) \
	docker save \
		-o $(docker-tar-dir)/$(subst $(comma),_,$(docker-tar-tags)).tar \
		$(foreach tag,$(subst $(comma), ,$(docker-tar-tags)),\
			$(docker-tar-image-name):$(tag))


# Load project Docker images from a tarball file.
#
# Usage:
#   make docker.untar [IMAGE=(<empty>|review|artifacts)]
#                     [TAGS=(dev|<t1>[,<t2>...])]

docker-untar-image-name = $(IMAGE_NAME)$(if $(call eq,$(IMAGE),),,/$(IMAGE))
docker-untar-dir = .cache/docker/$(docker-untar-image-name)
docker-untar-tags = $(if $(call eq,$(TAGS),),dev,$(TAGS))

docker.untar:
	$(docker-env) \
	docker load \
		-i $(docker-untar-dir)/$(subst $(comma),_,$(docker-untar-tags)).tar


# Run project in Dockerized development environment.
#
# Usage:
#   make docker.up [rebuild=(no|yes)]

docker.up: docker.down
	docker-compose up --abort-on-container-exit \
		$(if $(call eq,$(rebuild),yes),--build,)


# Run Bats tests for project Docker image.
#
# Documentation of Bats:
#   https://github.com/bats-core/bats-core
#
# Usage:
#   make docker.test [IMAGE=(<empty>|review|<docker-image-postfix>)]
#                    [TAG=(dev|<docker-tag>)]

test-docker-image = $(IMAGE_NAME)$(if $(call eq,$(IMAGE),),,/$(IMAGE))
test-docker-tag = $(if $(call eq,$(TAG),),dev,$(TAG))

docker.test:
	IMAGE=$(test-docker-image):$(test-docker-tag) \
	node_modules/.bin/bats tests/docker/suite.bats



################
# Git commands #
################

# Release project version (merge to release branch and apply version tag).
#
# Usage:
#   make git.release [VERSION=]

git.release:
ifneq ($(CURRENT_BRANCH),$(MAINLINE_BRANCH))
	@echo "--> Current branch is not '$(MAINLINE_BRANCH)'" && false
endif
	git fetch origin --tags $(RELEASE_BRANCH):$(RELEASE_BRANCH)
ifeq ($(shell git rev-parse v$(VERSION) >/dev/null 2>&1 && echo "ok"),ok)
	@echo "--> Tag v$(VERSION) already exists" && false
endif
	git fetch . $(MAINLINE_BRANCH):$(RELEASE_BRANCH)
	git tag v$(VERSION) $(RELEASE_BRANCH)
	git push origin $(RELEASE_BRANCH)
	git push --tags origin $(RELEASE_BRANCH)



# Squash changes of the current Git branch onto another Git branch.
#
# WARNING: You must merge `onto` branch in the current branch before squash!
#
# Usage:
#	make git.squash [onto=] [del=(no|yes)]

onto ?= $(MAINLINE_BRANCH)
del ?= no
upstream ?= origin

git.squash:
ifeq ($(CURRENT_BRANCH),$(onto))
	@echo "--> Current branch is '$(onto)' already" && false
endif
	git checkout $(onto)
	git branch -m $(CURRENT_BRANCH) orig-$(CURRENT_BRANCH)
	git checkout -b $(CURRENT_BRANCH)
	git branch --set-upstream-to $(upstream)/$(CURRENT_BRANCH)
	git merge --squash orig-$(CURRENT_BRANCH)
ifeq ($(del),yes)
	git branch -d orig-$(CURRENT_BRANCH)
endif



###################
# GitLab commands #
###################

# Prepare release notes for GitLab release.
#
# Usage:
#   make gitlab.release.notes [VERSION=<proj-version>]
#        [project-url=(https://gitlab.com/cmn_src/front1|<gitlab-project-url>)]

gitlab-proj-url = $(strip $(if $(call eq,$(project-url),),\
	https://gitlab.com/cmn_src/front1,$(project-url)))

gitlab.release.notes:
	@echo "$(strip \
		[Changelog]($(gitlab-proj-url)/-/blob/v$(VERSION)/CHANGELOG.md#$(shell \
			sed -n '/^## \[$(VERSION)\]/{\
				s/^## \[\(.*\)\][^0-9]*\([0-9].*\)/\1-\2/;\
				s/[^0-9a-z-]*//g;\
				p;\
			}' CHANGELOG.md)) | \
		[Milestone]($(gitlab-proj-url)/-/milestones/$(shell \
			sed -n '/^## \[$(VERSION)\]/,/Milestone/{\
				s/.*milestones.\([0-9]*\).*/\1/p;\
			}' CHANGELOG.md)) | \
		[Repository]($(gitlab-proj-url)/-/tree/v$(VERSION)))"



.PHONY: build deps down git.release squash up \
		deps.python.install \
		docker.auth docker.build docker.down docker.pull docker.push \
		docker.tag docker.test docker.up docker.tar docker.untar \
		git.release git.squash gitlab.release.notes