-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
196 lines (146 loc) · 5.65 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
SHELL = /bin/bash
.DEFAULT_GOAL := help
SERVICE_NAME := service-alti
CURRENT_DIR := $(shell pwd)
# Docker metadata
GIT_HASH = `git rev-parse HEAD`
GIT_HASH_SHORT = `git rev-parse --short HEAD`
GIT_BRANCH = `git symbolic-ref HEAD --short 2>/dev/null`
GIT_DIRTY = `git status --porcelain`
GIT_TAG = `git describe --tags || echo "no version info"`
AUTHOR = $(USER)
# Test report configuration
TEST_REPORT_DIR ?= $(CURRENT_DIR)/tests/report
TEST_REPORT_FILE ?= nose2-junit.xml
# Docker variables
DOCKER_REGISTRY = 974517877189.dkr.ecr.eu-central-1.amazonaws.com
DOCKER_IMG_LOCAL_TAG = $(DOCKER_REGISTRY)/$(SERVICE_NAME):local-$(USER)-$(GIT_HASH_SHORT)
# AWS variables
AWS_DEFAULT_REGION = eu-central-1
# Find all python files that are not inside a hidden directory (directory starting with .)
PYTHON_FILES := $(shell find ./* -type f -name "*.py" -print)
# PIPENV files
PIP_FILE = $(CURRENT_DIR)/Pipfile
PIP_FILE_LOCK = $(CURRENT_DIR)/Pipfile.lock
VENV := $(shell pipenv --venv)
# default configuration
HTTP_PORT ?= 5000
# For the DTM_BASE_PATH use the one in development.bgdi.ch server as default
DTM_BASE_PATH ?= /var/local/efs-dev/geodata/bund/swisstopo/
LOGS_DIR ?= $(CURRENT_DIR)/logs
# Commands
PYTHON := $(VENV)/bin/python
PIP := $(VENV)/bin/pip
FLASK := $(VENV)/bin/flask
YAPF := $(VENV)/bin/yapf
ISORT := $(VENV)/bin/isort
NOSE := $(VENV)/bin/nose2
PYLINT := $(VENV)/bin/pylint
all: help
.PHONY: help
help:
@echo "Usage: make <target>"
@echo
@echo "Possible targets:"
@echo -e " \033[1mSetup TARGETS\033[0m "
@echo "- setup Create the python virtual environment with developper tools"
@echo "- ci Create the python virtual environment and install requirements based on the Pipfile.lock"
@echo -e " \033[1mFORMATING, LINTING AND TESTING TOOLS TARGETS\033[0m "
@echo "- format Format the python source code"
@echo "- ci-check-format Format the python source code and check if any files has changed. This is meant to be used by the CI."
@echo "- lint Lint the python source code"
@echo "- format-lint Format and lint the python source code"
@echo "- test Run the tests"
@echo -e " \033[1mLOCAL SERVER TARGETS\033[0m "
@echo "- serve Run the project using the flask debug server. Port can be set by Env variable HTTP_PORT (default: 5000)"
@echo "- gunicornserve Run the project using the gunicorn WSGI server. Port can be set by Env variable DEBUG_HTTP_PORT (default: 5000)"
@echo -e " \033[1mDocker TARGETS\033[0m "
@echo "- dockerlogin Login to the AWS ECR registery for pulling/pushing docker images"
@echo "- dockerbuild Build the project localy (with tag := $(DOCKER_IMG_LOCAL_TAG)) using the gunicorn WSGI server inside a container"
@echo "- dockerpush Build and push the project localy (with tag := $(DOCKER_IMG_LOCAL_TAG))"
@echo "- dockerrun Run the project using the gunicorn WSGI server inside a container (exposed port: 5000)"
@echo "- shutdown Stop the aforementioned container"
@echo -e " \033[1mCLEANING TARGETS\033[0m "
@echo "- clean Clean genereated files"
@echo "- clean_venv Clean python venv"
# Build targets. Calling setup is all that is needed for the local files to be installed as needed.
.PHONY: setup
setup:
mkdir -p $(LOGS_DIR)
pipenv install --dev
pipenv shell
.PHONY: ci
ci:
# Create virtual env with all packages for development using the Pipfile.lock
pipenv sync --dev
# linting target, calls upon yapf to make sure your code is easier to read and respects some conventions.
.PHONY: format
format:
$(YAPF) -p -i --style .style.yapf $(PYTHON_FILES)
$(ISORT) $(PYTHON_FILES)
.PHONY: ci-check-format
ci-check-format: format
@if [[ -n `git status --porcelain` ]]; then \
>&2 echo "ERROR: the following files are not formatted correctly:"; \
>&2 git status --porcelain; \
exit 1; \
fi
.PHONY: lint
lint:
$(PYLINT) $(PYTHON_FILES)
.PHONY: format-lint
format-lint: format lint
# Test target
.PHONY: test
test:
mkdir -p $(TEST_REPORT_DIR)
DTM_BASE_PATH=$(CURRENT_DIR) $(NOSE) \
-c tests/unittest.cfg \
--verbose \
--junit-xml-path $(TEST_REPORT_DIR)/$(TEST_REPORT_FILE) \
-s tests/
# Serve targets. Using these will run the application on your local machine. You can either serve with a wsgi front (like it would be within the container), or without.
.PHONY: serve
serve:
LOGS_DIR=$(LOGS_DIR) DTM_BASE_PATH=$(DTM_BASE_PATH) FLASK_APP=$(subst -,_,$(SERVICE_NAME)) FLASK_DEBUG=1 $(FLASK) run \
--host=0.0.0.0 \
--port=$(HTTP_PORT)
.PHONY: gunicornserve
gunicornserve:
LOGS_DIR=$(LOGS_DIR) DTM_BASE_PATH=$(DTM_BASE_PATH) $(PYTHON) wsgi.py
# Docker related functions.
.PHONY: dockerlogin
dockerlogin:
aws --profile swisstopo-bgdi-builder ecr get-login-password --region $(AWS_DEFAULT_REGION) | docker login --username AWS --password-stdin $(DOCKER_REGISTRY)
.PHONY: dockerbuild
dockerbuild:
docker build \
--build-arg GIT_HASH="$(GIT_HASH)" \
--build-arg GIT_BRANCH="$(GIT_BRANCH)" \
--build-arg GIT_DIRTY="$(GIT_DIRTY)" \
--build-arg VERSION="$(GIT_TAG)" \
--build-arg AUTHOR="$(AUTHOR)" \
-t $(DOCKER_IMG_LOCAL_TAG) .
.PHONY: dockerpush
dockerpush:
docker push $(DOCKER_IMG_LOCAL_TAG)
.PHONY: dockerrun
dockerrun:
docker run \
-it --rm --net=host \
-e HTTP_PORT=$(HTTP_PORT) \
-e LOGS_DIR=/logs \
-e DTM_BASE_PATH=/var/local/profile \
-v "${LOGS_DIR}":/logs \
-v "${DTM_BASE_PATH}":/var/local/profile \
$(DOCKER_IMG_LOCAL_TAG)
# Clean targets
.PHONY: clean_venv
clean_venv:
pipenv --rm
.PHONY: clean
clean: clean_venv
@# clean python cache files
find . -name __pycache__ -type d -print0 | xargs -I {} -0 rm -rf "{}"
rm -rf $(TEST_REPORT_DIR)
rm -rf $(LOGS_DIR)