-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
364 lines (266 loc) · 9 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
###############################
# Common defaults/definitions #
###############################
comma := ,
# Checks two given strings for equality.
eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\
$(findstring $(2),$(1))),1)
######################
# Project parameters #
######################
NAME := baza
OWNER := $(or $(GITHUB_REPOSITORY_OWNER),instrumentisto)
REGISTRIES := $(strip $(subst $(comma), ,\
$(shell grep -m1 'registry: \["' .github/workflows/ci.yml \
| cut -d':' -f2 | tr -d '"][')))
VERSION ?= $(strip $(shell grep -m1 'version = "' Cargo.toml | cut -d'"' -f2))
RUST_VER := $(strip $(shell grep -m1 'RUST_VER: ' .github/workflows/ci.yml \
| cut -d':' -f2 | tr -d '"'))
###########
# Aliases #
###########
all: fmt lint test.unit
docs: cargo.doc
fmt: cargo.fmt
image: docker.image
lint: cargo.lint
test: test.e2e test.unit
####################
# Running commands #
####################
# Stop running project in local environment.
#
# Usage:
# make down [dockerized=(no|yes)]
down:
ifeq ($(dockerized),yes)
-docker stop $(NAME)
else
$(eval pid := $(shell ps ax | grep -v grep | grep 'target/' | grep '/baza'))
$(if $(call eq,$(pid),),,kill $(strip $(word 1,$(pid))))
endif
# Run project in local environment.
#
# Usage:
# make up [background=(no|yes)]
# [debug=(yes|no)]
# [( [dockerized=no] [careful=(no|yes)]
# | dockerized=yes [tag=(dev|<docker-tag>)]
# [( [rebuild=no] | rebuild=yes [no-cache=(no|yes)] )] )]
up: down
ifeq ($(dockerized),yes)
ifeq ($(rebuild),yes)
@make docker.image tag=$(tag) debug=$(debug) no-cache=$(no-cache)
endif
@mkdir -p .cache/baza/
docker run --rm $(if $(call eq,$(background),yes),-d,-it) --name $(NAME) \
-u $(shell id -u) \
-p 9294:9294 \
-v "$(PWD)/.cache/baza/":/.cache/baza/:z \
$(OWNER)/$(NAME):$(or $(tag),dev) -r .cache/baza
else
ifneq ($(debug),no)
ifeq ($(careful),yes)
ifeq ($(shell cargo install --list | grep cargo-careful),)
cargo install cargo-careful
endif
ifeq ($(shell rustup component list --toolchain=nightly \
| grep 'rust-src (installed)'),)
rustup component add --toolchain=nightly rust-src
endif
endif
endif
ifeq ($(background),yes)
cargo $(if $(call eq,$(careful),yes),+nightly careful,) run \
$(if $(call eq,$(debug),no),--release,) -- --help > /dev/null
endif
cargo $(if $(call eq,$(careful),yes),+nightly careful,) run \
$(if $(call eq,$(debug),no),--release,) -- -r .cache/baza \
$(if $(call eq,$(background),yes),&,)
endif
##################
# Cargo commands #
##################
cargo-crate = $(if $(call eq,$(crate),),--workspace,-p $(crate))
# Generate crates documentation from Rust sources.
#
# Usage:
# make cargo.doc [crate=<crate-name>] [private=(yes|no)]
# [open=(yes|no)] [clean=(no|yes)]
cargo.doc:
ifeq ($(clean),yes)
@rm -rf target/doc/
endif
cargo doc $(cargo-crate) --all-features \
$(if $(call eq,$(private),no),,--document-private-items) \
$(if $(call eq,$(open),no),,--open)
# Format Rust sources with rustfmt.
#
# Usage:
# make cargo.fmt [check=(no|yes)]
cargo.fmt:
cargo +nightly fmt --all $(if $(call eq,$(check),yes),-- --check,)
# Lint Rust sources with Clippy.
#
# Usage:
# make cargo.lint [crate=<crate-name>]
cargo.lint:
cargo clippy $(cargo-crate) --all-features -- -D warnings
####################
# Testing commands #
####################
# Run project E2E tests.
#
# Usage:
# make test.e2e [only=<regex>]
# [( [start-app=no]
# | start-app=yes [debug=(yes|no)]
# [( [dockerized=no] [careful=(no|yes)]
# | dockerized=yes [tag=(dev|<docker-tag>)]
# [( [rebuild=no] |
# rebuild=yes [no-cache=(no|yes)] )] )] )]
test.e2e:
ifeq ($(start-app),yes)
@make up background=yes debug=$(debug) careful=$(careful) \
dockerized=$(dockerized) tag=$(tag) \
rebuild=$(rebuild) no-cache=$(no-cache)
sleep 5
endif
cargo test -p baza-e2e --test e2e -- -vv \
$(if $(call eq,$(only),),,--name '$(only)')
ifeq ($(start-app),yes)
@make down dockerized=$(dockerized)
endif
# Run project unit tests.
#
# Usage:
# make test.unit [crate=<crate-name>] [careful=(no|yes)]
test.unit:
ifeq ($(careful),yes)
ifeq ($(shell cargo install --list | grep cargo-careful),)
cargo install cargo-careful
endif
ifeq ($(shell rustup component list --toolchain=nightly \
| grep 'rust-src (installed)'),)
rustup component add --toolchain=nightly rust-src
endif
endif
cargo $(if $(call eq,$(careful),yes),+nightly careful,) test \
$(if $(call eq,$(crate),),--workspace --exclude baza-e2e,-p $(crate)) \
--all-features
###################
# Docker commands #
###################
docker-registries = $(strip $(if $(call eq,$(registries),),\
$(REGISTRIES),$(subst $(comma), ,$(registries))))
docker-tags = $(strip $(if $(call eq,$(tags),),\
$(VERSION),$(subst $(comma), ,$(tags))))
# Build project Docker image.
#
# Usage:
# make docker.image [tag=(dev|<docker-tag>)] [no-cache=(no|yes)]
# [debug=(yes|no)]
github_url := $(strip $(or $(GITHUB_SERVER_URL),https://github.com))
github_repo := $(strip $(or $(GITHUB_REPOSITORY),$(OWNER)/$(NAME)))
docker.image:
docker build --network=host --force-rm \
$(if $(call eq,$(no-cache),yes),--no-cache --pull,) \
--build-arg rust_ver=$(RUST_VER) \
--build-arg rustc_mode=$(if $(call eq,$(debug),no),release,debug) \
--build-arg rustc_opts=$(if $(call eq,$(debug),no),--release,) \
--label org.opencontainers.image.source=$(github_url)/$(github_repo) \
--label org.opencontainers.image.revision=$(strip \
$(shell git show --pretty=format:%H --no-patch)) \
--label org.opencontainers.image.version=$(subst v,,$(strip \
$(shell git describe --tags --dirty --match='v*'))) \
-t $(OWNER)/$(NAME):$(or $(tag),dev) ./
# Manually push project Docker images to container registries.
#
# Usage:
# make docker.push [tags=($(VERSION)|<docker-tag-1>[,<docker-tag-2>...])]
# [registries=($(REGISTRIES)|<prefix-1>[,<prefix-2>...])]
docker.push:
$(foreach tag,$(subst $(comma), ,$(docker-tags)),\
$(foreach registry,$(subst $(comma), ,$(docker-registries)),\
$(call docker.push.do,$(registry),$(tag))))
define docker.push.do
$(eval repo := $(strip $(1)))
$(eval tag := $(strip $(2)))
docker push $(repo)/$(OWNER)/$(NAME):$(tag)
endef
# Tag project Docker image with the given tags.
#
# Usage:
# make docker.tags [of=($(VERSION)|<docker-tag>)]
# [tags=($(VERSION)|<docker-tag-1>[,<docker-tag-2>...])]
# [registries=($(REGISTRIES)|<prefix-1>[,<prefix-2>...])]
docker.tags:
$(foreach tag,$(subst $(comma), ,$(docker-tags)),\
$(foreach registry,$(subst $(comma), ,$(docker-registries)),\
$(call docker.tags.do,$(or $(of),$(VERSION)),$(registry),$(tag))))
define docker.tags.do
$(eval from := $(strip $(1)))
$(eval repo := $(strip $(2)))
$(eval to := $(strip $(3)))
docker tag $(OWNER)/$(NAME):$(from) $(repo)/$(OWNER)/$(NAME):$(to)
endef
# Save project Docker images to a tarball file.
#
# Usage:
# make docker.tar [to-file=(.cache/docker/image.tar|<file-path>)]
# [tags=($(VERSION)|<docker-tag-1>[,<docker-tag-2>...])]
docker-tar-file = $(or $(to-file),.cache/docker/image.tar)
docker.tar:
@mkdir -p $(dir $(docker-tar-file))
docker save -o $(docker-tar-file) \
$(foreach tag,$(subst $(comma), ,$(or $(tags),$(VERSION))),\
$(OWNER)/$(NAME):$(tag))
# Load project Docker images from a tarball file.
#
# Usage:
# make docker.untar [from-file=(.cache/docker/image.tar|<file-path>)]
docker.untar:
docker load -i $(or $(from-file),.cache/docker/image.tar)
#################
# Helm commands #
#################
helm-chart := $(or $(chart),baza)
helm-chart-dir := helm/$(helm-chart)
# Lint project Helm chart.
#
# Usage:
# make helm.lint [chart=baza]
helm.lint:
helm lint $(helm-chart-dir)/
# Build Helm package from project Helm chart.
#
# Usage:
# make helm.package [chart=baza]
# [out-dir=(.cache/helm|<dir-path>)] [clean=(no|yes]]
helm-package-dir = $(or $(out-dir),.cache/helm)
helm.package:
ifeq ($(clean),yes)
@rm -rf $(helm-package-dir)
endif
@mkdir -p $(helm-package-dir)/
helm package --destination=$(helm-package-dir)/ $(helm-chart-dir)/
# Create and push Git tag to release project Helm chart.
#
# Usage:
# make helm.release [chart=baza]
helm-git-tag = helm/$(helm-chart)/$(strip \
$(shell grep -m1 'version: ' $(helm-chart-dir)/Chart.yaml | cut -d' ' -f2))
helm.release:
ifeq ($(shell git rev-parse $(helm-git-tag) >/dev/null 2>&1 && echo "ok"),ok)
$(error "Git tag $(helm-git-tag) already exists")
endif
git tag $(helm-git-tag)
git push origin refs/tags/$(helm-git-tag)
##################
# .PHONY section #
##################
.PHONY: all docs down fmt image lint test up \
cargo.doc cargo.fmt cargo.lint \
docker.image docker.tags docker.push docker.tar docker.untar \
helm.lint helm.package helm.release \
test.e2e test.unit