From 4ea85f3c7ecf5c811ece96ae75f636ace91bc437 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 11 Jan 2024 14:52:36 +0100 Subject: [PATCH] change config to env, add ci-script --- .woodpecker.yml | 29 ++++++++++++++++++++++++ deployment/install-yml.tmpl | 44 +++++++++++++++++++++++++++++++++++++ src/config.py | 30 +++++++++++++++++++++++++ src/digitaltwin1.py | 15 ++----------- 4 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 .woodpecker.yml create mode 100644 deployment/install-yml.tmpl create mode 100644 src/config.py diff --git a/.woodpecker.yml b/.woodpecker.yml new file mode 100644 index 0000000..45910ee --- /dev/null +++ b/.woodpecker.yml @@ -0,0 +1,29 @@ +steps: + build: + image: plugins/kaniko + settings: + repo: gitea.hottis.de/wn/pv-controller + 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] + + deploy: + image: portainer/kubectl-shell:latest + secrets: + - source: kube_config + target: KUBE_CONFIG_CONTENT + commands: + - export IMAGE_TAG=$CI_COMMIT_TAG + - printf "$KUBE_CONFIG_CONTENT" > /tmp/kubeconfig + - export KUBECONFIG=/tmp/kubeconfig + - cat ./deployment/install-yml.tmpl | sed -e 's,%IMAGETAG%,'$IMAGE_TAG','g | kubectl apply -f - + when: + - event: tag + diff --git a/deployment/install-yml.tmpl b/deployment/install-yml.tmpl new file mode 100644 index 0000000..791decc --- /dev/null +++ b/deployment/install-yml.tmpl @@ -0,0 +1,44 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: homea +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: digitaltwin1 + namespace: homea +data: + MQTT__BROKER: "emqx01-anonymous-cluster-internal.broker.svc.cluster.local" + MQTT__DIGITALOUTPUTTOPICPREFIX: "dt1/coil" + MQTT__DIGITALINPUTTOPICPREFIX: "dt1/di" + MQTT__ANALOGINPUTEVENTTOPICPREFIX: "dt1/ai/event" + MQTT__ANALOGINPUTPERIODICTOPICPREFIX: "dt1/ai/periodic" + MQTT__ANALOGPUBLISHPERIOD: "10.0" + MQTT__DISABLEANALOGINPUTEVENTPUBLISHING: "true" + MODBUS__CLIENT: "172.16.2.157" + MODBUS__SCANRATE: "0.25" +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: digitaltwin1 + namespace: homea + labels: + app: digitaltwin1 +spec: + replicas: 1 + selector: + matchLabels: + app: digitaltwin1 + template: + metadata: + labels: + app: digitaltwin1 + spec: + containers: + - name: digitaltwin1 + image: gitea.hottis.de/wn/digitaltwin1:%IMAGETAG% + envFrom: + - configMapRef: + name: digitaltwin1 diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..4c6183f --- /dev/null +++ b/src/config.py @@ -0,0 +1,30 @@ +import os +from loguru import logger + +class Config: + OPTIONS = { + 'mqtt': [ 'broker', + 'digitalInputTopicPrefix', + 'digitalInputTopicPrefix', + 'analogInputEventTopicPrefix', + 'analogInputPeriodicTopicPrefix', + 'analogInputPublishPeriod', + 'disableAnalogInputEventPublishing' ], + 'modbus': [ 'client', 'scanrate' ] + } + + def __init__(self): + self.values = {} + for section, keys in Config.OPTIONS.items(): + self.values[section] = {} + for key in keys: + varname = f"{section}__{key}".upper() + try: + self.values[section][key] = os.environ[varname] + logger.info(f"Config: {section} {key} -> {self.values[section][key]}") + except KeyError: + pass + + def __getitem__(self, section): + return self.values[section] + diff --git a/src/digitaltwin1.py b/src/digitaltwin1.py index d384d65..c1a3040 100644 --- a/src/digitaltwin1.py +++ b/src/digitaltwin1.py @@ -8,7 +8,7 @@ from ModbusHandler import ModbusHandler from MqttEventPublisher import MqttEventPublisher, mqttEventPublisherStart from MqttPeriodPublisher import MqttPeriodPublisher from MqttCoilSubscriber import MqttCoilSubscriber - +from config import Config deathBell = threading.Event() @@ -21,18 +21,7 @@ def exceptHook(args): logger.info("DigitalTwin1 starting") -parser = argparse.ArgumentParser(description="DigitalTwin1") -parser.add_argument('--config', '-f', - help='Config file, default is $pwd/config/config.ini', - required=False, - default='./config/config.ini') -args = parser.parse_args() - -config = configparser.ConfigParser() -config.read(args.config) - - - +config = Config() processImage = ProcessImage()