blob: a36594fb0b9c944e352215f8a1ee8b7039edce68 [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):
garciadeblas43fc9352024-07-09 14:30:44 +020042 self.logger = logging.getLogger("osm_ee.frontend_server")
lloretgalleg1d2ff512020-08-01 06:05:58 +000043 self.base_ee = BaseEE()
44
garciadeblas43fc9352024-07-09 14:30:44 +020045 async def RunPrimitive(
46 self, stream: Stream[PrimitiveRequest, PrimitiveReply]
47 ) -> None:
lloretgalleg1d2ff512020-08-01 06:05:58 +000048 request = await stream.recv_message()
49 try:
garciadeblas43fc9352024-07-09 14:30:44 +020050 self.logger.debug(
51 f"Run primitive: id {request.id}, name: {request.name}, params: {request.params}"
52 )
53 async for status, detailed_message in self.base_ee.run_action(
54 request.id, request.name, request.params
55 ):
56 self.logger.debug(f"Send response {status}, {detailed_message}")
lloretgalleg1d2ff512020-08-01 06:05:58 +000057 await stream.send_message(
garciadeblas43fc9352024-07-09 14:30:44 +020058 PrimitiveReply(status=status, detailed_message=detailed_message)
59 )
lloretgalleg1d2ff512020-08-01 06:05:58 +000060 except Exception as e:
garciadeblas43fc9352024-07-09 14:30:44 +020061 self.logger.debug(
62 f"Error executing primitive: id {request.id}, name: {request.name}, error_msg: {str(e)}"
63 )
lloretgalleg1d2ff512020-08-01 06:05:58 +000064 await stream.send_message(
garciadeblas43fc9352024-07-09 14:30:44 +020065 PrimitiveReply(status="ERROR", detailed_message=str(e))
66 )
lloretgalleg1d2ff512020-08-01 06:05:58 +000067
68 async def GetSshKey(self, stream: Stream[SshKeyRequest, SshKeyReply]) -> None:
69 request = await stream.recv_message()
70 assert request is not None
71 message = await self.base_ee.get_ssh_key()
72 await stream.send_message(SshKeyReply(message=message))
73
74
garciadeblas43fc9352024-07-09 14:30:44 +020075async def main(*, host: str = "0.0.0.0", port: int = 50051) -> None:
lloretgalleg1d2ff512020-08-01 06:05:58 +000076 logging.basicConfig()
garciadeblas43fc9352024-07-09 14:30:44 +020077 logger = logging.getLogger("osm_ee")
lloretgalleg1d2ff512020-08-01 06:05:58 +000078 logger.setLevel(logging.DEBUG)
79
80 # Generate ssh key
81 file_dir = os.path.expanduser("~/.ssh/id_rsa")
82 command = "ssh-keygen -q -t rsa -N '' -f {}".format(file_dir)
83 return_code, stdout, stderr = await util_ee.local_async_exec(command)
84 logger.debug("Generated ssh_key, return_code: {}".format(return_code))
85
86 # Start server
87 server = Server([FrontendExecutor()])
88 with graceful_exit([server]):
Gabriel Cuba8b7a3952022-11-02 17:21:50 -050089 await server.start(host, port, ssl=util_grpc.create_secure_context())
garciadeblas43fc9352024-07-09 14:30:44 +020090 logging.getLogger("osm_ee.frontend_server").debug(f"Serving on {host}:{port}")
lloretgalleg1d2ff512020-08-01 06:05:58 +000091 await server.wait_closed()
92
93
garciadeblas43fc9352024-07-09 14:30:44 +020094if __name__ == "__main__":
lloretgalleg1d2ff512020-08-01 06:05:58 +000095 loop = asyncio.get_event_loop()
96 try:
97 main_task = asyncio.ensure_future(main())
98 loop.run_until_complete(main_task)
99 finally:
100 loop.close()