Makefile 4.5 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

73
74
# Run setup.py from in an isolated container, built from the base image.
# This speeds tests up because the image has most (if not all) of the dependencies already.
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
75
test-%:
76
77
78
79
	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-$*
80

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

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

86
sdist: dist/$(TARNAME)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
87
88

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

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

93
94
95
pip_upload: pip_test 
	python setup.py sdist upload ;

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

git_commit:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
103
	git commit -a
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
104
105

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
108
git_push:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
109
	git push --tags origin master
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
110

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

run: run-$(PYMAIN)
115

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

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

127
128
129
push-%: build-%
	docker push $(IMAGENAME):$(VERSION)-python$*

130
131
132
133
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
134
135
136
137
138
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)
139
	@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
140
141
142
	@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
143
144
145
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
146
deploy:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
147
148
	@$(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
149
	@$(KUBECTL) apply -f /tmp/cwd/k8s/
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
150
151
152


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