diff --git a/manifests/contextservice.yaml b/manifests/contextservice.yaml index 75406da088820da3e8360e7c37fc0d05aa8f40db..cf7da7e43fbfc02f5872745fc6f10f3cfcee6cb2 100644 --- a/manifests/contextservice.yaml +++ b/manifests/contextservice.yaml @@ -18,6 +18,7 @@ spec: imagePullPolicy: Always ports: - containerPort: 1010 + - containerPort: 8080 env: - name: DB_ENGINE value: "redis" @@ -51,3 +52,27 @@ spec: - name: grpc port: 1010 targetPort: 1010 + - name: http + port: 8080 + targetPort: 8080 +--- +apiVersion: v1 +kind: Service +metadata: + name: contextservice-public + labels: + app: contextservice +spec: + type: NodePort + selector: + app: contextservice + ports: + - name: grpc + protocol: TCP + port: 1010 + targetPort: 1010 + - name: http + protocol: TCP + port: 8080 + targetPort: 8080 +--- diff --git a/manifests/deviceservice.yaml b/manifests/deviceservice.yaml index 7aa02e815034ff05633b2febaaf38ceb72bf06a3..5afd6c7c85e0f62dd8902db746b3f54b412a7c56 100644 --- a/manifests/deviceservice.yaml +++ b/manifests/deviceservice.yaml @@ -49,5 +49,6 @@ spec: app: deviceservice ports: - name: grpc + protocol: TCP port: 2020 targetPort: 2020 diff --git a/manifests/monitoringservice.yaml b/manifests/monitoringservice.yaml index bf886f4f9643676e22173537a1de4523f95386ae..885e50adc04d86ed59d118c9223f11b1e39385da 100644 --- a/manifests/monitoringservice.yaml +++ b/manifests/monitoringservice.yaml @@ -46,5 +46,6 @@ spec: app: monitoringservice ports: - name: grpc + protocol: TCP port: 8080 targetPort: 8080 diff --git a/manifests/redis.yaml b/manifests/redis.yaml index 4d6d6cbf2e5d71806ebeddbb3d6b67cf19a5d3f6..9aaebb1673637e6afc4fcf2d5887009f5d365a4d 100644 --- a/manifests/redis.yaml +++ b/manifests/redis.yaml @@ -36,3 +36,19 @@ spec: port: 6379 targetPort: 6379 --- +apiVersion: v1 +kind: Service +metadata: + name: redis-public + labels: + app: redis +spec: + type: NodePort + selector: + app: redis + ports: + - name: redis + protocol: TCP + port: 6379 + targetPort: 6379 +--- diff --git a/manifests/serviceservice.yaml b/manifests/serviceservice.yaml index 3c673c7d5d973f06978b144ab0a747f2b5b1877a..72fd1c61564831f61bdf78aa494092829f0dd676 100644 --- a/manifests/serviceservice.yaml +++ b/manifests/serviceservice.yaml @@ -49,5 +49,6 @@ spec: app: serviceservice ports: - name: grpc + protocol: TCP port: 3030 targetPort: 3030 diff --git a/src/context/Config.py b/src/context/Config.py index 473db28503e6ef2a9db36087e7c62fa8799979f7..2019cdd0141dc98063fde51568c59e84f6ae087e 100644 --- a/src/context/Config.py +++ b/src/context/Config.py @@ -8,8 +8,9 @@ GRPC_SERVICE_PORT = 1010 GRPC_MAX_WORKERS = 10 GRPC_GRACE_PERIOD = 60 -# HTTP settings -HTTP_SERVICE_PORT = 8080 +# REST-API settings +RESTAPI_SERVICE_PORT = 8080 +RESTAPI_BASE_URL = '/api' # Prometheus settings METRICS_PORT = 9192 diff --git a/src/context/requirements.in b/src/context/requirements.in index 25abdad1b5767117956a88b816399635348884c7..398b8d3d8e276b69605bccd7b0608961752578c8 100644 --- a/src/context/requirements.in +++ b/src/context/requirements.in @@ -1,3 +1,4 @@ +flask-restful grpcio-health-checking grpcio prometheus-client diff --git a/src/context/service/__main__.py b/src/context/service/__main__.py index 4335f9137bb1b3d358b741c6e2b6d270887e54d2..0bcdb805672301d31b584170db9f12534dfa659f 100644 --- a/src/context/service/__main__.py +++ b/src/context/service/__main__.py @@ -1,8 +1,11 @@ import logging, os, signal, sys, threading from prometheus_client import start_http_server from common.database.Factory import get_database +from context.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, LOG_LEVEL, RESTAPI_SERVICE_PORT, \ + RESTAPI_BASE_URL, METRICS_PORT from context.service.ContextService import ContextService -from context.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, LOG_LEVEL, METRICS_PORT +from context.service.rest_server.Server import Server +from context.service.rest_server.resources.Context import Context terminate = threading.Event() logger = None @@ -15,11 +18,13 @@ def signal_handler(signal, frame): def main(): global terminate, logger - service_port = os.environ.get('CONTEXTSERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT) - max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) - grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD) - log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) - metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT) + grpc_service_port = os.environ.get('CONTEXTSERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT ) + max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) + grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD ) + log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) + restapi_service_port = os.environ.get('RESTAPI_SERVICE_PORT', RESTAPI_SERVICE_PORT) + restapi_base_url = os.environ.get('RESTAPI_BASE_URL', RESTAPI_BASE_URL ) + metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT ) logging.basicConfig(level=log_level) logger = logging.getLogger(__name__) @@ -36,17 +41,23 @@ def main(): database = get_database() # Starting context service - service = ContextService(database, port=service_port, max_workers=max_workers, grace_period=grace_period) - service.start() + grpc_service = ContextService(database, port=grpc_service_port, max_workers=max_workers, grace_period=grace_period) + grpc_service.start() + + rest_server = Server(port=restapi_service_port, base_url=restapi_base_url) + rest_server.add_resource(Context, '/restconf/config/context', endpoint='api.context') + rest_server.start() # Wait for Ctrl+C or termination signal - while not terminate.wait(0.1): pass + while not terminate.wait(timeout=0.1): pass logger.info('Terminating...') - service.stop() + grpc_service.stop() + rest_server.shutdown() + rest_server.join() logger.info('Bye') - return(0) + return 0 if __name__ == '__main__': sys.exit(main()) diff --git a/src/context/service/rest_server/Server.py b/src/context/service/rest_server/Server.py new file mode 100644 index 0000000000000000000000000000000000000000..e51970612eead5415e7786e4c001fd735fe01fe7 --- /dev/null +++ b/src/context/service/rest_server/Server.py @@ -0,0 +1,30 @@ +import logging, threading +from flask import Flask +from flask_restful import Api +from werkzeug.serving import make_server +from context.Config import RESTAPI_BASE_URL, RESTAPI_SERVICE_PORT + +logging.getLogger('werkzeug').setLevel(logging.WARNING) + +BIND_ADDRESS = '0.0.0.0' +LOGGER = logging.getLogger(__name__) + +class Server(threading.Thread): + def __init__(self, host=BIND_ADDRESS, port=RESTAPI_SERVICE_PORT, base_url=RESTAPI_BASE_URL): + threading.Thread.__init__(self, daemon=True) + self.host = host + self.port = port + self.app = Flask(__name__) + self.api = Api(self.app, prefix=base_url) + + def add_resource(self, resource, *urls, **kwargs): + self.api.add_resource(resource, *urls, **kwargs) + + def run(self): + self.srv = make_server(self.host, self.port, self.app, threaded=True) + self.ctx = self.app.app_context() + self.ctx.push() + self.srv.serve_forever() + + def shutdown(self): + self.srv.shutdown() diff --git a/src/context/service/rest_server/__init__.py b/src/context/service/rest_server/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/context/service/rest_server/resources/Context.py b/src/context/service/rest_server/resources/Context.py new file mode 100644 index 0000000000000000000000000000000000000000..421ad4a2aa16e9ad3fed3aec5f606bf0758d6953 --- /dev/null +++ b/src/context/service/rest_server/resources/Context.py @@ -0,0 +1,9 @@ +from flask.json import jsonify +from flask_restful import Resource +from common.database.Factory import get_database +from common.database.api.context.Constants import DEFAULT_CONTEXT_ID + +class Context(Resource): + def get(self): + database = get_database() + return jsonify(database.context(DEFAULT_CONTEXT_ID).dump()) diff --git a/src/context/service/rest_server/resources/__init__.py b/src/context/service/rest_server/resources/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/device/service/__main__.py b/src/device/service/__main__.py index ae7db591c5c39836e0e1a5c62039060eb6651434..3ac9893a1fe408478a9f6482fd38fb9dcf725038 100644 --- a/src/device/service/__main__.py +++ b/src/device/service/__main__.py @@ -16,10 +16,10 @@ def main(): global terminate, logger service_port = os.environ.get('DEVICESERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT) - max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) - grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD) - log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) - metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT) + max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) + grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD) + log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) + metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT ) logging.basicConfig(level=log_level) logger = logging.getLogger(__name__) @@ -36,17 +36,17 @@ def main(): database = get_database() # Starting device service - service = DeviceService(database, port=service_port, max_workers=max_workers, grace_period=grace_period) - service.start() + grpc_service = DeviceService(database, port=service_port, max_workers=max_workers, grace_period=grace_period) + grpc_service.start() # Wait for Ctrl+C or termination signal - while not terminate.wait(0.1): pass + while not terminate.wait(timeout=0.1): pass logger.info('Terminating...') - service.stop() + grpc_service.stop() logger.info('Bye') - return(0) + return 0 if __name__ == '__main__': sys.exit(main()) diff --git a/src/service/service/__main__.py b/src/service/service/__main__.py index 2eef3598396ceb74633b9c529532dc3cb11dc03e..f492d9096f4237280e03edfb594138c092099cd5 100644 --- a/src/service/service/__main__.py +++ b/src/service/service/__main__.py @@ -16,10 +16,10 @@ def main(): global terminate, logger service_port = os.environ.get('SERVICESERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT) - max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) - grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD) - log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) - metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT) + max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) + grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD) + log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) + metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT ) logging.basicConfig(level=log_level) logger = logging.getLogger(__name__) @@ -36,17 +36,17 @@ def main(): database = get_database() # Starting service service - service = ServiceService(database, port=service_port, max_workers=max_workers, grace_period=grace_period) - service.start() + grpc_service = ServiceService(database, port=service_port, max_workers=max_workers, grace_period=grace_period) + grpc_service.start() # Wait for Ctrl+C or termination signal - while not terminate.wait(0.1): pass + while not terminate.wait(timeout=0.1): pass logger.info('Terminating...') - service.stop() + grpc_service.stop() logger.info('Bye') - return(0) + return 0 if __name__ == '__main__': sys.exit(main())