From afcbd78d0cf0ea717465f1f792b70014c6ead175 Mon Sep 17 00:00:00 2001 From: santanude Date: Thu, 24 Feb 2022 20:39:42 +0530 Subject: [PATCH] Disable assert statements in production build A production version of the image should have the assert statements as a no-op, and should instead return an error code, instead of asserting and crashing the system. SMO-47 Signed-off-by: santanude Change-Id: Ia79d1cdf3e0adee756446d1c20a37ee8e2e0260d Signed-off-by: santanude --- collector/start.sh | 24 ++++++++++++++++++------ dmaapadapter/adapter/code/app_config.py | 5 +++++ dmaapadapter/adapter/code/dmaap_adapter.py | 16 +++++++++++----- dmaapadapter/adapter/config/adapter.conf | 2 +- dmaapadapter/start.sh | 26 ++++++++++++++++++++------ docker-compose.yaml | 3 +++ influxdb-connector/start.sh | 30 ++++++++++++++++++++++-------- 7 files changed, 80 insertions(+), 26 deletions(-) diff --git a/collector/start.sh b/collector/start.sh index 9e1c6e2..49a4311 100755 --- a/collector/start.sh +++ b/collector/start.sh @@ -59,11 +59,23 @@ echo; echo $config_file cat $config_file if [ "$loglevel" != "" ]; then - python3 /opt/smo/evel-test-collector/code/collector/monitor.py \ - --config /opt/smo/evel-test-collector/config/collector.conf \ - --section default > /opt/smo/monitor.log 2>&1 + if [ "$enable_assert" != "True" ]; then + python3 -O /opt/smo/evel-test-collector/code/collector/monitor.py \ + --config /opt/smo/evel-test-collector/config/collector.conf \ + --section default > /opt/smo/monitor.log 2>&1 + else + python3 /opt/smo/evel-test-collector/code/collector/monitor.py \ + --config /opt/smo/evel-test-collector/config/collector.conf \ + --section default > /opt/smo/monitor.log 2>&1 + fi else - python3 /opt/smo/evel-test-collector/code/collector/monitor.py \ - --config /opt/smo/evel-test-collector/config/collector.conf \ - --section default + if [ "$enable_assert" != "True" ]; then + python3 -O /opt/smo/evel-test-collector/code/collector/monitor.py \ + --config /opt/smo/evel-test-collector/config/collector.conf \ + --section default + else + python3 /opt/smo/evel-test-collector/code/collector/monitor.py \ + --config /opt/smo/evel-test-collector/config/collector.conf \ + --section default + fi fi diff --git a/dmaapadapter/adapter/code/app_config.py b/dmaapadapter/adapter/code/app_config.py index f64ab0b..2064e32 100644 --- a/dmaapadapter/adapter/code/app_config.py +++ b/dmaapadapter/adapter/code/app_config.py @@ -21,6 +21,7 @@ import logging.handlers class AppConfig: kafka_broker = "" logger = logging.getLogger() + enable_assert = "" def __init__(self): parser = ArgumentParser(description="", @@ -45,6 +46,7 @@ class AppConfig: config.read(config_file) self.kafka_broker = config.get(config_section, 'kafka_broker', vars=overrides) + self.enable_assert = config.get(config_section, 'enable_assert', vars=overrides) log_file = config.get(config_section, 'log_file', vars=overrides) log_level = config.get(config_section, 'log_level', vars=overrides) @@ -57,6 +59,9 @@ class AppConfig: def getKafkaBroker(self): return self.kafka_broker + def getAssertConfigValue(self): + return self.enable_assert + def getLogger(self): return self.logger diff --git a/dmaapadapter/adapter/code/dmaap_adapter.py b/dmaapadapter/adapter/code/dmaap_adapter.py index e3042e2..df8d2a4 100644 --- a/dmaapadapter/adapter/code/dmaap_adapter.py +++ b/dmaapadapter/adapter/code/dmaap_adapter.py @@ -17,6 +17,7 @@ import flask from flask import request from consumer import EventConsumer, TopicConsumer from prepare_response import PrepareResponse +from app_config import AppConfig app = flask.Flask(__name__) app.config["DEBUG"] = True @@ -52,7 +53,7 @@ def listall_topics(): @app.route(api_base_url + '/topics/', methods=['GET']) def topic_details(topic): - assert topic == request.view_args['topic'] + topic == request.view_args['topic'] prepareResponse = PrepareResponse() topicConsumer = TopicConsumer() topicConsumer.getTopicDetails(prepareResponse, topic) @@ -64,9 +65,9 @@ def topic_details(topic): @app.route(api_base_url + '/events///', methods=['GET']) def get_events(topic, consumergroup, consumerid): - assert topic == request.view_args['topic'] - assert consumergroup == request.view_args['consumergroup'] - assert consumerid == request.view_args['consumerid'] + topic == request.view_args['topic'] + consumergroup == request.view_args['consumergroup'] + consumerid == request.view_args['consumerid'] limit = "" timeout = "" @@ -105,4 +106,9 @@ def getTimeout(timeout): if __name__ == '__main__': - app.run(debug=True, host='0.0.0.0') + appConfig = AppConfig() + + if(appConfig.getAssertConfigValue() == 'False'): + app.run(debug=False, host='0.0.0.0') + else: + app.run(debug=True, host='0.0.0.0') diff --git a/dmaapadapter/adapter/config/adapter.conf b/dmaapadapter/adapter/config/adapter.conf index 0f86b96..1fb7a43 100755 --- a/dmaapadapter/adapter/config/adapter.conf +++ b/dmaapadapter/adapter/config/adapter.conf @@ -6,4 +6,4 @@ log_file = dmaap.log log_level = kafka_broker = - +enable_assert = diff --git a/dmaapadapter/start.sh b/dmaapadapter/start.sh index 11d6f7f..1254e2e 100755 --- a/dmaapadapter/start.sh +++ b/dmaapadapter/start.sh @@ -22,6 +22,8 @@ sed -i -- "s/kafka_broker =/kafka_broker = $kafka_host:$kafka_port/g" \ $config_file sed -i -- "s/log_level =/log_level = $log_level/g" \ $config_file +sed -i -- "s/enable_assert =/enable_assert = $enable_assert/g" \ + $config_file echo; echo $config_file @@ -29,11 +31,23 @@ cat $config_file if [ "$log_level" != "" ]; then - python3 /opt/smo/adapter/code/dmaap_adapter.py \ - --config /opt/smo/adapter/config/adapter.conf \ - --section default > /opt/smo/dmaap.log 2>&1 + if [ "$enable_assert" != "True" ]; then + python3 -O /opt/smo/adapter/code/dmaap_adapter.py \ + --config /opt/smo/adapter/config/adapter.conf \ + --section default > /opt/smo/dmaap.log 2>&1 + else + python3 /opt/smo/adapter/code/dmaap_adapter.py \ + --config /opt/smo/adapter/config/adapter.conf \ + --section default > /opt/smo/dmaap.log 2>&1 + fi else - python3 /opt/smo/adapter/code/dmaap_adapter.py \ - --config /opt/smo/adapter/config/adapter.conf \ - --section default + if [ "$enable_assert" != "True" ]; then + python3 -O /opt/smo/adapter/code/dmaap_adapter.py \ + --config /opt/smo/adapter/config/adapter.conf \ + --section default + else + python3 /opt/smo/adapter/code/dmaap_adapter.py \ + --config /opt/smo/adapter/config/adapter.conf \ + --section default + fi fi diff --git a/docker-compose.yaml b/docker-compose.yaml index 8471ce5..6707ec7 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -114,6 +114,7 @@ services: kafka_host: "smo-kafka" kafka_port: "29092" log_level: "DEBUG" + enable_assert: "False" smo-collector: container_name: smo-collector build: ./collector @@ -137,6 +138,7 @@ services: collector_pass: "password" collector_path: "" loglevel: "ERROR" + enable_assert: "False" depends_on: - smo-kafka smo-influxdb-connector: @@ -153,6 +155,7 @@ services: smo_kafka_host: "smo-kafka" smo_kafka_port: "29092" loglevel: "ERROR" + enable_assert: "False" depends_on: - smo-kafka - smo-influxdb diff --git a/influxdb-connector/start.sh b/influxdb-connector/start.sh index e7d0ecb..f438502 100755 --- a/influxdb-connector/start.sh +++ b/influxdb-connector/start.sh @@ -45,13 +45,27 @@ curl -X POST http://$smo_influxdb_host:$smo_influxdb_port/query \ --data-urlencode "q=CREATE DATABASE eventsdb" if [ "$loglevel" != "" ]; then - python3 /opt/smo/influxdb-connector/code/influxdb_connector.py \ - --config /opt/smo/influxdb-connector/config/influxdb_connector.conf \ - --influxdb $smo_influxdb_host:$smo_influxdb_port \ - --section default > /opt/smo/monitor.log 2>&1 + if [ "$enable_assert" != "True" ]; then + python3 -O /opt/smo/influxdb-connector/code/influxdb_connector.py \ + --config /opt/smo/influxdb-connector/config/influxdb_connector.conf \ + --influxdb $smo_influxdb_host:$smo_influxdb_port \ + --section default > /opt/smo/monitor.log 2>&1 + else + python3 /opt/smo/influxdb-connector/code/influxdb_connector.py \ + --config /opt/smo/influxdb-connector/config/influxdb_connector.conf \ + --influxdb $smo_influxdb_host:$smo_influxdb_port \ + --section default > /opt/smo/monitor.log 2>&1 + fi else - python3 /opt/smo/influxdb-connector/code/influxdb_connector.py \ - --config /opt/smo/influxdb-connector/config/influxdb_connector.conf \ - --influxdb $smo_influxdb_host:$smo_influxdb_port \ - --section default + if [ "$enable_assert" != "True" ]; then + python3 -O /opt/smo/influxdb-connector/code/influxdb_connector.py \ + --config /opt/smo/influxdb-connector/config/influxdb_connector.conf \ + --influxdb $smo_influxdb_host:$smo_influxdb_port \ + --section default + else + python3 /opt/smo/influxdb-connector/code/influxdb_connector.py \ + --config /opt/smo/influxdb-connector/config/influxdb_connector.conf \ + --influxdb $smo_influxdb_host:$smo_influxdb_port \ + --section default + fi fi -- 2.16.6