-
Notifications
You must be signed in to change notification settings - Fork 2
/
makefile
466 lines (426 loc) · 15.6 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# {{{ -- meta
OPSYS := alpine
SVCNAME := nfs
SRCIMAGE := s6
ifndef ORGNAME # ensure ORGNAME in env is prioritised over default
ORGNAME := woahbase
endif
REPONAME := $(OPSYS)-$(SVCNAME)
ifndef REGISTRY # ensure REGISTRY in env is prioritised over default
REGISTRY := $(shell docker info -f '{{.IndexServerAddress}}'|awk -F[/:] '{print $$4}')# default is index.docker.io
endif
# for daily build tags, will replace multiple same-day builds
BUILDDATE := $(shell date -u +%Y%m%d)
# detect builder host architecture
HOSTARCH ?= $(call get_os_platform)
# target architecture on build and run, defaults to host architecture
ARCH ?= $(HOSTARCH)
IMAGEBASE ?= $(if $(filter scratch,$(SRCIMAGE)),scratch,$(REGISTRY)/$(ORGNAME)/$(OPSYS)-$(SRCIMAGE):$(if $(SRCTAG),$(SRCTAG),$(ARCH)))
IMAGETAG ?= $(REGISTRY)/$(ORGNAME)/$(REPONAME):$(ARCH)
CNTNAME := docker_$(SVCNAME)
CNTSHELL := /bin/bash
VERSION ?= $(call get_svc_version)
TESTCMD := \
uname -a; \
rpc.mountd -v; \
mount.nfs -V ; \
#
# -- }}}
# {{{ -- flags
# buildtime flags
# pull newest version of source
# or cache intermediate images,
CACHEFLAGS := --no-cache=true --pull
DOCKERFILE ?= $(if $(wildcard Dockerfile_$(ARCH)),Dockerfile_$(ARCH),Dockerfile)
LABELFLAGS ?= \
--label online.woahbase.branch=$(shell git rev-parse --abbrev-ref HEAD) \
--label online.woahbase.build-date=$(BUILDDATE) \
--label online.woahbase.build-number=$${BUILDNUMBER:-undefined} \
--label online.woahbase.source-image="$(if $(filter scratch,$(SRCIMAGE)),scratch,$(OPSYS)-$(SRCIMAGE):$(if $(SRCTAG),$(SRCTAG),$(ARCH)))" \
--label org.opencontainers.image.base.name="$(if $(filter scratch,$(SRCIMAGE)),scratch,docker.io/$(ORGNAME)/$(OPSYS)-$(SRCIMAGE):$(if $(SRCTAG),$(SRCTAG),$(ARCH)))" \
--label org.opencontainers.image.created=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") \
--label org.opencontainers.image.documentation="$(if $(DOC_URL),$(DOC_URL),https://woahbase.online/images)/$(REPONAME)" \
--label org.opencontainers.image.revision=$(shell git rev-parse --short HEAD) \
--label org.opencontainers.image.source="$(shell git config --get remote.origin.url)" \
--label org.opencontainers.image.title=$(REPONAME) \
--label org.opencontainers.image.url="$(if $(REGISTRY_URL),$(REGISTRY_URL),https://$(REGISTRY))/$(ORGNAME)/$(REPONAME)" \
--label org.opencontainers.image.vendor=$(ORGNAME) \
--label org.opencontainers.image.version=tagged \
# --label org.opencontainers.image.authors="$(shell git config --get user.name)($(shell git config --get user.email))" \
#
# all build-time flags combined here
BUILDFLAGS ?= \
$(CACHEFLAGS) \
$(LABELFLAGS) \
--build-arg IMAGEBASE=$(IMAGEBASE) \
--build-arg http_proxy=$(http_proxy) \
--build-arg https_proxy=$(https_proxy) \
--build-arg no_proxy=$(no_proxy) \
--compress \
--file $(CURDIR)/$(DOCKERFILE) \
--force-rm \
--rm \
--tag $(IMAGETAG) \
#
# following applies if and when using buildx
BUILDKITCFG := $(if $(wildcard $(CURDIR)/config.toml),--config $(CURDIR)/config.toml,)
BUILDKITIMG := $(REGISTRY)/moby/buildkit:latest
BUILDERFLAGS ?= \
$(BUILDKITCFG) \
$(call get_docker_platform) \
--driver docker-container \
--driver-opt "image=$(BUILDKITIMG)" \
#
# runtime flags
MOUNTFLAGS := \
-v $(CURDIR)/data:/data \
# -v /etc/hosts:/etc/hosts:ro \
# -v /etc/localtime:/etc/localtime:ro \
#
PORTFLAGS := \
-p 111:111/tcp -p 111:111/udp \
-p 2049:2049/tcp -p 2049:2049/udp \
-p 32765:32765/tcp -p 32765:32765/udp \
-p 32766:32766/tcp -p 32766:32766/udp \
-p 32767:32767/tcp -p 32767:32767/udp \
-p 32768:32768/tcp -p 32768:32768/udp \
# # or use host network
# --net=host \
# # so other local containers can find it without explicit linking,
# # needs firewall cleared
PUID := $(shell id -u)
PGID := $(shell id -g)# gid 100(users) usually pre exists
OTHERFLAGS := \
--hostname $(SVCNAME) \
--name $(CNTNAME) \
-c 256 \
-m 256m \
-e PGID=$(PGID) \
-e PUID=$(PUID) \
--privileged \
# -e SKIP_SYSCTL=1 \
# -e MOUNTD_ARGS="-F -N 2 -N 3 -p 32767 --debug all" \
# -e NFSD_ARGS="-N 3 --debug 8" \
# -e RPCBIND_ARGS="-fw" \
# -e STATD_ARGS="-p 32765 -o 32766 -F -d" \
# -e NFSMODE=SERVER \
# -e TZ=Asia/Kolkata \
#
# all runtime flags combined here
RUNFLAGS := \
$(MOUNTFLAGS) \
$(OTHERFLAGS) \
$(PORTFLAGS) \
#
# -- }}}
## ---
## Target : Depends :Description
## ---
all : run ## default target
# {{{ -- container targets
logs : ## show logs
docker logs -f $(CNTNAME)
restart : ## restart container
docker ps -a --format '{{.Names}}' \
| grep '$(CNTNAME)' -q \
&& docker restart $(CNTNAME) \
|| echo "Service not running.";
rm : ## remove container
docker rm -f $(CNTNAME)
run : ## run service
docker run --rm $(RUNFLAGS) $(IMAGETAG)
client : # test mount in another container in local
docker run --rm -it \
--hostname nfsclient --name docker_nfsclient \
-c 256 -m 256m \
-e PGID=$(PGID) -e PUID=$(PUID) \
--privileged \
--net=host \
-e NFSMODE=client \
-v $(CURDIR)/root/defaults/fstab.nfs:/etc/fstab.nfs:ro \
$(IMAGETAG) \
$(CNTSHELL) \
-ec \
'cat /etc/fstab.nfs >> /etc/fstab \
&& mount -av \
&& ls -a /mnt';
# refer: https://www.baeldung.com/linux/docker-mount-nfs-shares
client_volume : # test mount in another container in local
docker run --rm -it \
--hostname nfsclient --name docker_nfsclient \
--entrypoint /bin/bash \
-c 256 -m 256m \
-e PGID=$(PGID) -e PUID=$(PUID) \
--privileged \
--net=host \
-e NFSMODE=client \
--mount 'type=volume,source=nfsvolume,target=/mnt,volume-driver=local,volume-opt=type=nfs,"volume-opt=o=addr=127.0.0.1,rw,nfsvers=4,sync,hard",volume-opt=device=:/' \
$(IMAGETAG) \
# $(CNTSHELL) \
#
shell : ## start a shell
docker run --rm -it $(RUNFLAGS) --entrypoint $(CNTSHELL) $(IMAGETAG)
rdebug : ## shell into container as root
docker exec -u root -it $(CNTNAME) $(CNTSHELL)
debug : ## shell into container as user
docker exec -u $(PUID):$(PGID) -it $(CNTNAME) $(CNTSHELL)
stop : ## stop container
docker stop -t 2 $(CNTNAME)
test : regbinfmt
test : ## run test command, i.e. TESTCMD
if [ -z "$(SKIP_TEST_$(ARCH))" ] && [ -z "$(SKIP_TEST)" ] && [ -z "$(SKIP_$(ARCH))" ]; \
then \
docker run --rm -it --pull=never \
$(RUNFLAGS) \
--entrypoint $(CNTSHELL) \
$(IMAGETAG) \
-ec '$(TESTCMD)'; \
else \
echo "Skipping test: $(IMAGETAG)."; \
fi;
#
# -- }}}
# {{{ -- image targets
build : regbinfmt
build : BUILDX := $(shell docker buildx version 1>/dev/null 2>&1 && echo 'present' || echo 'absent')
build : ## build image
if [ -z "$(SKIP_$(ARCH))" ]; \
then \
if [ "X$(BUILDX)" = "Xpresent" ]; \
then \
echo "Build(X)ing for $(ARCH) on $(HOSTARCH)"; \
docker buildx create \
--name builder_$(SVCNAME) \
$(BUILDERFLAGS) \
--use; \
docker buildx build \
--load \
--builder builder_$(SVCNAME) \
--progress plain $(call get_docker_platform) \
$(BUILDFLAGS) \
.; \
docker buildx rm builder_$(SVCNAME); \
else \
echo "Building for $(ARCH) on $(HOSTARCH)"; \
docker build $(BUILDFLAGS) .; \
fi; \
else \
echo "Skipping build: $(IMAGETAG)."; \
fi;
clean : ## cleanup
docker images -a --format '{{.Repository}}:{{.Tag}}' \
| grep "$(ORGNAME)/$(REPONAME)" \
| xargs -r -n1 docker rmi
# cleanup additional stuff below, e.g. config or data
# pull image (optionally from a different source)
pull : REGSRC ?= $(REGISTRY)
pull : ORGSRC ?= $(ORGNAME)
pull : REPOSRC ?= $(REPONAME)
pull : TAGSRC ?= $(ARCH)
# or, specify only IMAGESRC
pull : IMAGESRC ?= $(REGSRC)/$(ORGSRC)/$(REPOSRC):$(TAGSRC)
# to force skip retag
# set SKIP_RETAG to non empty string e.g. 1,
# default is unset
# pull : SKIP_RETAG=
pull : ## pull image from source (could be different repository)
docker pull \
$(call get_docker_platform) \
$(IMAGESRC); \
if [ -z "$(SKIP_RETAG)" ] && [ "$(IMAGESRC)" != "$(IMAGETAG)" ];\
then \
echo "Re-Tagging: $(IMAGESRC) -> $(IMAGETAG)."; \
docker tag $(IMAGESRC) $(IMAGETAG) \
&& docker rmi -f $(IMAGESRC); \
else \
echo "Skipping retag: $(IMAGESRC)."; \
fi;
# push image with arch tag
# push image with version tag (if available)
# to skip pushing version set SKIP_VERSIONTAG to non empty string e.g. 1, default is unset if version given
push : SKIP_VERSIONTAG ?= $(if $(VERSION),,1)
push : VERSIONTAG ?= $(subst $(ARCH),$(ARCH)$(if $(VERSION),_$(VERSION),),$(IMAGETAG))
push : BUILDDATETAG ?= $(subst $(ARCH),$(ARCH)$(if $(VERSION),_$(VERSION),)_$(BUILDDATE),$(IMAGETAG))
push : ## push image
if [ -z "$(SKIP_$(ARCH))" ]; \
then \
docker push $(IMAGETAG); \
if [ -z "$(SKIP_VERSIONTAG)" ] && [ -n "$(VERSION)" ];\
then \
echo "Tagging $(VERSIONTAG)"; \
docker tag $(IMAGETAG) $(VERSIONTAG); \
docker push $(VERSIONTAG); \
else \
echo "Skipping push version tag: $(VERSIONTAG)."; \
fi; \
if [ -z "$(SKIP_BUILDDATETAG)" ]; then \
echo "Tagging $(BUILDDATETAG)"; \
docker tag $(IMAGETAG) $(BUILDDATETAG); \
docker push $(BUILDDATETAG); \
else \
echo "Skipping push builddate tag: $(BUILDDATETAG)."; \
fi; \
else \
echo "Skipping push: $(IMAGETAG)."; \
fi;
# push image with arch tag, optionally to a different registry
# push image with version tag (if available)
# to skip pushing version set SKIP_VERSIONTAG to non empty string e.g. 1, default is unset if version given
push_registry_% : SKIP_VERSIONTAG ?= $(if $(VERSION),,1)
push_registry_% : REGDSTTAG ?= $(subst $(REGISTRY),$(subst push_registry_,,$@),$(IMAGETAG))
push_registry_% : REGDSTVERSIONTAG ?= $(subst $(ARCH),$(ARCH)_$(VERSION),$(REGDSTTAG))
push_registry_% : REGDSTBUILDDATETAG ?= $(subst $(ARCH),$(ARCH)$(if $(VERSION),_$(VERSION),)_$(BUILDDATE),$(REGDSTTAG))
push_registry_% : ## push image to a different registry
if [ -z "$(SKIP_$(ARCH))" ]; \
then \
if [ "$(IMAGETAG)" != "$(REGDSTTAG)" ]; \
then \
echo "Tagging $(REGDSTTAG)"; \
docker tag $(IMAGETAG) $(REGDSTTAG); \
fi; \
docker push $(REGDSTTAG); \
if [ -z "$(SKIP_VERSIONTAG)" ] && [ -n "$(VERSION)" ];\
then \
echo "Tagging $(REGDSTVERSIONTAG)"; \
docker tag $(IMAGETAG) $(REGDSTVERSIONTAG); \
docker push $(REGDSTVERSIONTAG); \
else \
echo "Skipping push version tag: $(REGDSTVERSIONTAG)."; \
fi; \
if [ -z "$(SKIP_BUILDDATETAG)" ]; then \
echo "Tagging $(REGDSTBUILDDATETAG)"; \
docker tag $(IMAGETAG) $(REGDSTBUILDDATETAG); \
docker push $(REGDSTBUILDDATETAG); \
else \
echo "Skipping push builddate tag: $(REGDSTBUILDDATETAG)."; \
fi; \
else \
echo "Skipping push: $(REGDSTTAG)."; \
fi;
# to skip annotating, set SKIP_$(ARCH) to non-empty string, e.g. 1. default is unset.
# manifest: SKIP_x86_64=
# manifest: SKIP_aarch64=
# manifest: SKIP_armv7l=
# manifest: SKIP_armhf=
# if tagname != latest, use $(ARCH)_$(TAGNAME) to annotate, else just $(ARCH)
# manifest: TAGNAME = latest
manifest: ## create or update image(s) manifest
if [ -z "$(SKIP_ANNOTATE)" ]; \
then \
MANIFESTTAG=$(subst $(ARCH),$(TAGNAME),$(IMAGETAG)); \
docker manifest inspect $${MANIFESTTAG} > /dev/null 2>&1; \
if [ $$? != 0 ]; then docker manifest create \
$${MANIFESTTAG} \
$(if $(SKIP_x86_64),,$(subst $(ARCH),x86_64$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG))) \
$(if $(SKIP_aarch64),,$(subst $(ARCH),aarch64$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG))) \
$(if $(SKIP_armv7l),,$(subst $(ARCH),armv7l$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG))) \
$(if $(SKIP_armhf),,$(subst $(ARCH),armhf$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG))) \
;\
else docker manifest create --amend \
$${MANIFESTTAG} \
$(if $(SKIP_x86_64),,$(subst $(ARCH),x86_64$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG))) \
$(if $(SKIP_aarch64),,$(subst $(ARCH),aarch64$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG))) \
$(if $(SKIP_armv7l),,$(subst $(ARCH),armv7l$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG))) \
$(if $(SKIP_armhf),,$(subst $(ARCH),armhf$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG))) \
;\
fi; \
else \
echo "Skipping manifest: $(IMAGETAG)."; \
fi;
# to skip annotating, set SKIP_$(ARCH) to non-empty string, e.g. 1. default is unset.
# annotate: SKIP_x86_64=
# annotate: SKIP_aarch64=
# annotate: SKIP_armv7l=
# annotate: SKIP_armhf=
# if tagname != latest, use $(ARCH)_$(TAGNAME) to annotate, else just $(ARCH)
# annotate: TAGNAME = latest
annotate: ## annotate image(s) os/arch in manifest
if [ -z "$(SKIP_ANNOTATE)" ]; \
then \
MANIFESTTAG=$(subst $(ARCH),$(TAGNAME),$(IMAGETAG)); \
if [ -z "$(SKIP_x86_64)" ]; then \
docker manifest annotate $(call get_manifest_platform,x86_64) \
$${MANIFESTTAG} \
$(subst $(ARCH),x86_64$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG)); \
fi; \
if [ -z "$(SKIP_aarch64)" ]; then \
docker manifest annotate $(call get_manifest_platform,aarch64) \
$${MANIFESTTAG} \
$(subst $(ARCH),aarch64$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG)); \
fi; \
if [ -z "$(SKIP_armv7l)" ]; then \
docker manifest annotate $(call get_manifest_platform,armv7l) \
$${MANIFESTTAG} \
$(subst $(ARCH),armv7l$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG)); \
fi; \
if [ -z "$(SKIP_armhf)" ]; then \
docker manifest annotate $(call get_manifest_platform,armhf) \
$${MANIFESTTAG} \
$(subst $(ARCH),armhf$(if $(subst latest,,$(TAGNAME)),_$(TAGNAME),),$(IMAGETAG)); \
fi; \
docker manifest push -p $${MANIFESTTAG}; \
else \
echo "Skipping annotate: $(IMAGETAG)."; \
fi;
annotate_latest : SKIP_ANNOTATE ?= $(if $(SKIP_LATESTTAG),1,)
annotate_latest : TAGNAME = latest
annotate_latest : manifest annotate ## annotate and push `latest` tag
annotate_version : SKIP_ANNOTATE ?= $(if $(SKIP_VERSIONTAG),1,)
annotate_version : TAGNAME ?= $(if $(VERSION),$(VERSION),$(error VERSION is not defined))
annotate_version : manifest annotate ## annotate and push `VERSION` tag
annotate_date : SKIP_ANNOTATE ?= $(if $(SKIP_BUILDDATETAG),1,)
annotate_date : TAGNAME ?= $(if $(VERSION),$(VERSION)_,$(error VERSION is not defined))$(BUILDDATE)
annotate_date : manifest annotate ## annotate and push `BUILDDATE` tag
# -- }}}
# {{{ -- other targets
regbinfmt : QEMUIMAGE ?= $(REGISTRY)/multiarch/qemu-user-static
regbinfmt : ## register binfmt for multiarch on x86_64
if [ "$(ARCH)" != "$(HOSTARCH)" ]; then \
docker run --rm --privileged $(QEMUIMAGE) --reset -p yes; \
fi;
#
help : ## show this help
@sed -ne '/@sed/!s/## /|/p' $(MAKEFILE_LIST) | sed -e's/\W*:\W*=/:/g' | column -et -c 3 -s ':|?=' #| sort -h
# -- }}}
# {{{ -- functions
# maps os platform to docker tags when building on $(HOSTARCH) for $(ARCH)
OS_PLATFORM_MAP := \
'aarch64') echo -n 'aarch64';; \
'armv6l' ) echo -n 'armhf' ;; \
'armv7l' ) echo -n 'armv7l' ;; \
'x86_64' ) echo -n 'x86_64' ;; \
#
define get_os_platform
$(shell case "$$(uname -m)" in $(OS_PLATFORM_MAP) esac)
endef
# sets docker platform when building for $(ARCH)
DOCKER_PLATFORM_MAP := \
'aarch64') echo -n '--platform linux/arm64' ;; \
'armhf' ) echo -n '--platform linux/arm/v6';; \
'armv7l' ) echo -n '--platform linux/arm/v7';; \
'x86_64' ) echo -n '--platform linux/amd64' ;; \
#
define get_docker_platform
$(shell case "$(ARCH)" in $(DOCKER_PLATFORM_MAP) esac)
endef
# sets docker manifest annotation when building for $(ARCH)
MANIFEST_PLATFORM_MAP := \
'aarch64') echo -n '--os linux --arch arm64';; \
'armhf' ) echo -n '--os linux --arch arm --variant v6';; \
'armv7l' ) echo -n '--os linux --arch arm --variant v7';; \
'x86_64' ) echo -n '--os linux --arch amd64';; \
#
# $1 = ARCH
define get_manifest_platform
$(shell case "$(1)" in $(MANIFEST_PLATFORM_MAP) esac)
endef
# gets installed version string from built image
define get_svc_version
$(shell docker run --rm --pull=never \
--entrypoint rpc.mountd \
$(IMAGETAG) \
-v \
2>&1 \
| tail -n1 | sed -e 's_rpc.mountd version __')
endef
# -- }}}