Commit 0d7d5099 authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

Merge branch '1-end-support-for-directly-deploying-k8s-manifests' into 'master'

Resolve "End support for directly deploying k8s manifests"

Closes #1

See merge request !1
parents 6b368501 312d3b1f
Pipeline #31991 passed with stages
in 1 minute and 44 seconds
variables: # Global definierte Umgebungsvariablen zur Nutzung in einzelnen jobs
DOCKER_TLS_CERTDIR: "" # Notwendig, damit mit Docker Registry kommuniziert werden kann
variables:
DOCKER_TLS_CERTDIR: ""
stages: # Stages sind Gruppen von jobs, welche parallel laufen können
include:
- project: 'memoriav/memobase-2020/utilities/ci-templates'
file: 'docker-image/docker-image.yml'
stages:
- test
- publish
......@@ -13,49 +17,3 @@ test:
- pip install -r requirements.txt
script:
- flake8 ./autodeploy_service_app
.build-image: # .-Präfix markiert ein job-template
tags: [mbr]
stage: publish
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- mkdir -p /kaniko/.docker
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $IMAGE_TAG
build-tagged-image:
extends: .build-image # Implementiert das job-template .build-image
variables:
IMAGE_TAG: "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG" # $CI_*-Variablen gehören zu einem set an vordefinierten Variablen
REGISTRY_PASSWORD: "$CI_REGISTRY_PASSWORD"
REGISTRY_USER: "$CI_REGISTRY_USER"
REGISTRY: "$CI_REGISTRY"
DOCKERFILE: "Dockerfile"
only: # job wird nur in folgenden Branches verwendet, wobei tags = getaggte commits
- tags
build-latest-image:
extends: .build-image
variables:
IMAGE_TAG: "$CI_REGISTRY_IMAGE:latest"
REGISTRY_PASSWORD: "$CI_REGISTRY_PASSWORD"
REGISTRY_USER: "$CI_REGISTRY_USER"
REGISTRY: "$CI_REGISTRY"
DOCKERFILE: "Dockerfile"
only:
- master
build-feature-branch-image:
extends: .build-image
variables:
IMAGE_TAG: "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"
REGISTRY_PASSWORD: "$CI_REGISTRY_PASSWORD"
REGISTRY_USER: "$CI_REGISTRY_USER"
REGISTRY: "$CI_REGISTRY"
DOCKERFILE: "Dockerfile"
except: # job wird in allen branches ausser den folgenden verwendet
- master
- tags
\ No newline at end of file
FROM python:3.8
FROM index.docker.io/library/python:3.8
ARG K8S_VERSION=v1.17.11
ARG HELM_VERSION=v3.6.3
WORKDIR /autodeploy_service_app
ADD autodeploy_service_app /autodeploy_service_app/
ADD requirements.txt /autodeploy_service_app/
WORKDIR /autodeploy_service_app/kubectl
#install kubectl so that helm can work in the docker container
RUN wget https://storage.googleapis.com/kubernetes-release/release/v1.16.2/bin/linux/amd64/kubectl && chmod +x ./kubectl
ENV PATH /autodeploy_service_app/kubectl:$PATH
WORKDIR /autodeploy_service_app
#install helm
RUN wget https://get.helm.sh/helm-v3.2.1-linux-amd64.tar.gz && tar -xvzf helm-v3.2.1-linux-amd64.tar.gz && mv linux-amd64 helm
ENV PATH /autodeploy_service_app/helm:$PATH
RUN ./pull-charts.sh
RUN pip install -r requirements.txt
WORKDIR /
ADD setup.py /
RUN pip install -e .
ENV FLASK_APP autodeploy_service_app
ENV FLASK_ENV development
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["/autodeploy_service_app/main.py"]
RUN cd /usr/bin \
&& wget -q https://storage.googleapis.com/kubernetes-release/release/${K8S_VERSION}/bin/linux/amd64/kubectl \
&& chmod +x ./kubectl \
&& wget -q https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz \
&& tar -xvzf helm-${HELM_VERSION}-linux-amd64.tar.gz \
&& mv linux-amd64 helm
WORKDIR /
ADD setup.py /
RUN mkdir autodeploy_service_app \
&& pip install -e .
WORKDIR /autodeploy_service_app
ADD requirements.txt .
RUN pip install -r requirements.txt
ADD autodeploy_service_app .
import logging
import os
from autodeploy_service_app import create_app
if __name__ == "__main__":
logging.basicConfig(format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
level=logging.DEBUG)
logging.info("Starting up")
os.environ["HELM_EXPERIMENTAL_OCI"] = "1"
app = create_app()
app.run(host='0.0.0.0', debug=False)
logging.info("Shutting down")
......@@ -64,17 +64,7 @@ class AutoDeploy(Resource):
capture_output=True, text=True)
output = 'removing autodeploy-workdir\n' + \
proc.stdout + '\n' + proc.stderr
logging.debug('cloning repository')
proc = subprocess.run('git clone ' + repositoryUrl +
' $(pwd)/autodeploy-workdir/', shell=True,
capture_output=True, text=True)
output += 'git clone ' + repositoryUrl + '\n' +\
proc.stdout + '\n' + proc.stderr
if os.path.exists(os.getcwd() + '/autodeploy-workdir/helm-charts'):
output += deployHelmCharts(projectName, repositoryPath, tag, branch)
elif os.path.exists(os.getcwd() + '/autodeploy-workdir/k8s-manifests/test'):
output += deployK8sManifests(projectName, tag)
output += deployHelmCharts(projectName, repositoryPath, tag, branch)
except subprocess.CalledProcessError as ex:
status = 'something failed during installation: ' + ex.output + ' \n' +\
ex.returncode + ' \noutput=' + output
......@@ -94,25 +84,6 @@ class AutoDeploy(Resource):
return returnVal, 500
def deployK8sManifests(projectName, tag):
logging.debug('k8s manifests detected')
logging.debug('deleting previous deployment')
proc = subprocess.run('kubectl delete deployment ' + projectName +
'-deployment', shell=True,
capture_output=True, text=True)
output = 'deleting ' + projectName + '-deployment \n' + \
proc.stdout + '\n' + proc.stderr
logging.debug('applying new deployment')
proc = subprocess.run('kubectl apply -f $(pwd)' +
'/autodeploy-workdir/k8s-manifests/test/', shell=True,
capture_output=True, text=True)
output += 'kubectly apply -f k8s-manifests/test/\n' + \
proc.stdout + '\n' + proc.stderr
output += 'successfully installed manifests'
logging.debug('successfully installed manifests')
return output
def deployHelmCharts(projectName, repositoryPath, tag, branch):
logging.debug('helm charts detected')
output = ''
......@@ -120,13 +91,13 @@ def deployHelmCharts(projectName, repositoryPath, tag, branch):
logging.debug('commit on prod-branch with semver-tag detected: installing on prod+stage')
pullChartUri = os.environ['GITLAB_REGISTRY'] + '/' + repositoryPath + ':' + tag + '-chart'
logging.debug('pulling helm charts')
proc = subprocess.run('export HELM_EXPERIMENTAL_OCI=1 && helm chart pull ' + pullChartUri,
proc = subprocess.run('helm chart pull ' + pullChartUri,
shell=True, capture_output=True, text=True)
output += 'pulling charts from ' + pullChartUri + ' \n' + \
proc.stdout + '\n' + proc.stderr
pulledChartsDir = os.getcwd() + '/autodeploy-workdir/pulled-helm-charts/'
logging.debug('exporting helm charts')
proc = subprocess.run('export HELM_EXPERIMENTAL_OCI=1 && helm chart export ' +
proc = subprocess.run('helm chart export ' +
pullChartUri + ' -d ' + pulledChartsDir,
shell=True, capture_output=True, text=True)
output += 'exporting charts to local directory \n' + \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment