This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
416 lines (327 loc) · 14.8 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
#
#
#
.DEFAULT_GOAL := help
SHELL := /bin/bash
MAKE_C := $(MAKE) --no-print-directory --directory
# Operating system
ifeq ($(filter Windows_NT,$(OS)),)
IS_WSL := $(if $(findstring Microsoft,$(shell uname -a)),WSL,)
IS_WSL2 := $(if $(findstring -microsoft-,$(shell uname -a)),WSL2,)
IS_OSX := $(filter Darwin,$(shell uname -a))
IS_LINUX:= $(if $(or $(IS_WSL),$(IS_OSX)),,$(filter Linux,$(shell uname -a)))
endif
IS_WIN := $(strip $(if $(or $(IS_LINUX),$(IS_OSX),$(IS_WSL)),,$(OS)))
$(if $(IS_WIN),$(error Windows is not supported in all recipes. Use WSL2 instead. Follow instructions in README.md),)
makefile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
makefile_dir := $(patsubst %/,%,$(dir $(makefile_path)))
SERVICES_LIST := $(notdir $(subst /Dockerfile,,$(wildcard services/*/Dockerfile)))
# version control
export VCS_URL := $(shell git config --get remote.origin.url)
export VCS_REF := $(shell git rev-parse --short HEAD)
export VCS_STATUS_CLIENT:= $(if $(shell git status -s),'modified/untracked','clean')
export BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
# swarm stacks
export SWARM_STACK_NAME ?= dask-gateway
# version tags
export DOCKER_IMAGE_TAG ?= latest
export DOCKER_REGISTRY ?= itisfoundation
get_my_ip := $(shell hostname --all-ip-addresses | cut --delimiter=" " --fields=1)
# osparc-dask-gateway configuration file
export OSPARC_GATEWAY_CONFIG_FILE_HOST = .osparc-dask-gateway-config.py
.PHONY: help
help: ## help on rule's targets
@awk --posix 'BEGIN {FS = ":.*?## "} /^[[:alpha:][:space:]_-]+:.*?## / {printf "\033[36m%-25s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
## DOCKER BUILD -------------------------------
#
# - all builds are inmediatly tagged as 'local/{service}:${BUILD_TARGET}' where BUILD_TARGET='development', 'production', 'cache'
# - only production and cache images are released (i.e. tagged pushed into registry)
#
SWARM_HOSTS = $(shell docker node ls --format="{{.Hostname}}" 2>$(if $(IS_WIN),NUL,/dev/null))
.PHONY: build build-nc rebuild build-devel build-devel-nc
# define _docker_compose_build
# export BUILD_TARGET=$(if $(findstring -devel,$@),development,production);\
# pushd services && docker buildx bake --file docker-compose-build.yml $(if $(target),$(target),) && popd;
# endef
# docker buildx cache location
DOCKER_BUILDX_CACHE_FROM ?= /tmp/.buildx-cache
DOCKER_BUILDX_CACHE_TO ?= /tmp/.buildx-cache
DOCKER_TARGET_PLATFORMS ?= linux/amd64
comma := ,
define _docker_compose_build
$(if $(create_cache),$(info creating cache file in $(DOCKER_BUILDX_CACHE_TO)),)
export BUILD_TARGET=$(if $(findstring -devel,$@),development,production);\
pushd services &&\
$(foreach service, $(SERVICES_LIST),\
$(if $(push),\
export $(subst -,_,$(shell echo $(service) | tr a-z A-Z))_VERSION=$(shell cat services/$(service)/VERSION);\
,) \
)\
docker buildx bake \
$(if $(findstring -devel,$@),,\
--set *.platform=$(DOCKER_TARGET_PLATFORMS) \
$(foreach service, $(SERVICES_LIST),\
--set $(service).cache-from="type=local,src=$(DOCKER_BUILDX_CACHE_FROM)/$(service)" \
$(if $(create_cache),--set $(service).cache-to="type=local$(comma)mode=max$(comma)dest=$(DOCKER_BUILDX_CACHE_TO)/$(service)",) \
)\
)\
$(if $(findstring $(comma),$(DOCKER_TARGET_PLATFORMS)),,--set *.output="type=docker$(comma)push=false") \
$(if $(push),--push,) \
$(if $(push),--file docker-bake.hcl,) --file docker-compose-build.yml $(if $(target),$(target),) &&\
popd
endef
rebuild: build-nc # alias
build build-nc: .env ## Builds production images and tags them as 'local/{service-name}:production'. For single target e.g. 'make target=osparc-$(SWARM_STACK_NAME) build'
ifeq ($(target),)
# Building services
@$(_docker_compose_build)
else
# Building service $(target)
@$(_docker_compose_build)
endif
build-devel build-devel-nc: .env ## Builds development images and tags them as 'local/{service-name}:development'. For single target e.g. 'make target=osparc-$(SWARM_STACK_NAME) build-devel'
ifeq ($(target),)
# Building services
@$(_docker_compose_build)
else
# Building service $(target)
@@$(_docker_compose_build)
endif
.PHONY: shell
shell:
docker run -it local/$(target):production /bin/sh
## DOCKER SWARM -------------------------------
#
# - All resolved configuration are named as .stack-${name}-*.yml to distinguish from docker-compose files which can be parametrized
#
SWARM_HOSTS = $(shell docker node ls --format="{{.Hostname}}" 2>$(if $(IS_WIN),null,/dev/null))
docker-compose-configs = $(wildcard services/docker-compose*.yml)
$(OSPARC_GATEWAY_CONFIG_FILE_HOST): services/osparc-gateway-server/config/default_config.py ## creates .env file from defaults in .env-devel
$(if $(wildcard $@), \
@echo "WARNING ##### $< is newer than $@ ####"; diff -uN $@ $<; false;,\
@echo "WARNING ##### $@ does not exist, cloning $< as $@ ############"; cp $< $@)
# REFERENCE: https://github.com/docker/compose/issues/9306
# composeV2 defines specifications for docker compose to run
# they are not 100% compatible with what docker stack deploy command expects
# some parts have to be modified
define generate_docker_compose_specs
./scripts/docker-compose-config.bash $1 > $@
endef
.stack-$(SWARM_STACK_NAME)-development.yml: .env $(docker-compose-configs)
# Creating config for stack with 'local/{service}:development' to $@
@export DOCKER_REGISTRY=local \
export DOCKER_IMAGE_TAG=development; \
$(call generate_docker_compose_specs,\
services/docker-compose.yml \
services/docker-compose.local.yml \
services/docker-compose.devel.yml\
)
.stack-$(SWARM_STACK_NAME)-production.yml: .env $(docker-compose-configs)
# Creating config for stack with 'local/{service}:production' to $@
@export DOCKER_REGISTRY=local; \
export DOCKER_IMAGE_TAG=production; \
$(call generate_docker_compose_specs,\
services/docker-compose.yml \
services/docker-compose.local.yml\
)
.stack-$(SWARM_STACK_NAME)-version.yml: .env $(docker-compose-configs)
# Creating config for stack with '$(DOCKER_REGISTRY)/{service}:${DOCKER_IMAGE_TAG}' to $@
@$(call generate_docker_compose_specs,\
services/docker-compose.yml \
services/docker-compose.local.yml\
)
.stack-$(SWARM_STACK_NAME)-ops.yml: .env $(docker-compose-configs)
# Creating config for ops stack to $@
@$(call generate_docker_compose_specs,\
services/docker-compose-ops.yml)
.PHONY: up-devel up-prod up-version up-latest
.deploy-ops: .stack-$(SWARM_STACK_NAME)-ops.yml
# Deploy stack 'ops'
ifndef ops_disabled
@docker stack deploy --with-registry-auth --compose-file $< $(SWARM_STACK_NAME)-ops
else
@echo "Explicitly disabled with ops_disabled flag in CLI"
endif
define _show_endpoints
# The following endpoints are available
set -o allexport; \
source $(CURDIR)/.env; \
set +o allexport; \
separator=------------------------------------------------------------------------------------;\
separator=$${separator}$${separator}$${separator};\
rows="%-22s | %40s | %12s | %12s\n";\
TableWidth=100;\
printf "%22s | %40s | %12s | %12s\n" Name Endpoint User Password;\
printf "%.$${TableWidth}s\n" "$$separator";\
$(if $(shell docker stack ps ${SWARM_STACK_NAME}-ops 2>/dev/null), \
printf "$$rows" Portainer 'http://$(get_my_ip):9000' admin adminadmin;,)\
printf "$$rows" Dask-Gateway 'http://$(get_my_ip):8000' whatever $(filter-out %.password =,$(shell cat $(OSPARC_GATEWAY_CONFIG_FILE_HOST) | grep c.Authenticator.password));
endef
show-endpoints:
@$(_show_endpoints)
up-devel: .stack-$(SWARM_STACK_NAME)-development.yml .init-swarm config ## Deploys local development stack and ops stack (pass 'make ops_disabled=1 up-...' to disable)
# Deploy stack $(SWARM_STACK_NAME) [back-end]
@docker stack deploy --with-registry-auth -c $< $(SWARM_STACK_NAME)
@$(MAKE) .deploy-ops
@$(_show_endpoints)
up-prod: .stack-$(SWARM_STACK_NAME)-production.yml .init-swarm config ## Deploys local production stack and ops stack (pass 'make ops_disabled=1 up-...' to disable)
ifeq ($(target),)
# Deploy stack $(SWARM_STACK_NAME)
@docker stack deploy --with-registry-auth -c $< $(SWARM_STACK_NAME)
@$(MAKE) .deploy-ops
else
# deploys ONLY $(target) service
@docker compose --file $< up --detach $(target)
endif
@$(_show_endpoints)
up up-version: .stack-$(SWARM_STACK_NAME)-version.yml .init-swarm config ## Deploys versioned stack '$(DOCKER_REGISTRY)/{service}:$(DOCKER_IMAGE_TAG)' and ops stack (pass 'make ops_disabled=1 up-...' to disable)
# Deploy stack $(SWARM_STACK_NAME)
@docker stack deploy --with-registry-auth -c $< $(SWARM_STACK_NAME)
@$(_show_endpoints)
up-latest:
@export DOCKER_IMAGE_TAG=latest; \
$(MAKE) up-version
.PHONY: down leave
down: ## Stops and removes stack
# Removing stacks in reverse order to creation
-@docker stack rm $(SWARM_STACK_NAME)
-@docker stack rm $(SWARM_STACK_NAME)-ops
# Removing generated docker compose configurations, i.e. .stack-*
-@rm $(wildcard .stack-*)
-@rm $(wildcard $(OSPARC_GATEWAY_CONFIG_FILE_HOST))
leave: ## Forces to stop all services, networks, etc by the node leaving the swarm
-docker swarm leave -f
PHONY: .init-swarm
.init-swarm:
# Ensures swarm is initialized
$(if $(SWARM_HOSTS),,docker swarm init --advertise-addr=$(get_my_ip))
## DOCKER TAGS -------------------------------
.PHONY: tag-local tag-version tag-latest
tag-local: ## Tags version '${DOCKER_REGISTRY}/{service}:${DOCKER_IMAGE_TAG}' images as 'local/{service}:production'
# Tagging all '${DOCKER_REGISTRY}/{service}:${DOCKER_IMAGE_TAG}' as 'local/{service}:production'
@$(foreach service, $(SERVICES_LIST)\
,docker tag ${DOCKER_REGISTRY}/$(service):${DOCKER_IMAGE_TAG} local/$(service):production; \
)
tag-version: ## Tags 'local/{service}:production' images as versioned '${DOCKER_REGISTRY}/{service}:${DOCKER_IMAGE_TAG}'
# Tagging all 'local/{service}:production' as '${DOCKER_REGISTRY}/{service}:${DOCKER_IMAGE_TAG}'
@$(foreach service, $(SERVICES_LIST)\
,docker tag local/$(service):production ${DOCKER_REGISTRY}/$(service):${DOCKER_IMAGE_TAG}; \
)
tag-latest: ## Tags last locally built production images as '${DOCKER_REGISTRY}/{service}:latest'
@export DOCKER_IMAGE_TAG=latest; \
$(MAKE) tag-version
## DOCKER PULL/PUSH -------------------------------
#
# TODO: cannot push modified/untracked
# TODO: cannot push disceted
#
.PHONY: pull-version
pull-version: .env ## pulls images from DOCKER_REGISTRY tagged as DOCKER_IMAGE_TAG
# Pulling images '${DOCKER_REGISTRY}/{service}:${DOCKER_IMAGE_TAG}'
@docker compose --file services/docker-compose-deploy.yml pull
.PHONY: push-version push-latest
push-latest: tag-latest
@export DOCKER_IMAGE_TAG=latest; \
$(MAKE) push-version
# below BUILD_TARGET gets overwritten but is required when merging yaml files
push-version: tag-version
# pushing '${DOCKER_REGISTRY}/{service}:${DOCKER_IMAGE_TAG}'
@export BUILD_TARGET=undefined; \
docker compose --file services/docker-compose-build.yml --file services/docker-compose-deploy.yml push
## ENVIRONMENT -------------------------------
.PHONY: devenv
.venv:
python3 -m venv $@
$@/bin/pip3 --quiet install --upgrade \
pip~=23.0 \
wheel \
setuptools
devenv: .venv ## create a python virtual environment with dev tools (e.g. linters, etc)
$</bin/pip3 --quiet install -r requirements/devenv.txt
# Installing pre-commit hooks in current .git repo
@$</bin/pre-commit install
@echo "To activate the venv, execute 'source .venv/bin/activate'"
.env: .env-devel ## creates .env file from defaults in .env-devel
$(if $(wildcard $@), \
@echo "WARNING ##### $< is newer than $@ ####"; diff -uN $@ $<; false;,\
@echo "WARNING ##### $@ does not exist, cloning $< as $@ ############"; cp $< $@)
.vscode/settings.json: .vscode-template/settings.json
$(info WARNING: ##### $< is newer than $@ ####)
@diff -uN $@ $<
@false
.PHONY: info info-images info-swarm info-tools
info: ## displays setup information
# setup info:
@echo ' Detected OS : $(IS_LINUX)$(IS_OSX)$(IS_WSL)$(IS_WSL2)$(IS_WIN)'
@echo ' SWARM_STACK_NAME : ${SWARM_STACK_NAME}'
@echo ' DOCKER_REGISTRY : $(DOCKER_REGISTRY)'
@echo ' DOCKER_IMAGE_TAG : ${DOCKER_IMAGE_TAG}'
@echo ' BUILD_DATE : ${BUILD_DATE}'
@echo ' VCS_* '
@echo ' - ULR : ${VCS_URL}'
@echo ' - REF : ${VCS_REF}'
# dev tools version
@echo ' make : $(shell make --version 2>&1 | head -n 1)'
@echo ' jq : $(shell jq --version)'
@echo ' awk : $(shell awk -W version 2>&1 | head -n 1)'
@echo ' python : $(shell python3 --version)'
@echo ' node : $(shell node --version 2> /dev/null || echo ERROR nodejs missing)'
@echo ' docker : $(shell docker --version)'
@echo ' docker buildx : $(shell docker buildx version)'
@echo ' docker-compose: $(shell docker compose --version)'
define show-meta
$(foreach iid,$(shell docker images "*/$(1):*" -q | sort | uniq),\
docker image inspect $(iid) | jq '.[0] | .RepoTags, .Config.Labels, .Architecture';)
endef
info-images: ## lists tags and labels of built images. To display one: 'make target=webserver info-images'
ifeq ($(target),)
@$(foreach service,$(SERVICES_LIST),\
echo "## $(service) images:";\
docker images */$(service):*;\
$(call show-meta,$(service))\
)
else
## $(target) images:
@$(call show-meta,$(target))
endif
info-swarm: ## displays info about stacks and networks
ifneq ($(SWARM_HOSTS), )
# Stacks in swarm
@docker stack ls
# Containers (tasks) running in '$(SWARM_STACK_NAME)' stack
-@docker stack ps $(SWARM_STACK_NAME)
# Services in '$(SWARM_STACK_NAME)' stack
-@docker stack services $(SWARM_STACK_NAME)
# Networks
@docker network ls
endif
## CLEAN -------------------------------
.PHONY: clean clean-images clean-venv clean-all clean-more
_git_clean_args := -dx --force --exclude=.vscode --exclude=TODO.md --exclude=.venv --exclude=.python-version --exclude=*keep*
_running_containers = $(shell docker ps -aq)
.check-clean:
@git clean -n $(_git_clean_args)
@echo -n "Are you sure? [y/N] " && read ans && [ $${ans:-N} = y ]
@echo -n "$(shell whoami), are you REALLY sure? [y/N] " && read ans && [ $${ans:-N} = y ]
clean-venv: devenv ## Purges .venv into original configuration
# Cleaning your venv
.venv/bin/pip-sync --quiet $(CURDIR)/requirements/devenv.txt
@pip list
clean-hooks: ## Uninstalls git pre-commit hooks
@-pre-commit uninstall 2> /dev/null || rm .git/hooks/pre-commit
clean: .check-clean ## cleans all unversioned files in project and temp files create by this makefile
# Cleaning unversioned
@git clean $(_git_clean_args)
clean-more: ## cleans containers and unused volumes
# stops and deletes running containers
@$(if $(_running_containers), docker rm --force $(_running_containers),)
# pruning unused volumes
docker volume prune --force
clean-all: clean clean-more clean-images clean-hooks # Deep clean including .venv and produced images
-rm -rf .venv
.PHONY: reset
reset: ## restart docker daemon (LINUX ONLY)
sudo systemctl restart docker
## Initial config
.PHONY: config
config: $(OSPARC_GATEWAY_CONFIG_FILE_HOST) ## create configuration file