-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
186 lines (145 loc) Β· 5.69 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
#########################################################
##
# EXPERIMENTAL - Only use if you know what you're doing #
##
#########################################################
BASE := $(shell /bin/pwd)
CODE_COVERAGE = 72
PYTHON ?= $(shell pyenv which python3.6)
#################
# Python vars #
#################
SERVICE ?= cynnig
#############
# SAM vars #
#############
NETWORK = ""
target:
$(info ${HELP_MESSAGE})
@exit 0
clean: ##=> Deletes current build environment and latest build
$(info [*] Who needs all that anyway? Destroying environment....)
rm -rf ./${SERVICE}/build
rm -rf ./${SERVICE}.zip
find . -type d -name '*pycache*' -exec rm -rf {} \;
all: clean build
install: _install_packages _install_dev_packages
shell:
@pipenv shell
package: _check_service_definition ##=> Builds package using Docker Lambda container
ifeq ($(DOCKER),1)
$(info [*] Cleaning up local dev/builds before build task...)
@$(MAKE) clean SERVICE="${SERVICE}"
$(info [+] Packaging service '$(SERVICE)' using Docker Lambda -- This may take a while...)
docker run -v $$PWD:/var/task -it lambci/lambda:build-python3.6 /bin/bash -c 'make _package SERVICE="${SERVICE}"'
else
$(info [*] Cleaning up local builds before build task...)
@$(MAKE) clean SERVICE="${SERVICE}"
$(info [+] Packaging service '$(SERVICE)' -- This may take a while...)
@$(MAKE) _package SERVICE="${SERVICE}"
endif
build: _check_service_definition _clone_service_to_build ##=> Same as package except that we don't create a ZIP
@$(MAKE) _install_deps SERVICE="${SERVICE}"
run: ##=> Run SAM Local API GW and can optionally run new containers connected to a defined network
@test -z ${NETWORK} \
&& sam local start-api \
|| sam local start-api --docker-network ${NETWORK}
test: ##=> Run pytest
@AWS_XRAY_CONTEXT_MISSING=LOG_ERROR pipenv run python -m pytest --cov . --cov-report term-missing --cov-fail-under $(CODE_COVERAGE) tests/ -v
#############
# Helpers #
#############
_install_packages:
$(info [*] Install required packages...)
@pipenv install --python ${PYTHON}
_install_dev_packages:
$(info [*] Install required dev-packages...)
@pipenv install -d --python ${PYTHON}
_check_service_definition:
$(info [*] Checking whether service $(SERVICE) exists...)
# SERVICE="<name_of_service>" must be passed as ARG for target or else fail
ifndef SERVICE
$(error [!] SERVICE env not defined...FAIL)
endif
ifeq ($(wildcard $(SERVICE)/.),)
$(error [!] '$(SERVICE)' folder doesn't exist)
endif
ifeq ($(wildcard Pipfile),)
$(error [!] Pipfile dependencies file missing from $(BASE) folder...)
endif
_clone_service_to_build:
ifeq ($(wildcard $(SERVICE)/build/.),)
$(info [+] Setting permissions for files under ${SERVICE}/)
find ${SERVICE}/ -type f -exec chmod go+r {} \;
$(info [+] Setting permissions for directories under ${SERVICE}/)
find ${SERVICE}/ -type d -exec chmod go+rx {} \;
$(info [+] Cloning ${SERVICE} directory structure to ${SERVICE}/build)
rsync -a -f "+ */" -f "- *" ${SERVICE}/ ${SERVICE}/build/
$(info [+] Cloning source files from ${SERVICE} to ${SERVICE}/build)
@find ${SERVICE} -type f \
-not -name "*.pyc" \
-not -name "*__pycache__" \
-not -name "requirements.txt" \
-not -name "event.json" \
-not -name "build" | cut -d '/' -f2- > .results.txt
@while read line; do \
ln -f ${SERVICE}/$$line ${SERVICE}/build/$$line; \
done < .results.txt
rm -f .results.txt
else
$(info [-] '$(SERVICE)' already has a development build - Ignoring cloning task...)
endif
_check_dev_definition: _check_service_definition
$(info [*] Checking whether service $(SERVICE) development build exists...)
ifeq ($(wildcard $(SERVICE)/build/.),)
$(warning [FIX] run 'make build SERVICE=$(SERVICE)' to create one")
$(error [!] '$(SERVICE)' doesn't have development build)
endif
_install_deps:
$(info [+] Installing '$(SERVICE)' dependencies...")
@pip install pipenv
@pipenv lock -r > requirements.txt
@pipenv run pip install \
--isolated \
--disable-pip-version-check \
-Ur requirements.txt -t ${SERVICE}/build/
@rm -f requirements.txt
# Package application and devs together in expected zip from build
_package: _clone_service_to_build _check_service_definition _install_deps
@$(MAKE) _zip SERVICE="${SERVICE}"
# As its name states: Zip everything up from build
_zip: _check_dev_definition
$(info [+] Creating '$(SERVICE)' ZIP...")
@cd ${SERVICE}/build && zip -rq -9 "$(BASE)/$(SERVICE).zip" * \
--exclude "wheel/*" "setuptools/*" "pkg_resources/*" "pip/*" \
"easy_install.py" "__pycache__/*" "*.dist-info/*" "./**/__pycache__/*"
$(info [*] Build complete: $(BASE)/$(SERVICE).zip)
define HELP_MESSAGE
Environment variables to be aware of or to hardcode depending on your use case:
SERVICE
Default: not_defined
Info: Environment variable to declare where source code for lambda is
DOCKER
Default: not_defined
Info: Environment variable to declare whether Docker should be used to build (great for C-deps)
Common usage:
...::: Installs all required packages as defined in the pipfile :::...
$ make install
...::: Spawn a virtual environment shell :::...
$ make shell
...::: Cleans up the environment - Deletes Virtualenv, ZIP builds and Dev env :::...
$ make clean SERVICE="slack"
...::: Creates local dev environment for Python hot-reloading w/ packages:::...
$ make build SERVICE="cynnig"
...::: Bundles app and dependencies into a ZIP file :::...
$ make package SERVICE="email"
...::: Bundles app and dependencies into a ZIP file using Docker:::...
$ make package SERVICE="email" DOCKER=1
...::: Run SAM Local API Gateway :::...
$ make run
...::: Run Pytest under tests/ with pipenv :::...
$ make test
Advanced usage:
...::: Run SAM Local API Gateway within a Docker Network :::...
$ make run NETWORK="sam-network"
endef