blob: 079503d1e9e50f6408e83df933f6e566810ec2b9 [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
36
37
38class FrontendExecutor(FrontendExecutorBase):
39
40 def __init__(self):
41 self.logger = logging.getLogger('osm_ee.frontend_server')
42 self.base_ee = BaseEE()
43
44 async def RunPrimitive(self, stream: Stream[PrimitiveRequest, PrimitiveReply]) -> None:
45 request = await stream.recv_message()
46 try:
47 self.logger.debug(f'Run primitive: id {request.id}, name: {request.name}, params: {request.params}')
48 async for status, detailed_message in self.base_ee.run_action(request.id, request.name, request.params):
49 self.logger.debug(f'Send response {status}, {detailed_message}')
50 await stream.send_message(
51 PrimitiveReply(status=status, detailed_message=detailed_message))
52 except Exception as e:
53 self.logger.debug(f'Error executing primitive: id {request.id}, name: {request.name}, error_msg: {str(e)}')
54 await stream.send_message(
55 PrimitiveReply(status="ERROR", detailed_message=str(e)))
56
57 async def GetSshKey(self, stream: Stream[SshKeyRequest, SshKeyReply]) -> None:
58 request = await stream.recv_message()
59 assert request is not None
60 message = await self.base_ee.get_ssh_key()
61 await stream.send_message(SshKeyReply(message=message))
62
63
64async def main(*, host: str = '0.0.0.0', port: int = 50051) -> None:
65 logging.basicConfig()
66 logger = logging.getLogger('osm_ee')
67 logger.setLevel(logging.DEBUG)
68
69 # Generate ssh key
70 file_dir = os.path.expanduser("~/.ssh/id_rsa")
71 command = "ssh-keygen -q -t rsa -N '' -f {}".format(file_dir)
72 return_code, stdout, stderr = await util_ee.local_async_exec(command)
73 logger.debug("Generated ssh_key, return_code: {}".format(return_code))
74
75 # Start server
76 server = Server([FrontendExecutor()])
77 with graceful_exit([server]):
78 await server.start(host, port)
79 logging.getLogger('osm_ee.frontend_server').debug(f'Serving on {host}:{port}')
80 await server.wait_closed()
81
82
83if __name__ == '__main__':
84 loop = asyncio.get_event_loop()
85 try:
86 main_task = asyncio.ensure_future(main())
87 loop.run_until_complete(main_task)
88 finally:
89 loop.close()