blob: 32b3afe279db973153cfc6802f6cbd2784bb2676 [file] [log] [blame]
lloretgalleg1d2ff512020-08-01 06:05:58 +00001##
2# Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U.
3# This file is part of OSM
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15# implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# For those usages not covered by the Apache License, Version 2.0 please
20# contact with: nfvlabs@tid.es
21##
22
23import asyncio
24import logging
25import os
26
27from grpclib.utils import graceful_exit
28from grpclib.server import Server, Stream
29
30from osm_ee.frontend_grpc import FrontendExecutorBase
31from osm_ee.frontend_pb2 import PrimitiveRequest, PrimitiveReply
32from osm_ee.frontend_pb2 import SshKeyRequest, SshKeyReply
33
34from osm_ee.base_ee import BaseEE
35import osm_ee.util.util_ee as util_ee
Gabriel Cuba8b7a3952022-11-02 17:21:50 -050036import osm_ee.util.util_grpc as util_grpc
lloretgalleg1d2ff512020-08-01 06:05:58 +000037
38
39class FrontendExecutor(FrontendExecutorBase):
40
41 def __init__(self):
42 self.logger = logging.getLogger('osm_ee.frontend_server')
43 self.base_ee = BaseEE()
44
45 async def RunPrimitive(self, stream: Stream[PrimitiveRequest, PrimitiveReply]) -> None:
46 request = await stream.recv_message()
47 try:
48 self.logger.debug(f'Run primitive: id {request.id}, name: {request.name}, params: {request.params}')
49 async for status, detailed_message in self.base_ee.run_action(request.id, request.name, request.params):
50 self.logger.debug(f'Send response {status}, {detailed_message}')
51 await stream.send_message(
52 PrimitiveReply(status=status, detailed_message=detailed_message))
53 except Exception as e:
54 self.logger.debug(f'Error executing primitive: id {request.id}, name: {request.name}, error_msg: {str(e)}')
55 await stream.send_message(
56 PrimitiveReply(status="ERROR", detailed_message=str(e)))
57
58 async def GetSshKey(self, stream: Stream[SshKeyRequest, SshKeyReply]) -> None:
59 request = await stream.recv_message()
60 assert request is not None
61 message = await self.base_ee.get_ssh_key()
62 await stream.send_message(SshKeyReply(message=message))
63
64
65async def main(*, host: str = '0.0.0.0', port: int = 50051) -> None:
66 logging.basicConfig()
67 logger = logging.getLogger('osm_ee')
68 logger.setLevel(logging.DEBUG)
69
70 # Generate ssh key
71 file_dir = os.path.expanduser("~/.ssh/id_rsa")
72 command = "ssh-keygen -q -t rsa -N '' -f {}".format(file_dir)
73 return_code, stdout, stderr = await util_ee.local_async_exec(command)
74 logger.debug("Generated ssh_key, return_code: {}".format(return_code))
75
76 # Start server
77 server = Server([FrontendExecutor()])
78 with graceful_exit([server]):
Gabriel Cuba8b7a3952022-11-02 17:21:50 -050079 await server.start(host, port, ssl=util_grpc.create_secure_context())
lloretgalleg1d2ff512020-08-01 06:05:58 +000080 logging.getLogger('osm_ee.frontend_server').debug(f'Serving on {host}:{port}')
81 await server.wait_closed()
82
83
84if __name__ == '__main__':
85 loop = asyncio.get_event_loop()
86 try:
87 main_task = asyncio.ensure_future(main())
88 loop.run_until_complete(main_task)
89 finally:
90 loop.close()