diff --git a/docker/wis2-gdc.env b/docker/wis2-gdc.env index f90ba90..aca4791 100644 --- a/docker/wis2-gdc.env +++ b/docker/wis2-gdc.env @@ -6,6 +6,7 @@ export WIS2_GDC_BROKER_URL=mqtt://wis2-gdc:wis2-gdc@mosquitto:1883 export WIS2_GDC_CENTRE_ID=ca-eccc-msc-gdc export WIS2_GDC_GB=mqtt://everyone:everyone@mosquitto:1883 export WIS2_GDC_GB_TOPIC=origin/a/wis2/+/+/metadata/# +export WIS2_GDC_PUBLISH_REPORTS=true # global broker links export WIS2_GDC_GB_LINK_METEOFRANCE="Météo-France,mqtts://everyone:everyone@globalbroker.meteo.fr:8883" diff --git a/wis2-gdc.env b/wis2-gdc.env index 44a5727..9b5dfa5 100644 --- a/wis2-gdc.env +++ b/wis2-gdc.env @@ -6,6 +6,7 @@ export WIS2_GDC_BROKER_URL=mqtt://wis2-gdc:wis2-gdc@localhost:1883 export WIS2_GDC_CENTRE_ID=ca-eccc-msc-gdc export WIS2_GDC_GB=mqtt://everyone:everyone@localhost:1883 export WIS2_GDC_GB_TOPIC=origin/a/wis2/+/+/metadata/# +export WIS2_GDC_PUBLISH_REPORTS=true # global broker links export WIS2_GDC_GB_LINK_METEOFRANCE="Météo-France,mqtts://everyone:everyone@globalbroker.meteo.fr:8883" diff --git a/wis2_gdc/env.py b/wis2_gdc/env.py index aa66bbb..e28d9a5 100644 --- a/wis2_gdc/env.py +++ b/wis2_gdc/env.py @@ -20,6 +20,28 @@ ############################################################################### import os +from typing import Any + + +def str2bool(value: Any) -> bool: + """ + helper function to return Python boolean + type (source: https://stackoverflow.com/a/715468) + + :param value: value to be evaluated + + :returns: `bool` of whether the value is boolean-ish + """ + + value2 = False + + if isinstance(value, bool): + value2 = value + else: + value2 = value.lower() in ('yes', 'true', 't', '1', 'on') + + return value2 + API_URL = os.environ.get('WIS2_GDC_API_URL') API_URL_DOCKER = os.environ.get('WIS2_GDC_API_URL_DOCKER') @@ -27,13 +49,14 @@ BACKEND_CONNECTION = os.environ.get('WIS2_GDC_BACKEND_CONNECTION') BROKER_URL = os.environ.get('WIS2_GDC_BROKER_URL') CENTRE_ID = os.environ.get('WIS2_GDC_CENTRE_ID') +PUBLISH_REPORTS = str2bool(os.environ.get('WIS2_GDC_PUBLISH_REPORTS', 'false')) + +GB_LINKS = [] if None in [API_URL, API_URL_DOCKER, BACKEND_TYPE, - BACKEND_CONNECTION, BROKER_URL, CENTRE_ID]: + BACKEND_CONNECTION, BROKER_URL, CENTRE_ID, PUBLISH_REPORTS]: raise EnvironmentError('Environment variables not set!') -GB_LINKS = [] - for key, value in os.environ.items(): if key.startswith('WIS2_GDC_GB_LINK'): GB_LINKS.append(value.split(',')) diff --git a/wis2_gdc/registrar.py b/wis2_gdc/registrar.py index b67aa78..bbe4dbe 100644 --- a/wis2_gdc/registrar.py +++ b/wis2_gdc/registrar.py @@ -33,7 +33,7 @@ from wis2_gdc.backend import BACKENDS from wis2_gdc.env import (BACKEND_TYPE, BACKEND_CONNECTION, BROKER_URL, - GB_LINKS) + GB_LINKS, PUBLISH_REPORTS) LOGGER = logging.getLogger(__name__) @@ -46,8 +46,11 @@ def __init__(self): :returns: `wis2_gdc.registrar.Registrar` """ + self.broker = None self.metadata = None - self.broker = MQTTPubSubClient(BROKER_URL) + + if PUBLISH_REPORTS: + self.broker = MQTTPubSubClient(BROKER_URL) def register(self, metadata: dict) -> None: """ @@ -67,8 +70,9 @@ def register(self, metadata: dict) -> None: LOGGER.info('Running ETS') ets_results = self._run_ets() - LOGGER.info('Publishing ETS report to broker') - self.broker.pub(topic, json.dumps(ets_results)) + if self.broker is not None: + LOGGER.info('Publishing ETS report to broker') + self.broker.pub(topic, json.dumps(ets_results)) try: if ets_results['ets-report']['summary']['FAILED'] > 0: @@ -85,10 +89,12 @@ def register(self, metadata: dict) -> None: LOGGER.info(f'Publishing metadata to {BACKEND_TYPE} ({BACKEND_CONNECTION})') # noqa self._publish() - LOGGER.info('Running ETS') + LOGGER.info('Running KPI') kpi_results = self._run_kpi() - LOGGER.info('Publishing KPI report to broker') - self.broker.pub(topic, json.dumps(kpi_results)) + + if self.broker is not None: + LOGGER.info('Publishing KPI report to broker') + self.broker.pub(topic, json.dumps(kpi_results)) def _run_ets(self) -> dict: """