From 54da48b54809d95e1d168345b4e96328e2465fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Fri, 15 Jun 2018 09:46:15 +0200 Subject: [PATCH] Add CI/CD and k8s --- .gitlab-ci.yml | 50 ++++++++++++++++++++++++++++++++ Makefile | 18 +++++------- example-plugin/example_plugin.py | 7 ++--- k8s/README.md | 8 +++++ k8s/senpy-deployment.yaml | 24 +++++++++++++++ k8s/senpy-ingress.yaml | 14 +++++++++ k8s/senpy-svc.yaml | 12 ++++++++ 7 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 .gitlab-ci.yml create mode 100644 k8s/README.md create mode 100644 k8s/senpy-deployment.yaml create mode 100644 k8s/senpy-ingress.yaml create mode 100644 k8s/senpy-svc.yaml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..cf086de --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,50 @@ +# Uncomment if you want to use docker-in-docker +# image: gsiupm/dockermake:latest +# services: +# - docker:dind +# When using dind, it's wise to use the overlayfs driver for +# improved performance. + +stages: + - test + - build + - push + - deploy + +before_script: + - make -e login + +test: + stage: test + script: + - make -e test + +build: + stage: build + script: + - make -e docker-build + only: + - master + - fix-makefiles + +push: + stage: push + script: + - make -e docker-push + only: + - master + - fix-makefiles + +deploy: + stage: deploy + script: + - make -e deploy + only: + - master + - fix-makefiles + +clean : + stage: clean + script: + - make -e clean + when: manual diff --git a/Makefile b/Makefile index 852f12f..be83f1b 100644 --- a/Makefile +++ b/Makefile @@ -1,30 +1,28 @@ PYVERSION=2.7 NAME=senpycommunity REPO=gsiupm -VERSION=test PLUGINS= $(filter %/, $(wildcard */)) -DOCKER_FLAGS= +IMAGENAME=gsiupm/senpy-plugins-community +DOCKER_FLAGS=-p 5000:5000 ifdef SENPY_FOLDER - DOCKER_FLAGS+=-v $(realpath $(SENPY_FOLDER)):/usr/src/app/ + DOCKER_FLAGS+= -v $(realpath $(SENPY_FOLDER)):/usr/src/app/ endif all: build run -build: clean Dockerfile - docker build -t '$(REPO)/$(NAME):$(VERSION)-python$(PYVERSION)' -f Dockerfile .; - test-%: docker run $(DOCKER_FLAGS) -v $$PWD/$*:/senpy-plugins/ --rm -ti '$(REPO)/$(NAME):$(VERSION)-python$(PYVERSION)' --only-test $(TEST_FLAGS) test: test-. -clean: +clean-docker: @docker ps -a | awk '/$(REPO)\/$(NAME)/{ split($$2, vers, "-"); if(vers[1] != "${VERSION}"){ print $$1;}}' | xargs docker rm 2>/dev/null|| true @docker images | awk '/$(REPO)\/$(NAME)/{ split($$2, vers, "-"); if(vers[1] != "${VERSION}"){ print $$1":"$$2;}}' | xargs docker rmi 2>/dev/null|| true -run: build - docker run $(DOCKER_FLAGS) --rm -p 5000:5000 -ti '$(REPO)/$(NAME):$(VERSION)-python$(PYMAIN)' +.PHONY:: test test-% build-% build test test_pip run clean + +include .makefiles/base.mk +include .makefiles/k8s.mk -.PHONY: test test-% build-% build test test_pip run clean diff --git a/example-plugin/example_plugin.py b/example-plugin/example_plugin.py index 3ce1233..ba88d7a 100644 --- a/example-plugin/example_plugin.py +++ b/example-plugin/example_plugin.py @@ -1,9 +1,6 @@ from senpy.plugins import Analysis from senpy.models import Response, Entry -import logging - -logger = logging.getLogger(__name__) class ExamplePlugin(Analysis): '''A *VERY* simple plugin that exemplifies the development of Senpy Plugins''' @@ -21,8 +18,8 @@ class ExamplePlugin(Analysis): custom_attribute = "42" def analyse_entry(self, entry, params): - logger.debug('Analysing with the example.') - logger.debug('The answer to this response is: %s.' % params['parameter']) + self.log.debug('Analysing with the example.') + self.log.debug('The answer to this response is: %s.' % params['parameter']) resp = Response() entry['example:reversed'] = entry.text[::-1] entry['example:the_answer'] = params['parameter'] diff --git a/k8s/README.md b/k8s/README.md new file mode 100644 index 0000000..175beec --- /dev/null +++ b/k8s/README.md @@ -0,0 +1,8 @@ +Deploy senpy to a kubernetes cluster. +The files are templates, which need to be expanded with something like envsubst. + +Example usage: + +``` +cat k8s/*.ya*ml | envsubst | kubectl apply -n senpy -f - +``` diff --git a/k8s/senpy-deployment.yaml b/k8s/senpy-deployment.yaml new file mode 100644 index 0000000..e8608d0 --- /dev/null +++ b/k8s/senpy-deployment.yaml @@ -0,0 +1,24 @@ +--- +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: senpy-$NAME-latest +spec: + replicas: 1 + template: + metadata: + labels: + role: $NAME-latest + app: test + spec: + containers: + - name: senpy-latest + image: $IMAGEWTAG + imagePullPolicy: Always + resources: + limits: + memory: "512Mi" + cpu: "1000m" + ports: + - name: web + containerPort: 5000 diff --git a/k8s/senpy-ingress.yaml b/k8s/senpy-ingress.yaml new file mode 100644 index 0000000..ff3c873 --- /dev/null +++ b/k8s/senpy-ingress.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: extensions/v1beta1 +kind: Ingress +metadata: + name: senpy-$NAME-ingress +spec: + rules: + - host: senpy.cluster.gsi.dit.upm.es + http: + paths: + - path: / + backend: + serviceName: senpy-$NAME-latest + servicePort: 5000 diff --git a/k8s/senpy-svc.yaml b/k8s/senpy-svc.yaml new file mode 100644 index 0000000..4dac88f --- /dev/null +++ b/k8s/senpy-svc.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: v1 +kind: Service +metadata: + name: senpy-$NAME-latest +spec: + type: ClusterIP + ports: + - port: 5000 + protocol: TCP + selector: + role: $NAME-latest