-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
118 lines (97 loc) · 4.59 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
# --------------------------------------------------------------------
# Copyright (c) 2019 LINKIT, The Netherlands. All Rights Reserved.
# Author(s): Anthony Potappel
#
# This software may be modified and distributed under the terms of the
# MIT license. See the LICENSE file for details.
# --------------------------------------------------------------------
# If you see pwd_unknown showing up, this is why. Re-calibrate your system.
PWD ?= pwd_unknown
# PROJECT_NAME defaults to name of the current directory.
# should not to be changed if you follow GitOps operating procedures.
PROJECT_NAME = $(notdir $(PWD))
# Note. If you change this, you also need to update docker-compose.yml.
# only useful in a setting with multiple services/ makefiles.
SERVICE_TARGET := main
# if vars not set specifially: try default to environment, else fixed value.
# strip to ensure spaces are removed in future editorial mistakes.
# tested to work consistently on popular Linux flavors and Mac.
ifeq ($(user),)
# USER retrieved from env, UID from shell.
HOST_USER ?= $(strip $(if $(USER),$(USER),nodummy))
HOST_UID ?= $(strip $(if $(shell id -u),$(shell id -u),4000))
else
# allow override by adding user= and/ or uid= (lowercase!).
# uid= defaults to 0 if user= set (i.e. root).
HOST_USER = $(user)
HOST_UID = $(strip $(if $(uid),$(uid),0))
endif
THIS_FILE := $(lastword $(MAKEFILE_LIST))
CMD_ARGUMENTS ?= $(cmd)
RUN_DOCK = docker-compose -p $(PROJECT_NAME)_$(HOST_UID) run --rm $(SERVICE_TARGET) sh -l -c
# export such that its passed to shell functions for Docker to pick up.
export PROJECT_NAME
export HOST_USER
export HOST_UID
# all our targets are phony (no files to check).
.PHONY: shell help docker build publish rebuild service login test clean prune
# suppress makes own output
#.SILENT:
# shell is the first target. So instead of: make shell cmd="whoami", we can type: make cmd="whoami".
# more examples: make shell cmd="whoami && env", make shell cmd="echo hello container space".
# leave the double quotes to prevent commands overflowing in makefile (things like && would break)
# special chars: '',"",|,&&,||,*,^,[], should all work. Except "$" and "`", if someone knows how, please let me know!).
# escaping (\) does work on most chars, except double quotes (if someone knows how, please let me know)
# i.e. works on most cases. For everything else perhaps more useful to upload a script and execute that.
shell:
ifeq ($(CMD_ARGUMENTS),)
# no command is given, default to shell
docker-compose -p $(PROJECT_NAME)_$(HOST_UID) run --rm $(SERVICE_TARGET) sh
else
# run the command
docker-compose -p $(PROJECT_NAME)_$(HOST_UID) run --rm $(SERVICE_TARGET) sh -c "$(CMD_ARGUMENTS)"
endif
# Regular Makefile part for buildpypi itself
help:
@echo ''
@echo 'Usage: make [TARGET] [EXTRA_ARGUMENTS]'
@echo 'Targets:'
@echo ' build build docker --image-- for current user: $(HOST_USER)(uid=$(HOST_UID))'
@echo ' rebuild rebuild docker --image-- for current user: $(HOST_USER)(uid=$(HOST_UID))'
@echo ' test test docker --container-- for current user: $(HOST_USER)(uid=$(HOST_UID))'
@echo ' service run as service --container-- for current user: $(HOST_USER)(uid=$(HOST_UID))'
@echo ' login run as service and login --container-- for current user: $(HOST_USER)(uid=$(HOST_UID))'
@echo ' clean remove docker --image-- for current user: $(HOST_USER)(uid=$(HOST_UID))'
@echo ' prune shortcut for docker system prune -af. Cleanup inactive containers and cache.'
@echo ' shell run docker --container-- for current user: $(HOST_USER)(uid=$(HOST_UID))'
@echo ''
@echo 'Extra arguments:'
@echo 'cmd=: make cmd="whoami"'
@echo '# user= and uid= allows to override current user. Might require additional privileges.'
@echo 'user=: make shell user=root (no need to set uid=0)'
@echo 'uid=: make shell user=dummy uid=4000 (defaults to 0 if user= set)'
docker:
docker-compose build $(SERVICE_TARGET)
rebuild:
# force a rebuild by passing --no-cache
docker-compose build --no-cache $(SERVICE_TARGET)
service:
# run as a (background) service
docker-compose -p $(PROJECT_NAME)_$(HOST_UID) up -d $(SERVICE_TARGET)
login: service
# run as a service and attach to it
docker exec -it $(PROJECT_NAME)_$(HOST_UID) sh
build:
# only build the container. Note, docker does this also if you apply other targets.
#docker-compose build $(SERVICE_TARGET)
$(RUN_DOCK) "python3 setup.py sdist bdist_wheel"
publish:
$(RUN_DOCK) "twine upload dist/*"
clean:
rm -rf ./build ./dist ./*.egg-info
prune:
# clean all that is not actively used
docker system prune -af
lint:
$(RUN_DOCK) "pylint --rcfile=.pylintrc $(PROJECT_NAME)/* -f parseable"
test: lint