Makefile 7.92 KB
Newer Older
1
NAME=senpy
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
2
VERSION=$(shell git describe --tags --dirty 2>/dev/null)
3
4
5
GITHUB_REPO=git@github.com:gsi-upm/senpy.git

IMAGENAME=gsiupm/senpy
6
IMAGEWTAG=$(IMAGENAME):$(VERSION)
7
8
9
10

PYVERSIONS=3.5 2.7
PYMAIN=$(firstword $(PYVERSIONS))

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
11
DEVPORT=5000
12
13

TARNAME=$(NAME)-$(VERSION).tar.gz 
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
14
action="test-${PYMAIN}"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
15
GITHUB_REPO=git@github.com:gsi-upm/senpy.git
16

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
17
18
19
KUBE_CA_PEM_FILE=""
KUBE_URL=""
KUBE_TOKEN=""
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
20
KUBE_NAMESPACE=$(NAME)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
21
KUBECTL=docker run --rm -v $(KUBE_CA_PEM_FILE):/tmp/ca.pem -v $$PWD:/tmp/cwd/ -i lachlanevenson/k8s-kubectl --server="$(KUBE_URL)" --token="$(KUBE_TOKEN)" --certificate-authority="/tmp/ca.pem" -n $(KUBE_NAMESPACE)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
22
23
CI_COMMIT_REF_NAME=master

24
25
help:           ## Show this help.
	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/\(.*:\)[^#]*##\s*\(.*\)/\1\t\2/' | column -t -s "	"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
26

27
28
29
30
31
32
33
config:  ## Load config from the environment. You should run it once in every session before other tasks. Run: eval $(make config)
	@echo ". ../.env || true;"
	@awk '{ print "export " $$0}' .env
	@echo "# Please, run: "
	@echo "# eval \$$(make config)"
# If you need to run a command on the key/value pairs, use this:
# @awk '{ split($$0, a, "="); "echo " a[2] " | base64 -w 0" |& getline b64; print "export " a[1] "=" a[2]; print "export " a[1] "_BASE64=" b64}' .env
34

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
35
quick_build: $(addprefix build-, $(PYMAIN))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
36

37
build: $(addprefix build-, $(PYVERSIONS)) ## Build all images / python versions
38

39
build-%: version Dockerfile-%  ## Build a specific version (e.g. build-2.7)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
40
	docker build -t '$(IMAGEWTAG)-python$*' --cache-from $(IMAGENAME):python$* -f Dockerfile-$* .;
41

42
quick_test: test-$(PYMAIN)
43

44
dev-%: ## Launch a specific development environment using docker (e.g. dev-2.7)
45
	@docker start $(NAME)-dev$* || (\
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
46
		$(MAKE) build-$*; \
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
47
		docker run -d -w /usr/src/app/ -p $(DEVPORT):5000 -v $$PWD:/usr/src/app --entrypoint=/bin/bash -ti --name $(NAME)-dev$* '$(IMAGEWTAG)-python$*'; \
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
48
49
	)\

50
	docker exec -ti $(NAME)-dev$* bash
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
51

52
dev: dev-$(PYMAIN) ## Launch a development environment using docker, using the default python version
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
53

54
test-%: ## Run setup.py from in an isolated container, built from the base image. (e.g. test-2.7)
55
56
57
58
59
# This speeds tests up because the image has most (if not all) of the dependencies already.
	docker rm $(NAME)-test-$* || true
	docker create -ti --name $(NAME)-test-$* --entrypoint="" -w /usr/src/app/ $(IMAGENAME):python$* python setup.py test
	docker cp . $(NAME)-test-$*:/usr/src/app
	docker start -a $(NAME)-test-$*
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
test: $(addprefix test-,$(PYVERSIONS)) ## Run the tests with the main python version

run-%: build-%
	docker run --rm -p $(DEVPORT):5000 -ti '$(IMAGEWTAG)-python$(PYMAIN)' --default-plugins

run: run-$(PYMAIN)


#
# Deployment and advanced features
# 


deploy: ## Deploy to kubernetes using the credentials in KUBE_CA_PEM_FILE (or KUBE_CA_BUNDLE ) and TOKEN
	@cat k8s/* | envsubst | $(KUBECTL) apply -f -

deploy-check: ## Get the deployed configuration.
	@$(KUBECTL) get deploy,pods,svc,ingress

login: ## Log in to the registry. It will only be used in the server, or when running a CI task locally (if CI_BUILD_TOKEN is set).
ifeq ($(CI_BUILD_TOKEN),)
	@echo "Not logging in to the docker registry" "$(CI_REGISTRY)"
else
	docker login -u gitlab-ci-token -p $(CI_BUILD_TOKEN) $(CI_REGISTRY)
endif
ifeq ($(HUB_USER),)
	@echo "Not logging in to global the docker registry"
	docker login -u $(HUB_USER) -p $(HUB_PASSWORD)
else
endif

.FORCE:

version: .FORCE
	@echo $(VERSION) > $(NAME)/VERSION
	@echo $(VERSION)

yapf: ## Format python code
	yapf -i -r $(NAME)
	yapf -i -r tests

init: ## Init pre-commit hooks (i.e. enforcing format checking before allowing a commit)
	pip install --user pre-commit
	pre-commit install

dockerfiles: $(addprefix Dockerfile-,$(PYVERSIONS)) ## Generate dockerfiles for each python version
	@unlink Dockerfile >/dev/null
	ln -s Dockerfile-$(PYMAIN) Dockerfile

Dockerfile-%: Dockerfile.template  ## Generate a specific dockerfile (e.g. Dockerfile-2.7)
	sed "s/{{PYVERSION}}/$*/" Dockerfile.template > Dockerfile-$*
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
112

113
dist/$(TARNAME): version
114
	python setup.py sdist;
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
115

116
sdist: dist/$(TARNAME) ## Generate the distribution file (wheel)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
117

118
pip_test-%: sdist ## Test the distribution file using pip install and a specific python version (e.g. pip_test-2.7)
119
	docker run --rm -v $$PWD/dist:/dist/ python:$* pip install /dist/$(TARNAME);
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
120

121
pip_test: $(addprefix pip_test-,$(PYVERSIONS)) ## Test pip installation with the main python version
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
122

123
pip_upload: pip_test  ## Upload package to pip
124
125
	python setup.py sdist upload ;

126
clean: ## Clean older docker images and containers related to this project and dev environments
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
127
128
	@docker ps -a | grep $(IMAGENAME) | awk '{ split($$2, vers, "-"); if(vers[0] != "${VERSION}"){ print $$1;}}' | xargs docker rm -v 2>/dev/null|| true
	@docker images | grep $(IMAGENAME) | awk '{ split($$2, vers, "-"); if(vers[0] != "${VERSION}"){ print $$1":"$$2;}}' | xargs docker rmi 2>/dev/null|| true
129
130
	@docker stop $(addprefix $(NAME)-dev,$(PYVERSIONS)) 2>/dev/null || true
	@docker rm $(addprefix $(NAME)-dev,$(PYVERSIONS)) 2>/dev/null || true
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
131
132

git_commit:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
133
	git commit -a
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
134
135

git_tag:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
136
	git tag ${VERSION}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
137

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
138
git_push:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
139
	git push --tags origin master
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
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
quick_build: $(addprefix build-, $(PYMAIN))

build: $(addprefix build-, $(PYVERSIONS)) ## Build all images / python versions

build-%: version Dockerfile-%  ## Build a specific version (e.g. build-2.7)
	docker build -t '$(IMAGEWTAG)-python$*' --cache-from $(IMAGENAME):python$* -f Dockerfile-$* .;

quick_test: test-$(PYMAIN)

dev-%: ## Launch a specific development environment using docker (e.g. dev-2.7)
	@docker start $(NAME)-dev$* || (\
		$(MAKE) build-$*; \
		docker run -d -w /usr/src/app/ -p $(DEVPORT):5000 -v $$PWD:/usr/src/app --entrypoint=/bin/bash -ti --name $(NAME)-dev$* '$(IMAGEWTAG)-python$*'; \
	)\

	docker exec -ti $(NAME)-dev$* bash

dev: dev-$(PYMAIN) ## Launch a development environment using docker, using the default python version

test-%: ## Run setup.py from in an isolated container, built from the base image. (e.g. test-2.7)
# This speeds tests up because the image has most (if not all) of the dependencies already.
	docker rm $(NAME)-test-$* || true
	docker create -ti --name $(NAME)-test-$* --entrypoint="" -w /usr/src/app/ $(IMAGENAME):python$* python setup.py test
	docker cp . $(NAME)-test-$*:/usr/src/app
	docker start -a $(NAME)-test-$*

test: $(addprefix test-,$(PYVERSIONS)) ## Run the tests with the main python version

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
169
run-%: build-%
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
170
	docker run --rm -p $(DEVPORT):5000 -ti '$(IMAGEWTAG)-python$(PYMAIN)' --default-plugins
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
171
172

run: run-$(PYMAIN)
173

174
push-latest: $(addprefix push-latest-,$(PYVERSIONS)) ## Push the "latest" tag to dockerhub
175
176
	docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGEWTAG)'
	docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGENAME)'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
177
	docker push '$(IMAGENAME):latest'
178
	docker push '$(IMAGEWTAG)'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
179

180
push-latest-%: build-%  ## Push the latest image for a specific python version
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
181
182
183
184
	docker tag $(IMAGENAME):$(VERSION)-python$* $(IMAGENAME):python$*
	docker push $(IMAGENAME):$(VERSION)-python$*
	docker push $(IMAGENAME):python$*

185
push-%: build-%  ## Push the image of the current version (tagged). e.g. push-2.7
186
187
	docker push $(IMAGENAME):$(VERSION)-python$*

188
push: $(addprefix push-,$(PYVERSIONS)) ## Push an image with the current version for every python version
189
190
191
	docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGEWTAG)'
	docker push  $(IMAGENAME):$(VERSION)

192
push-github: ## Push the code to github. You need to set up HUB_USER and HUB_PASSWORD
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
193
194
195
196
	$(eval KEY_FILE := $(shell mktemp))
	@echo "$$GITHUB_DEPLOY_KEY" > $(KEY_FILE)
	@git remote rm github-deploy || true
	git remote add github-deploy $(GITHUB_REPO)
197
	@GIT_SSH_COMMAND="ssh -i $(KEY_FILE)" git fetch github-deploy $(CI_COMMIT_REF_NAME) || true
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
198
199
200
	@GIT_SSH_COMMAND="ssh -i $(KEY_FILE)" git push github-deploy $(CI_COMMIT_REF_NAME)
	rm $(KEY_FILE)

201
202
ci:  ## Run a task using gitlab-runner. Only use to debug problems in the CI pipeline
	gitlab-runner exec shell --builds-dir '.builds' --env CI_PROJECT_NAME=$(NAME) ${action}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
203
204
205


.PHONY: test test-% test-all build-% build test pip_test run yapf push-main push-% dev ci version .FORCE deploy