Makefile 4.29 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
PYVERSIONS=3.5 2.7
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
2
PYMAIN=$(firstword $(PYVERSIONS))
3
4
NAME=senpy
REPO=gsiupm
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
5
VERSION=$(shell git describe --tags --dirty 2>/dev/null)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
6
TARNAME=$(NAME)-$(VERSION).tar.gz 
7
8
IMAGENAME=$(REPO)/$(NAME)
IMAGEWTAG=$(IMAGENAME):$(VERSION)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
9
DEVPORT=5000
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
10
action="test-${PYMAIN}"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
11
GITHUB_REPO=git@github.com:gsi-upm/senpy.git
12

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
13
14
15
KUBE_CA_PEM_FILE=""
KUBE_URL=""
KUBE_TOKEN=""
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
KUBE_NS=$(NAME)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
17
18
19
20
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
21
22
CI_COMMIT_REF_NAME=master

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
23
24


25
26
all: build run

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
29
version: .FORCE
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
30
	@echo $(VERSION) > $(NAME)/VERSION
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
31
	@echo $(VERSION)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
32

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
33
yapf:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
34
	yapf -i -r $(NAME)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
35
36
	yapf -i -r tests

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
init:
38
39
40
	pip install --user pre-commit
	pre-commit install

41
dockerfiles: $(addprefix Dockerfile-,$(PYVERSIONS))
42
	@unlink Dockerfile >/dev/null
43
	ln -s Dockerfile-$(PYMAIN) Dockerfile
44
45
46
47

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

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

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

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

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

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

63
	docker exec -ti $(NAME)-dev$* bash
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
64
65
66
67

dev: dev-$(PYMAIN)

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

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

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

74
dist/$(TARNAME): version
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
75
	docker run --rm -ti -v $$PWD:/usr/src/app/ -w /usr/src/app/ python:$(PYMAIN) python setup.py sdist;
76
77
	docker run --rm -ti -v $$PWD:/usr/src/app/ -w /usr/src/app/ python:$(PYMAIN) chmod -R a+rwx dist;

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

79
sdist: dist/$(TARNAME)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
80
81

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

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

86
clean:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
87
88
	@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
89
90
	@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
91
92

git_commit:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
93
	git commit -a
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
94
95

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
98
git_push:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
99
	git push --tags origin master
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
100

101
pip_upload: pip_test 
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
102
	python setup.py sdist upload ;
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
103

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
104
run-%: build-%
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
105
	docker run --rm -p $(DEVPORT):5000 -ti '$(IMAGEWTAG)-python$(PYMAIN)' --default-plugins
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
106
107

run: run-$(PYMAIN)
108

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
109
push-latest: $(addprefix push-latest-,$(PYVERSIONS))
110
111
	docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGEWTAG)'
	docker tag '$(IMAGEWTAG)-python$(PYMAIN)' '$(IMAGENAME)'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
112
	docker push '$(IMAGENAME):latest'
113
	docker push '$(IMAGEWTAG)'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
114

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
115
116
117
118
119
push-latest-%: build-%
	docker tag $(IMAGENAME):$(VERSION)-python$* $(IMAGENAME):python$*
	docker push $(IMAGENAME):$(VERSION)-python$*
	docker push $(IMAGENAME):python$*

120
121
122
push-%: build-%
	docker push $(IMAGENAME):$(VERSION)-python$*

123
124
125
126
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
127
128
129
130
131
132
133
134
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)
	@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
135
136
137
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
138
deploy:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
139
	$(KUBECTL) delete -n $(KUBE_NS) secret $(CI_REGISTRY) || true
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
140
141
142
143
144
145
	@$(KUBECTL) create -n $(NAME) secret docker-registry $(CI_REGISTRY) --docker-server=$(CI_REGISTRY) --docker-username=$(CI_REGISTRY_USER) --docker-email=$(CI_REGISTRY_USER) --docker-password=$(CI_BUILD_TOKEN)
	$(KUBECTL) apply -f /tmp/cwd/k8s/



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