blob: 0c345807616b042c3304e2024aaa994ce61ee537 [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
27import traceback
28import os
29import socket
30
31from grpclib.client import Channel
32
33from osm_ee.frontend_pb2 import PrimitiveRequest, PrimitiveReply
34from osm_ee.frontend_pb2 import SshKeyRequest, SshKeyReply
35from osm_ee.frontend_grpc import FrontendExecutorStub
36
37
38async def frontend_client(host_name, port, primitive_name, params):
39
40 ip_addr = socket.gethostbyname(host_name)
41 channel = Channel(ip_addr, port)
42 try:
43 stub = FrontendExecutorStub(channel)
44
45 if (primitive_name == "get_ssh_key"):
46 print("Get ssh key")
47 reply: SshKeyReply = await stub.GetSshKey(SshKeyRequest())
48 print(reply.message)
49 else:
50 async with stub.RunPrimitive.open() as stream:
51 primitive_id = str(uuid.uuid1())
52 print("Execute primitive {}, params: {}".format(primitive_name, params))
53 await stream.send_message(
54 PrimitiveRequest(id=primitive_id, name=primitive_name, params=yaml.dump(params)), end=True)
55 async for reply in stream:
56 print(reply)
57 #replies = [reply async for reply in stream]
58 #print(replies)
59 except Exception as e:
60 print("Error executing primitive {}: {}".format(primitive_name, str(e)))
61 #print(traceback.format_exc())
62 finally:
63 channel.close()
64
65
66if __name__ == '__main__':
67
68 args = sys.argv[1:]
69 if (len(args) < 1):
70 print("Usage: host port primitive_name params")
71 else:
72 host_name = args[0]
73 port = args[1]
74 primitive_name = args[2]
75 arg_params = args[3] if len(args) >= 4 else ""
76 print(primitive_name)
77 print(arg_params)
78 params = yaml.safe_load(arg_params)
79
80 loop = asyncio.get_event_loop()
81 try:
82 task = asyncio.ensure_future(frontend_client(host_name, port, primitive_name, params))
83 loop.run_until_complete(task)
84 finally:
85 loop.close()