From dccd123868bd4d7582a78e1bf64d5a1533120fa4 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Wed, 6 Nov 2024 17:32:24 +0100 Subject: [PATCH] add deployment --- .woodpecker.yml | 40 +++++++++++++++++++++++ Dockerfile | 25 +++++++++++++++ deployment/deploy-yml.tmpl | 66 ++++++++++++++++++++++++++++++++++++++ deployment/deploy.sh | 25 +++++++++++++++ src/message_processor.py | 1 + 5 files changed, 157 insertions(+) create mode 100644 .woodpecker.yml create mode 100644 Dockerfile create mode 100644 deployment/deploy-yml.tmpl create mode 100644 deployment/deploy.sh diff --git a/.woodpecker.yml b/.woodpecker.yml new file mode 100644 index 0000000..3bbe4b3 --- /dev/null +++ b/.woodpecker.yml @@ -0,0 +1,40 @@ +steps: + build: + image: plugins/kaniko + settings: + repo: ${FORGE_NAME}/${CI_REPO} + registry: + from_secret: container_registry + tags: latest,${CI_COMMIT_SHA},${CI_COMMIT_TAG} + username: + from_secret: container_registry_username + password: + from_secret: container_registry_password + dockerfile: Dockerfile + when: + - event: [push, tag] + + scan_image: + image: aquasec/trivy + commands: + - trivy image $FORGE_NAME/$CI_REPO:$CI_COMMIT_SHA --quiet --exit-code 1 + when: + - event: [push, tag] + + deploy: + image: portainer/kubectl-shell:latest + secrets: + - source: kube_config + target: KUBE_CONFIG_CONTENT + - source: encryption_key + target: ENCRYPTION_KEY + - source: secrets_checksum + target: MD5_CHECKSUM + commands: + - export IMAGE_TAG=$CI_COMMIT_TAG + - printf "$KUBE_CONFIG_CONTENT" > /tmp/kubeconfig + - export KUBECONFIG=/tmp/kubeconfig + - ./deployment/deploy.sh + when: + - event: tag + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..67aa91a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM python:3.13-alpine + +WORKDIR /app + +COPY src/requirements.txt . +COPY src/main.py . +COPY src/message_processor.py . + +RUN pip install --no-cache-dir -r requirements.txt + + +ENV MQTT_BROKER="" +ENV MQTT_PORT="" +ENV MQTT_CLIENT_PREFIX="" +ENV MQTT_BOX_TOPIC_PREFIXES="" +ENV MQTT_CENTRAL_TOPICS="" +ENV MQTT_STATUS_TOPIC="" +ENV OFF_TEMPERATURE="" +ENV LOW_TEMPERATURE="" +ENV DEFAULT_HIGH_TEMPERATURE="" +ENV MAINTENANCE_TEMPERATURE="" +ENV BOXES="" + +CMD ["python", "main.py"] + diff --git a/deployment/deploy-yml.tmpl b/deployment/deploy-yml.tmpl new file mode 100644 index 0000000..be044c3 --- /dev/null +++ b/deployment/deploy-yml.tmpl @@ -0,0 +1,66 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: heating-controller-config +data: + MQTT_BROKER: "emqx01-anonymous-cluster-internal.broker.svc.cluster.local" + MQTT_PORT: "1883" + MQTT_CLIENT_PREFIX: "HeatingController" + MQTT_BOX_TOPIC_PREFIXES: | + { + "high_temp": "heating/config/high_temp/", + "cmd": "heating/command/" + } + MQTT_CENTRAL_TOPICS: | + { + "general_off": "heating/system/general_off", + "maintenance_mode": "heating/system/maintenance_mode", + "status": "heating/system/status" + } + MQTT_STATUS_TOPIC: "heating/status" + OFF_TEMPERATURE: "5.0" + LOW_TEMPERATURE: "15.0" + DEFAULT_HIGH_TEMPERATURE: "21.0" + MAINTENANCE_TEMPERATURE: "30.0" + BOXES: | + { + "box1": { + "label": "living_room", + "windows": [ + { "topic": "window/living_room/street_side", "label": "street_side" }, + { "topic": "window/living_room/garden_side", "label": "garden_side" } + ], + "output_topic": "output/living_room" + }, + "box2": { + "label": "kitchen", + "windows": [ + { "topic": "window/kitchen/street_side", "label": "street_side" }, + { "topic": "window/kitchen/garden_side", "label": "garden_side" }, + { "topic": "window/kitchen/garden_door", "label": "garden_door" } + ], + "output_topic": "output/kitchen" + } + } +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: heating-controller +spec: + replicas: 1 + selector: + matchLabels: + app: heating-controller + template: + metadata: + labels: + app: heating-controller + spec: + containers: + - name: heating-controller + image: %IMAGE% + envFrom: + - configMapRef: + name: heating-controller-config + diff --git a/deployment/deploy.sh b/deployment/deploy.sh new file mode 100644 index 0000000..2bb4f27 --- /dev/null +++ b/deployment/deploy.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +if [ "$IMAGE_TAG" == "" ]; then + echo "Make sure IMAGE_TAG is set" + exit 1 +fi + + +IMAGE_NAME=$FORGE_NAME/$CI_REPO +NAMESPACE=homea +DEPLOYMENT_DIR=$PWD/deployment + +pushd $DEPLOYMENT_DIR > /dev/null +kubectl create namespace $NAMESPACE \ + --dry-run=client \ + -o yaml | \ + kubectl -f - apply + +cat $DEPLOYMENT_DIR/deploy-yml.tmpl | \ + sed -e 's,%IMAGE%,'$IMAGE_NAME':'$IMAGE_TAG','g | \ + kubectl apply -f - -n $NAMESPACE + +popd > /dev/null + + diff --git a/src/message_processor.py b/src/message_processor.py index 717b529..30e1c23 100644 --- a/src/message_processor.py +++ b/src/message_processor.py @@ -86,6 +86,7 @@ def _calculate_output_temperature(local_context): if local_context['mode'] == 'high': local_context['output_temperature'] = local_context['high_temperature'] return + # if we come here, something serious happened logger.error(f"Error in calculation of output_temperature: {local_context=}") return