Skip to content
Snippets Groups Projects
Commit be0e1527 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Added REST-API to Context service and updated service manifest files.

parent 9ec6b204
No related branches found
No related tags found
1 merge request!54Release 2.0.0
Showing with 127 additions and 31 deletions
...@@ -18,6 +18,7 @@ spec: ...@@ -18,6 +18,7 @@ spec:
imagePullPolicy: Always imagePullPolicy: Always
ports: ports:
- containerPort: 1010 - containerPort: 1010
- containerPort: 8080
env: env:
- name: DB_ENGINE - name: DB_ENGINE
value: "redis" value: "redis"
...@@ -51,3 +52,27 @@ spec: ...@@ -51,3 +52,27 @@ spec:
- name: grpc - name: grpc
port: 1010 port: 1010
targetPort: 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
---
...@@ -49,5 +49,6 @@ spec: ...@@ -49,5 +49,6 @@ spec:
app: deviceservice app: deviceservice
ports: ports:
- name: grpc - name: grpc
protocol: TCP
port: 2020 port: 2020
targetPort: 2020 targetPort: 2020
...@@ -46,5 +46,6 @@ spec: ...@@ -46,5 +46,6 @@ spec:
app: monitoringservice app: monitoringservice
ports: ports:
- name: grpc - name: grpc
protocol: TCP
port: 8080 port: 8080
targetPort: 8080 targetPort: 8080
...@@ -36,3 +36,19 @@ spec: ...@@ -36,3 +36,19 @@ spec:
port: 6379 port: 6379
targetPort: 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
---
...@@ -49,5 +49,6 @@ spec: ...@@ -49,5 +49,6 @@ spec:
app: serviceservice app: serviceservice
ports: ports:
- name: grpc - name: grpc
protocol: TCP
port: 3030 port: 3030
targetPort: 3030 targetPort: 3030
...@@ -8,8 +8,9 @@ GRPC_SERVICE_PORT = 1010 ...@@ -8,8 +8,9 @@ GRPC_SERVICE_PORT = 1010
GRPC_MAX_WORKERS = 10 GRPC_MAX_WORKERS = 10
GRPC_GRACE_PERIOD = 60 GRPC_GRACE_PERIOD = 60
# HTTP settings # REST-API settings
HTTP_SERVICE_PORT = 8080 RESTAPI_SERVICE_PORT = 8080
RESTAPI_BASE_URL = '/api'
# Prometheus settings # Prometheus settings
METRICS_PORT = 9192 METRICS_PORT = 9192
flask-restful
grpcio-health-checking grpcio-health-checking
grpcio grpcio
prometheus-client prometheus-client
......
import logging, os, signal, sys, threading import logging, os, signal, sys, threading
from prometheus_client import start_http_server from prometheus_client import start_http_server
from common.database.Factory import get_database 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.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() terminate = threading.Event()
logger = None logger = None
...@@ -15,11 +18,13 @@ def signal_handler(signal, frame): ...@@ -15,11 +18,13 @@ def signal_handler(signal, frame):
def main(): def main():
global terminate, logger global terminate, logger
service_port = os.environ.get('CONTEXTSERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT) grpc_service_port = os.environ.get('CONTEXTSERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT )
max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS )
grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD) grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD )
log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL )
metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT) 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) logging.basicConfig(level=log_level)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -36,17 +41,23 @@ def main(): ...@@ -36,17 +41,23 @@ def main():
database = get_database() database = get_database()
# Starting context service # Starting context service
service = ContextService(database, port=service_port, max_workers=max_workers, grace_period=grace_period) grpc_service = ContextService(database, port=grpc_service_port, max_workers=max_workers, grace_period=grace_period)
service.start() 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 # 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...') logger.info('Terminating...')
service.stop() grpc_service.stop()
rest_server.shutdown()
rest_server.join()
logger.info('Bye') logger.info('Bye')
return(0) return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())
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()
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())
...@@ -16,10 +16,10 @@ def main(): ...@@ -16,10 +16,10 @@ def main():
global terminate, logger global terminate, logger
service_port = os.environ.get('DEVICESERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT) service_port = os.environ.get('DEVICESERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT)
max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS )
grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD) grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD)
log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL )
metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT) metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT )
logging.basicConfig(level=log_level) logging.basicConfig(level=log_level)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -36,17 +36,17 @@ def main(): ...@@ -36,17 +36,17 @@ def main():
database = get_database() database = get_database()
# Starting device service # Starting device service
service = DeviceService(database, port=service_port, max_workers=max_workers, grace_period=grace_period) grpc_service = DeviceService(database, port=service_port, max_workers=max_workers, grace_period=grace_period)
service.start() grpc_service.start()
# Wait for Ctrl+C or termination signal # 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...') logger.info('Terminating...')
service.stop() grpc_service.stop()
logger.info('Bye') logger.info('Bye')
return(0) return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())
...@@ -16,10 +16,10 @@ def main(): ...@@ -16,10 +16,10 @@ def main():
global terminate, logger global terminate, logger
service_port = os.environ.get('SERVICESERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT) service_port = os.environ.get('SERVICESERVICE_SERVICE_PORT_GRPC', GRPC_SERVICE_PORT)
max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS ) max_workers = os.environ.get('MAX_WORKERS', GRPC_MAX_WORKERS )
grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD) grace_period = os.environ.get('GRACE_PERIOD', GRPC_GRACE_PERIOD)
log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL ) log_level = os.environ.get('LOG_LEVEL', LOG_LEVEL )
metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT) metrics_port = os.environ.get('METRICS_PORT', METRICS_PORT )
logging.basicConfig(level=log_level) logging.basicConfig(level=log_level)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -36,17 +36,17 @@ def main(): ...@@ -36,17 +36,17 @@ def main():
database = get_database() database = get_database()
# Starting service service # Starting service service
service = ServiceService(database, port=service_port, max_workers=max_workers, grace_period=grace_period) grpc_service = ServiceService(database, port=service_port, max_workers=max_workers, grace_period=grace_period)
service.start() grpc_service.start()
# Wait for Ctrl+C or termination signal # 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...') logger.info('Terminating...')
service.stop() grpc_service.stop()
logger.info('Bye') logger.info('Bye')
return(0) return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment