-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
123 lines (100 loc) · 4.41 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
# --------------------------------------------------------------------
# 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)
URL ?= $(url)
# cli prefix for commands to run in container
RUN_DOCK = docker-compose -p $(PROJECT_NAME)_$(HOST_UID) run --rm $(SERVICE_TARGET) sh -l -c
# terraform variables
#WORKSPACE ?= $(strip $(if $(workspace),$(workspace),dev))
# 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 test build rebuild git remote plan infra destroy clean
# 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 -l
else
# run the command
docker-compose -p $(PROJECT_NAME)_$(HOST_UID) run --rm $(SERVICE_TARGET) sh -l -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 ' clean remove docker --image-- for current user: $(HOST_USER)(uid=$(HOST_UID))'
@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)'
rebuild:
# force a rebuild by passing --no-cache
docker-compose build --no-cache $(SERVICE_TARGET)
build:
docker-compose build $(SERVICE_TARGET)
git:
$(RUN_DOCK) "GIT_URL=\"$(URL)\" makegit"
infra:
$(RUN_DOCK) "remotestate"
$(RUN_DOCK) "terraform init \
&& terraform plan \
&& terraform apply -auto-approve \
&& terraform output"
remote:
$(RUN_DOCK) "remotestate"
plan:
$(RUN_DOCK) "terraform init && terraform plan"
destroy:
$(RUN_DOCK) "terraform destroy -auto-approve"
clean_build:
@rm -rf build/*
clean: clean_build
test:
# here it is useful to add your own customised tests
docker-compose -p $(PROJECT_NAME)_$(HOST_UID) run --rm $(SERVICE_TARGET) sh -c '\
echo "I am `whoami`. My uid is `id -u`." && echo "Docker runs!"' \
&& echo success