add ci/cd

This commit is contained in:
Wolfgang Hottgenroth 2023-01-30 12:03:28 +01:00
parent fd4de170a0
commit bdb388d092
Signed by: wn
GPG Key ID: 836E9E1192A6B132
3 changed files with 91 additions and 7 deletions

53
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,53 @@
stages:
- dockerize
- deploy
variables:
IMAGE_NAME: $CI_REGISTRY/$CI_PROJECT_PATH
dockerize:
image: registry.hottis.de/dockerized/docker-bash:latest
stage: build
tags:
- hottis
- linux
- docker
script:
- docker build --tag $IMAGE_NAME:${CI_COMMIT_SHORT_SHA} .
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY;
- docker push $IMAGE_NAME:${CI_COMMIT_SHORT_SHA}
- if [ "$CI_COMMIT_TAG" != "" ]; then
docker tag $IMAGE_NAME:${CI_COMMIT_SHORT_SHA} $IMAGE_NAME:${CI_COMMIT_TAG};
docker push $IMAGE_NAME:${CI_COMMIT_TAG};
fi
deploy:
image: registry.hottis.de/dockerized/docker-bash:latest
stage: deploy
tags:
- saerbeck-deployment-only
only:
- tags
variables:
GIT_STRATEGY: none
CONTAINER_NAME: preprocessor
script:
- docker stop $CONTAINER_NAME || echo "container not running, never mind"
- docker rm $CONTAINER_NAME || echo "container not existing, never mind"
- docker run
-d
--name $CONTAINER_NAME
--restart always
--link timescaledb-server
-e "APPLICATION_TENANT=$APPLICATION_TENANT"
-e "MQTT_LOGIN=$MQTT_LOGIN"
-e "MQTT_PASSWORD=$MQTT_PASSWORD"
-e "MQTT_BROKER=$MQTT_BROKER"
-e "PGHOST=timescaledb-server"
-e "PGUSER=$PGUSER"
-e "PGPASSWORD=$PGPASSWORD"
-e "PGDATABASE=$PGDATABASE"
$IMAGE_NAME:$CI_COMMIT_TAG
environment:
name: production

36
Dockerfile Normal file
View File

@ -0,0 +1,36 @@
FROM python:3.10-bullseye
ENV APPLICATION_TENANT "-"
ENV MQTT_LOGIN "-"
ENV MQTT_PASSWORD "-"
ENV MQTT_BROKER "-"
ENV MQTT_PORT "8883"
ENV MQTT_CA ""
ENV PGHOST ""
ENV PGPORT "5432"
ENV PGUSER "-"
ENV PGSSLMODE "disable"
ENV PGDATABASE "-"
ARG APP_DIR="/opt/app"
RUN \
apt update && \
apt upgrade -y && \
apt install -y libpq-dev && \
mkdir -p ${APP_DIR}
COPY src/requirements.txt ${APP_DIR}/
COPY src/preprocess.py ${APP_DIR}/
WORKDIR ${APP_DIR}
RUN \
pip install -r requirements.txt
USER nobody
CMD [ "python", "preprocess.py" ]

View File

@ -22,14 +22,10 @@ class UnknownSensorException (Exception):
class DbOp(object): class DbOp(object):
def __init__(self, config): def __init__(self, config):
self.db_name = config['DB_NAME']
self.conn = None self.conn = None
def __getConn(self): def __getConn(self):
conn = psycopg2.connect( conn = psycopg2.connect()
database = self.db_name,
sslmode = 'require'
)
conn.autocommit = False conn.autocommit = False
return conn return conn
@ -188,8 +184,7 @@ REQUIRED_CONFIG_OPTIONS = [
'MQTT_BROKER', 'MQTT_BROKER',
'MQTT_PORT', 'MQTT_PORT',
'MQTT_CA', 'MQTT_CA',
'APPLICATION_TENANT', 'APPLICATION_TENANT'
'DB_NAME'
] ]
config = {} config = {}