blob: 21203a279fe32334d16f8c4c6825602e3a55249f [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 sys
24import yaml
25import asyncio
26import uuid
lloretgalleg1d2ff512020-08-01 06:05:58 +000027import socket
28
29from grpclib.client import Channel
30
garciadeblas43fc9352024-07-09 14:30:44 +020031from osm_ee.frontend_pb2 import PrimitiveRequest
lloretgalleg1d2ff512020-08-01 06:05:58 +000032from osm_ee.frontend_pb2 import SshKeyRequest, SshKeyReply
33from osm_ee.frontend_grpc import FrontendExecutorStub
34
35
36async def frontend_client(host_name, port, primitive_name, params):
37
38 ip_addr = socket.gethostbyname(host_name)
39 channel = Channel(ip_addr, port)
40 try:
41 stub = FrontendExecutorStub(channel)
42
garciadeblas43fc9352024-07-09 14:30:44 +020043 if primitive_name == "get_ssh_key":
lloretgalleg1d2ff512020-08-01 06:05:58 +000044 print("Get ssh key")
45 reply: SshKeyReply = await stub.GetSshKey(SshKeyRequest())
46 print(reply.message)
47 else:
48 async with stub.RunPrimitive.open() as stream:
49 primitive_id = str(uuid.uuid1())
50 print("Execute primitive {}, params: {}".format(primitive_name, params))
51 await stream.send_message(
garciadeblas43fc9352024-07-09 14:30:44 +020052 PrimitiveRequest(
53 id=primitive_id, name=primitive_name, params=yaml.dump(params)
54 ),
55 end=True,
56 )
lloretgalleg1d2ff512020-08-01 06:05:58 +000057 async for reply in stream:
58 print(reply)
garciadeblas43fc9352024-07-09 14:30:44 +020059 # replies = [reply async for reply in stream]
60 # print(replies)
lloretgalleg1d2ff512020-08-01 06:05:58 +000061 except Exception as e:
62 print("Error executing primitive {}: {}".format(primitive_name, str(e)))
lloretgalleg1d2ff512020-08-01 06:05:58 +000063 finally:
64 channel.close()
65
66
garciadeblas43fc9352024-07-09 14:30:44 +020067if __name__ == "__main__":
lloretgalleg1d2ff512020-08-01 06:05:58 +000068
69 args = sys.argv[1:]
garciadeblas43fc9352024-07-09 14:30:44 +020070 if len(args) < 1:
lloretgalleg1d2ff512020-08-01 06:05:58 +000071 print("Usage: host port primitive_name params")
72 else:
73 host_name = args[0]
74 port = args[1]
75 primitive_name = args[2]
76 arg_params = args[3] if len(args) >= 4 else ""
77 print(primitive_name)
78 print(arg_params)
79 params = yaml.safe_load(arg_params)
80
81 loop = asyncio.get_event_loop()
82 try:
garciadeblas43fc9352024-07-09 14:30:44 +020083 task = asyncio.ensure_future(
84 frontend_client(host_name, port, primitive_name, params)
85 )
lloretgalleg1d2ff512020-08-01 06:05:58 +000086 loop.run_until_complete(task)
87 finally:
88 loop.close()