Skip to content

Commit 69290ee

Browse files
authored
Merge pull request nicolargo#2910 from ariel-anieli/makefile
Refactorized `Makefile` by using pattern rules
2 parents e40aa05 + f5c2d8c commit 69290ee

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed

Makefile

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@ VENV_MIN := venv-min/bin
55
CONF := conf/glances.conf
66
PIP := $(VENV)/pip
77
PYTHON := $(VENV)/python
8-
UNITTEST := unittest
9-
8+
LASTTAG = $(shell git describe --tags --abbrev=0)
9+
10+
IMAGES_TYPES := full minimal dev
11+
ALPINE_IMAGES := $(IMAGES_TYPES:%=docker-alpine-%)
12+
UBUNTU_IMAGES := $(IMAGES_TYPES:%=docker-ubuntu-%)
13+
DOCKER_IMAGES := $(ALPINE_IMAGES) $(UBUNTU_IMAGES)
14+
DOCKER_RUNTIMES := $(DOCKER_IMAGES:%=run-%)
15+
UNIT_TESTS := test-core test-restful test-xmlrpc
1016
DOCKER_BUILD := docker buildx build
1117
DOCKER_RUN := docker run
12-
DOCKERFILE_UBUNTU := docker-files/ubuntu.Dockerfile
13-
DOCKERFILE_ALPINE := docker-files/alpine.Dockerfile
1418
PODMAN_SOCK ?= /run/user/$(shell id -u)/podman/podman.sock
1519
DOCKER_SOCK ?= /var/run/docker.sock
1620
DOCKER_SOCKS := -v $(PODMAN_SOCK):$(PODMAN_SOCK):ro -v $(DOCKER_SOCK):$(DOCKER_SOCK):ro
1721
DOCKER_OPTS := --rm -e TZ="${TZ}" -e GLANCES_OPT="" --pid host --network host
1822

19-
LASTTAG = $(shell git describe --tags --abbrev=0)
23+
define DOCKER_TAG
24+
glances:local-$*
25+
endef
2026

2127
# if the command is only `make`, the default tasks will be the printing of the help.
2228
.DEFAULT_GOAL := help
@@ -86,24 +92,22 @@ venv-dev-upgrade: ## Upgrade Python 3 dev dependencies
8692
# Tests
8793
# ===================================================================
8894

89-
test-core: ## Run core unit tests
90-
$(PYTHON) $(UNITTEST)-core.py
95+
$(UNIT_TESTS): test-%: unittest-%.py
96+
$(PYTHON) $<
9197

98+
test-core: ## Run core unit tests
9299
test-restful: ## Run Restful unit tests
93-
$(PYTHON) $(UNITTEST)-restful.py
94-
95100
test-xmlrpc: ## Run XMLRPC unit tests
96-
$(PYTHON) $(UNITTEST)-xmlrpc.py
97101

98-
test: test-core test-restful test-xmlrpc ## Run unit tests
102+
test: $(UNIT_TESTS) ## Run unit tests
99103

100104
test-with-upgrade: venv-upgrade venv-dev-upgrade test ## Upgrade deps and run unit tests
101105

102106
test-min: ## Run core unit tests in minimal environment
103-
$(VENV_MIN)/python $(UNITTEST)-core.py
107+
$(VENV_MIN)/python unittest-core.py
104108

105109
test-min-with-upgrade: venv-min-upgrade ## Upgrade deps and run unit tests in minimal environment
106-
$(VENV_MIN)/python $(UNITTEST)-core.py
110+
$(VENV_MIN)/python unittest-core.py
107111

108112
# ===================================================================
109113
# Linters, profilers and cyber security
@@ -219,29 +223,28 @@ snapcraft:
219223
# Need Docker Buildx package (apt install docker-buildx on Ubuntu)
220224
# ===================================================================
221225

226+
define DOCKERFILE
227+
docker-files/$(word 1,$(subst -, ,$*)).Dockerfile
228+
endef
229+
230+
define TARGET
231+
$(word 2,$(subst -, ,$*))
232+
endef
233+
234+
$(DOCKER_IMAGES): docker-%:
235+
$(DOCKER_BUILD) --target $(TARGET) -f $(DOCKERFILE) -t $(DOCKER_TAG) .
236+
222237
docker: docker-alpine docker-ubuntu ## Generate local docker images
223238

224-
docker-alpine: docker-alpine-full docker-alpine-minimal docker-alpine-dev ## Generate local docker images (Alpine)
239+
docker-alpine: $(ALPINE_IMAGES) ## Generate local docker images (Alpine)
240+
docker-ubuntu: $(UBUNTU_IMAGES) ## Generate local docker images (Ubuntu)
225241

226242
docker-alpine-full: ## Generate local docker image (Alpine full)
227-
$(DOCKER_BUILD) --target full -f $(DOCKERFILE_ALPINE) -t glances:local-alpine-full .
228-
229243
docker-alpine-minimal: ## Generate local docker image (Alpine minimal)
230-
$(DOCKER_BUILD) --target minimal -f $(DOCKERFILE_ALPINE) -t glances:local-alpine-minimal .
231-
232244
docker-alpine-dev: ## Generate local docker image (Alpine dev)
233-
$(DOCKER_BUILD) --target dev -f $(DOCKERFILE_ALPINE) -t glances:local-alpine-dev .
234-
235-
docker-ubuntu: docker-ubuntu-full docker-ubuntu-minimal docker-ubuntu-dev ## Generate local docker images (Ubuntu)
236-
237245
docker-ubuntu-full: ## Generate local docker image (Ubuntu full)
238-
$(DOCKER_BUILD) --target full -f $(DOCKERFILE_UBUNTU) -t glances:local-ubuntu-full .
239-
240246
docker-ubuntu-minimal: ## Generate local docker image (Ubuntu minimal)
241-
$(DOCKER_BUILD) --target minimal -f $(DOCKERFILE_UBUNTU) -t glances:local-ubuntu-minimal .
242-
243247
docker-ubuntu-dev: ## Generate local docker image (Ubuntu dev)
244-
$(DOCKER_BUILD) --target dev -f $(DOCKERFILE_UBUNTU) -t glances:local-ubuntu-dev .
245248

246249
# ===================================================================
247250
# Run
@@ -268,23 +271,15 @@ run-min-debug: ## Start minimal Glances in debug console mode (also called stand
268271
run-min-local-conf: ## Start minimal Glances in console mode with the system conf file
269272
$(VENV_MIN)/python -m glances
270273

271-
run-docker-alpine-minimal: ## Start Glances Alpine Docker minimal in console mode
272-
$(DOCKER_RUN) $(DOCKER_OPTS) $(DOCKER_SOCKS) -it glances:local-alpine-minimal
274+
$(DOCKER_RUNTIMES): run-docker-%:
275+
$(DOCKER_RUN) $(DOCKER_OPTS) $(DOCKER_SOCKS) -it $(DOCKER_TAG)
273276

277+
run-docker-alpine-minimal: ## Start Glances Alpine Docker minimal in console mode
274278
run-docker-alpine-full: ## Start Glances Alpine Docker full in console mode
275-
$(DOCKER_RUN) $(DOCKER_OPTS) $(DOCKER_SOCKS) -it glances:local-alpine-full
276-
277279
run-docker-alpine-dev: ## Start Glances Alpine Docker dev in console mode
278-
$(DOCKER_RUN) $(DOCKER_OPTS) $(DOCKER_SOCKS) -it glances:local-alpine-dev
279-
280280
run-docker-ubuntu-minimal: ## Start Glances Ubuntu Docker minimal in console mode
281-
$(DOCKER_RUN) $(DOCKER_OPTS) $(DOCKER_SOCKS) -it glances:local-ubuntu-minimal
282-
283281
run-docker-ubuntu-full: ## Start Glances Ubuntu Docker full in console mode
284-
$(DOCKER_RUN) $(DOCKER_OPTS) $(DOCKER_SOCKS) -it glances:local-ubuntu-full
285-
286282
run-docker-ubuntu-dev: ## Start Glances Ubuntu Docker dev in console mode
287-
$(DOCKER_RUN) $(DOCKER_OPTS) $(DOCKER_SOCKS) -it glances:local-ubuntu-dev
288283

289284
run-webserver: ## Start Glances in Web server mode
290285
$(PYTHON) -m glances -C $(CONF) -w

0 commit comments

Comments
 (0)