Makefile 4.38 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
22
23
24
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)
CI_REGISTRY=docker.io
CI_REGISTRY_USER=gitlab
CI_BUILD_TOKEN=""
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
25
26
CI_COMMIT_REF_NAME=master

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
27
28


29
30
all: build run

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
31
.FORCE:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
32

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
version: .FORCE
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
34
	@echo $(VERSION) > $(NAME)/VERSION
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
35
	@echo $(VERSION)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
36

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
37
yapf:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
38
	yapf -i -r $(NAME)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
39
40
	yapf -i -r tests

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
41
init:
42
43
44
	pip install --user pre-commit
	pre-commit install

45
dockerfiles: $(addprefix Dockerfile-,$(PYVERSIONS))
46
	@unlink Dockerfile >/dev/null
47
	ln -s Dockerfile-$(PYMAIN) Dockerfile
48
49
50
51

Dockerfile-%: Dockerfile.template
	sed "s/{{PYVERSION}}/$*/" Dockerfile.template > Dockerfile-$*

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
54
build: $(addprefix build-, $(PYVERSIONS))
55

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
56
build-%: version Dockerfile-%
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
57
	docker build -t '$(IMAGEWTAG)-python$*' --cache-from $(IMAGENAME):python$* -f Dockerfile-$* .;
58

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
59
quick_test: $(addprefix test-,$(PYMAIN))
60

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
61
dev-%:
62
	@docker start $(NAME)-dev$* || (\
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
		$(MAKE) build-$*; \
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
64
		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
65
66
	)\

67
	docker exec -ti $(NAME)-dev$* bash
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
68
69
70
71

dev: dev-$(PYMAIN)

test-all: $(addprefix test-,$(PYVERSIONS))
72

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
73
74
test-%:
	docker run --rm --entrypoint /usr/local/bin/python -w /usr/src/app $(IMAGENAME):python$*  setup.py test
75

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
76
test: test-$(PYMAIN)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
77

78
dist/$(TARNAME): version
79
80
	docker run --rm -v $$PWD:/usr/src/app/ -w /usr/src/app/ python:$(PYMAIN) python setup.py sdist;
	docker run --rm -v $$PWD:/usr/src/app/ -w /usr/src/app/ python:$(PYMAIN) chmod -R a+rwx dist;
81

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
82

83
sdist: dist/$(TARNAME)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
84
85

pip_test-%: sdist
86
	docker run --rm -v $$PWD/dist:/dist/ python:$* pip install /dist/$(TARNAME);
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
87
88

pip_test: $(addprefix pip_test-,$(PYVERSIONS))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
89

90
91
92
pip_upload: pip_test 
	python setup.py sdist upload ;

93
clean:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
94
95
	@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
96
97
	@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
98
99

git_commit:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
100
	git commit -a
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
101
102

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
105
git_push:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
106
	git push --tags origin master
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
107

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
108
run-%: build-%
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
109
	docker run --rm -p $(DEVPORT):5000 -ti '$(IMAGEWTAG)-python$(PYMAIN)' --default-plugins
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
110
111

run: run-$(PYMAIN)
112

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
113
push-latest: $(addprefix push-latest-,$(PYVERSIONS))
114
115
	docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGEWTAG)'
	docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGENAME)'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
116
	docker push '$(IMAGENAME):latest'
117
	docker push '$(IMAGEWTAG)'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
118

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
119
120
121
122
123
push-latest-%: build-%
	docker tag $(IMAGENAME):$(VERSION)-python$* $(IMAGENAME):python$*
	docker push $(IMAGENAME):$(VERSION)-python$*
	docker push $(IMAGENAME):python$*

124
125
126
push-%: build-%
	docker push $(IMAGENAME):$(VERSION)-python$*

127
128
129
130
push: $(addprefix push-,$(PYVERSIONS))
	docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGEWTAG)'
	docker push  $(IMAGENAME):$(VERSION)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
131
132
133
134
135
push-github:
	$(eval KEY_FILE := $(shell mktemp))
	@echo "$$GITHUB_DEPLOY_KEY" > $(KEY_FILE)
	@git remote rm github-deploy || true
	git remote add github-deploy $(GITHUB_REPO)
136
	@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
137
138
139
	@GIT_SSH_COMMAND="ssh -i $(KEY_FILE)" git push github-deploy $(CI_COMMIT_REF_NAME)
	rm $(KEY_FILE)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
140
141
142
ci:
	gitlab-runner exec docker --docker-volumes /var/run/docker.sock:/var/run/docker.sock --env CI_PROJECT_NAME=$(NAME) ${action}

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
143
deploy:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
144
145
	@$(KUBECTL) delete secret $(CI_REGISTRY) || true
	@$(KUBECTL) create secret docker-registry $(CI_REGISTRY) --docker-server=$(CI_REGISTRY) --docker-username=$(CI_REGISTRY_USER) --docker-email=$(CI_REGISTRY_USER) --docker-password=$(CI_BUILD_TOKEN)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
146
	@$(KUBECTL) apply -f /tmp/cwd/k8s/
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
147
148
149


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