예제 #1
0
    async def get_and_subscribe(self, path, protobuf_type, callback):
        if self._channel == None:
            self._channel = Channel(self._host, self._port)

        stream = (await ProtoconfServiceStub(
            self._channel).SubscribeForConfig.open().__aenter__())
        await stream.send_message(ConfigSubscriptionRequest(path=path))

        async def get_config():
            protoconf_value = await stream.recv_message()
            if protoconf_value == None:
                return None

            config = protobuf_type()
            protoconf_value.value.Unpack(config)
            return config

        config = await get_config()
        if config != None and callback != None:
            self._streams.append(stream)

            async def get_updates():
                while True:
                    config = await get_config()
                    if config == None:
                        break
                    asyncio.ensure_future(callback(config))

            self._updates_tasks.append(asyncio.ensure_future(get_updates()))
        else:
            await stream.cancel()
        return config
예제 #2
0
파일: infra.py 프로젝트: raphaelvrosa/gym
async def callScenario(command, test, vnfbd):
    deploy_dict = {
        "id": test,
        "workflow": command,
        "scenario": vnfbd.scenario(),
    }
    deploy = json_format.ParseDict(deploy_dict, gym_pb2.Deploy())
    
    environment = vnfbd.environment()
    env_plugin = environment.get("plugin")
    env_params = env_plugin.get("parameters")
    address = env_params.get("address").get("value")
    host, port = address.split(":")

    channel = Channel(host, port)
    stub = gym_grpc.InfraStub(channel)
    built = await stub.Run(deploy)

    if built.error:
        ack = False
        print(f'Scenario not deployed error: {built.error}')
    else:
        ack = True
        print(f'Scenario deployed: {built.ack}')


    channel.close()
    return ack
예제 #3
0
파일: tests.py 프로젝트: renaissanxe/umbra
        async def connect_to_agent():
            actions = [{
                'id': "1",
                "tool": "ping",
                "output": {
                    "live": False,
                    "address": None,
                },
                'parameters': {
                    "target": "peer0.org1.example.com",
                    "interval": "1",
                    "duration": "3",
                },
                'schedule': {
                    "from": 0,
                    "until": 14,
                    "duration": 0,
                    "interval": 2,
                    "repeat": 1
                },
            }]

            inst_dict = {
                "id": "100",
                "actions": actions,
            }

            channel = Channel("192.168.0.13", 8910)
            stub = AgentStub(channel)

            instruction = json_format.ParseDict(inst_dict, Instruction())
            reply = await stub.Probe(instruction)
            print("DONE connect_to_agent reply =", reply)
            channel.close()
예제 #4
0
    async def call_monitor(self, address, data):
        logger.info(f"Calling Monitor - {address}")

        directrix = json_format.ParseDict(data, Directrix())
        host, port = address.split(":")

        try:
            channel = Channel(host, port)
            stub = MonitorStub(channel)
            status = await stub.Measure(directrix)

        except Exception as e:
            ack = False
            info = repr(e)
            logger.info(f"Error - monitor failed - {info}")
        else:
            if status.error:
                ack = False
                logger.info(f"Monitor error: {status.error}")
                info = status.error
            else:
                ack = True
                if status.info:
                    info = self.parse_bytes(status.info)
                else:
                    info = {}
                logger.info(f"Monitor info: {info}")
        finally:
            channel.close()

        return ack, info
예제 #5
0
class ProtoconfMutation(object):
    def __init__(self, host="127.0.0.1", port=SERVER_DEFAULT_PORT):
        self._host = host
        self._port = port
        self._clear_state()

    def _clear_state(self):
        self._channel = None

    async def mutate_config(self, path, value, script_metadata):
        if self._channel == None:
            self._channel = Channel(self._host, self._port)

        protoconf_service = ProtoconfMutationServiceStub(self._channel)

        config = ProtoconfValue()
        config.proto_file = value.DESCRIPTOR.file.name
        config.value.Pack(value)

        await protoconf_service.MutateConfig(
            ConfigMutationRequest(path=path,
                                  value=config,
                                  script_metadata=script_metadata))

    def close(self):
        self._channel.close()
        self._clear_state()
예제 #6
0
파일: tools.py 프로젝트: serial-coder/umbra
    async def _send(self, address, message):
        logger.info(f"Sending message Stats")

        try:
            host, port = address.split(":")
            channel = Channel(host, port)
            stub = BrokerStub(channel)

            info = json_format.ParseDict(message, Stats())
            reply = await stub.Collect(info)
            info_reply = json_format.MessageToDict(
                reply, preserving_proto_field_name=True
            )

        except GRPCError as e:
            logger.info(f"Error in reaching: Stats")
            logger.debug(f"Exception in stats: {repr(e)}")
            info_reply = {}

        except OSError as e:
            logger.info(f"Could not reach channel for Stats")
            logger.debug(f"Exception: {repr(e)}")
            info_reply = {}

        except Exception as e:
            logger.debug(f"Exception: {repr(e)}")
            info_reply = {}

        finally:
            channel.close()
            logger.info(f"Reply message Stats {info_reply}")
예제 #7
0
    def __init__(self):
        self._host = os.environ.get("LND_HOST")
        self._port = int(os.environ.get("LND_PORT"))
        self._network = os.environ.get("NETWORK")
        self.id_pubkey = None
        self.stub = None

        os.environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA"
        os.environ["SSL_CERT_DIR"] = "/root/.lnd"

        with open(
                f"/root/.lnd/data/chain/bitcoin/{self._network}/admin.macaroon",
                "rb") as macaroon_bytes:
            self._macaroon = macaroon_bytes.read().hex()

        ctx = ssl.SSLContext(ssl.PROTOCOL_TLS)
        ctx.verify_mode = ssl.CERT_REQUIRED
        ctx.load_verify_locations(cafile="/root/.lnd/tls.cert")
        # can take second arg path the private key str(client_key)
        ctx.load_cert_chain("/root/.lnd/tls.cert", "/root/.lnd/tls.key")
        # ctx.load_verify_locations(str(trusted)) WE TRUST THE CERTIFICATE
        ctx.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1
        ctx.set_ciphers("ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20")
        ctx.set_alpn_protocols(["h2"])
        try:
            ctx.set_npn_protocols(["h2"])
        except NotImplementedError:
            pass
        self._channel = Channel(self._host, self._port, ssl=ctx)

        async def attach_metadata(event: SendRequest):
            event.metadata["macaroon"] = self._macaroon

        listen(self._channel, SendRequest, attach_metadata)
예제 #8
0
    async def run_directrix(self):
        uuid, address = "monitor-test", "127.0.0.1:50051"
        monitor_p = self.start_monitor(uuid, address)

        uuid, address = "broker-test", "127.0.0.1:50052"
        broker_p = self.start_broker(uuid, address)

        try:
            await asyncio.sleep(1.0)
            channel = Channel("127.0.0.1", 50051)
            stub = MonitorStub(channel)

            action = "start"
            info_reply = await self.call_directrix(stub, action)

            await asyncio.sleep(10)

            action = "stop"
            info_reply = await self.call_directrix(stub, action)

        except Exception as e:
            logger.info(f"run directrix exception - {repr(e)}")
            info_reply = None

        finally:
            channel.close()

        ack = self.stop_monitor(monitor_p)
        assert ack == True

        ack = self.stop_broker(broker_p)
        assert ack == True

        return info_reply
예제 #9
0
class UnixClientServer:
    temp = None
    sock = None
    server = None
    channel = None

    async def __aenter__(self):
        self.temp = tempfile.mkdtemp()
        self.sock = os.path.join(self.temp, 'grpclib.sock')

        dummy_service = DummyService()

        self.server = Server([dummy_service])
        await self.server.start(path=self.sock)

        self.channel = Channel(path=self.sock)
        dummy_stub = DummyServiceStub(self.channel)
        return dummy_service, dummy_stub

    async def __aexit__(self, *exc_info):
        self.server.close()
        await self.server.wait_closed()
        self.channel.close()
        if os.path.exists(self.sock):
            os.unlink(self.sock)
        if os.path.exists(self.temp):
            os.rmdir(self.temp)
예제 #10
0
파일: grpc.py 프로젝트: zeng4250538/idb
 async def get_stub(self) -> AsyncContextManager[CompanionServiceStub]:
     await self.spawn_notifier()
     channel: Optional[Channel] = None
     try:
         try:
             self.companion_info = self.direct_companion_manager.get_companion_info(
                 target_udid=self.target_udid
             )
         except IdbException as e:
             # will try to spawn a companion if on mac.
             companion_info = await self.spawn_companion(
                 target_udid=none_throws(self.target_udid)
             )
             if companion_info:
                 self.companion_info = companion_info
             else:
                 raise e
         self.logger.info(f"using companion {self.companion_info}")
         channel = Channel(
             # pyre-fixme[16]: `Optional` has no attribute `host`.
             self.companion_info.host,
             # pyre-fixme[16]: `Optional` has no attribute `port`.
             self.companion_info.port,
             loop=asyncio.get_event_loop(),
         )
         yield CompanionServiceStub(channel=channel)
     finally:
         if channel:
             channel.close()
예제 #11
0
 async def get_stub(self) -> CompanionServiceStub:
     channel: Optional[Channel] = None
     try:
         try:
             self.companion_info = self.direct_companion_manager.get_companion_info(
                 target_udid=self.target_udid
             )
         except IdbException as e:
             # will try to spawn a companion if on mac.
             if platform.system() == "Darwin" and self.target_udid:
                 self.logger.info(
                     f"will attempt to spawn a companion for {self.target_udid}"
                 )
                 port = await self.companion_spawner.spawn_companion(
                     target_udid=self.target_udid
                 )
                 host = "localhost"
                 self.companion_info = CompanionInfo(
                     host=host, port=port, udid=self.target_udid, is_local=True
                 )
                 self.direct_companion_manager.add_companion(self.companion_info)
             else:
                 raise e
         self.logger.info(f"using companion {self.companion_info}")
         channel = Channel(
             self.companion_info.host,
             self.companion_info.port,
             loop=asyncio.get_event_loop(),
         )
         yield CompanionServiceStub(channel=channel)
     finally:
         if channel:
             channel.close()
예제 #12
0
class ClientServer:
    server = None
    channel = None

    def __init__(self, *, loop):
        self.loop = loop

    async def __aenter__(self):
        host = '127.0.0.1'
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            s.bind(('127.0.0.1', 0))
            _, port = s.getsockname()

        dummy_service = DummyService()

        self.server = Server([dummy_service], loop=self.loop)
        await self.server.start(host, port)

        self.channel = Channel(host=host, port=port, loop=self.loop)
        dummy_stub = DummyServiceStub(self.channel)
        return dummy_service, dummy_stub

    async def __aexit__(self, *exc_info):
        self.server.close()
        await self.server.wait_closed()
        self.channel.close()
예제 #13
0
    def __init__(
            self,
            addr: str,
            port: int,
            pem: str = None,
            channel_options: dict = None,
            loop: asyncio.AbstractEventLoop = None,
        ):

        self.addr = addr
        self.port = port

        if channel_options is None:
            channel_options = {}
        if pem is None:
            make_sync_channel = partial(grpc.insecure_channel, options=channel_options)
        else:
            creds = grpc.ssl_channel_credentials(pem)
            make_sync_channel = partial(
                grpc.secure_channel,
                credentials=creds,
                options=channel_options,
            )

        if loop is None:
            loop = asyncio.get_event_loop()

        self.sync_channel = make_sync_channel(f"{addr}:{port}")
        self.async_channel = Channel(addr, port, loop=loop)

        self.sync_stub = prediction_service_pb2_grpc.PredictionServiceStub(self.sync_channel)
        self.async_stub = prediction_service_grpc.PredictionServiceStub(self.async_channel)
예제 #14
0
async def main(index):
    channel = Channel('127.0.0.1', 50051)
    client = DemoServiceStub(channel)

    response: demo_pb2.ResponseData = await client.ApiCreate(
        demo_pb2.RequestData(data="call ApiCreate from client " + str(index), )
    )
    print(response.return_code, response.message, response.data)

    # response: demo_pb2.ResponseData = await client.ApiList(demo_pb2.RequestData(
    #     data="call ApiList from client",
    # ))
    # print(response.return_code, response.message, response.data)
    #
    # response: demo_pb2.ResponseData = await client.ApiUpdate(demo_pb2.RequestData(
    #     data="call ApiUpdate from client",
    # ))
    # print(response.return_code, response.message, response.data)
    #
    # response: demo_pb2.ResponseData = await client.ApiDelete(demo_pb2.RequestData(
    #     data="call ApiDelete from client",
    # ))
    # print(response.return_code, response.message, response.data)

    channel.close()
예제 #15
0
async def _wait_for_channel(
    host: str,
    port: int,
    timeout: float = CONNECTION_TIMEOUT,
    ssl_context: Optional[ssl.SSLContext] = None,
) -> Channel:
    logger.debug('Starting service health check')
    request = HealthCheckRequest()
    request.service = ''  # empty service name for a server check

    deadline = time.time() + timeout
    while time.time() < deadline:
        channel = Channel(host,
                          port,
                          ssl=ssl_context,
                          loop=asyncio.get_event_loop())
        client = HealthStub(channel)
        try:
            response = await client.Check(request)
            if response.status == HealthCheckResponse.SERVING:
                logger.debug('Service health check completed with success')
                return channel
        except (StreamTerminatedError, ConnectionError):
            pass
        channel.close()
        sleep_time = 0.1
        logger.debug(
            'Service health check failed, will try again in %fs. deadline=%r',
            sleep_time,
            deadline,
        )
        await asyncio.sleep(sleep_time)

    raise TimeoutError
예제 #16
0
파일: grpc.py 프로젝트: zeng4250538/idb
 async def connect(
     self,
     destination: ConnectionDestination,
     metadata: Optional[Dict[str, str]] = None,
 ) -> CompanionInfo:
     self.logger.debug(f"Connecting directly to {destination} with meta {metadata}")
     if isinstance(destination, Address):
         channel = Channel(
             destination.host, destination.port, loop=asyncio.get_event_loop()
         )
         stub = CompanionServiceStub(channel=channel)
         with tempfile.NamedTemporaryFile(mode="w+b") as f:
             response = await stub.connect(
                 ConnectRequest(
                     destination=destination_to_grpc(destination),
                     metadata=metadata,
                     local_file_path=f.name,
                 )
             )
         companion = CompanionInfo(
             udid=response.companion.udid,
             host=destination.host,
             port=destination.port,
             is_local=response.companion.is_local,
         )
         self.logger.debug(f"Connected directly to {companion}")
         self.direct_companion_manager.add_companion(companion)
         channel.close()
         return companion
     else:
         companion = await self.spawn_companion(target_udid=destination)
         if companion:
             return companion
         else:
             raise IdbException(f"can't find target for udid {destination}")
예제 #17
0
class LightningStub(LoggerMixin):
    """lightning stub manager"""
    def __init__(self):
        self._host = os.environ.get("LND_HOST")
        self._port = int(os.environ.get("LND_PORT"))
        self._network = os.environ.get("NETWORK")
        self.id_pubkey = None
        self.stub = None

        os.environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA"
        os.environ["SSL_CERT_DIR"] = "/root/.lnd"

        with open(
                f"/root/.lnd/data/chain/bitcoin/{self._network}/admin.macaroon",
                "rb") as macaroon_bytes:
            self._macaroon = macaroon_bytes.read().hex()

        ctx = ssl.SSLContext(ssl.PROTOCOL_TLS)
        ctx.verify_mode = ssl.CERT_REQUIRED
        ctx.load_verify_locations(cafile="/root/.lnd/tls.cert")
        # can take second arg path the private key str(client_key)
        ctx.load_cert_chain("/root/.lnd/tls.cert", "/root/.lnd/tls.key")
        # ctx.load_verify_locations(str(trusted)) WE TRUST THE CERTIFICATE
        ctx.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1
        ctx.set_ciphers("ECDHE+AESGCM:ECDHE+CHACHA20:DHE+AESGCM:DHE+CHACHA20")
        ctx.set_alpn_protocols(["h2"])
        try:
            ctx.set_npn_protocols(["h2"])
        except NotImplementedError:
            pass
        self._channel = Channel(self._host, self._port, ssl=ctx)

        async def attach_metadata(event: SendRequest):
            event.metadata["macaroon"] = self._macaroon

        listen(self._channel, SendRequest, attach_metadata)

    async def initialize(self) -> None:
        """asynchronously init class and populate pubkey"""

        # TODO add wallet unlocking stub for wallet unlock
        # TODO max receive message length? = 1024^3
        i = 1
        while True:
            try:
                self.logger.info(f"Attempt ${i} to initialize lnd")
                self.stub = lnrpc.LightningStub(self._channel)
                req = ln.GetInfoRequest()
                info = await self.stub.GetInfo(req)
                self.id_pubkey = info.identity_pubkey
                assert self.id_pubkey
                self.logger.info("Success")
                break
            except (ConnectionRefusedError, gaierror) as error:
                i += 1
                self.logger.info(f"Attempt failed: {error}")
                await asyncio.sleep(5)

    def destroy(self) -> None:
        self._channel.close()
async def green() -> dict:
    green_channel = Channel(host="127.0.0.1", port=50052)
    service = GreenStub(green_channel)
    response = await service.green()
    green_channel.close()
    print(response.to_dict())
    return response.to_dict()
예제 #19
0
 async def build(
     cls,
     address: Address,
     logger: logging.Logger,
     is_local: Optional[bool] = None,
     exchange_metadata: bool = True,
     extra_metadata: Optional[Dict[str, str]] = None,
     use_tls: bool = False,
 ) -> AsyncGenerator["Client", None]:
     metadata_to_companion = (
         {
             **{
                 key: value
                 for (key, value) in plugin.resolve_metadata(logger=logger).items()
                 if isinstance(value, str)
             },
             **(extra_metadata or {}),
         }
         if exchange_metadata
         else {}
     )
     ssl_context = plugin.channel_ssl_context() if use_tls else None
     if use_tls:
         assert ssl_context is not None
     async with (
         Channel(
             host=address.host,
             port=address.port,
             loop=asyncio.get_event_loop(),
             ssl=ssl_context,
         )
         if isinstance(address, TCPAddress)
         else Channel(path=address.path, loop=asyncio.get_event_loop())
     ) as channel:
         stub = CompanionServiceStub(channel=channel)
         with tempfile.NamedTemporaryFile(mode="w+b") as f:
             try:
                 response = await stub.connect(
                     ConnectRequest(
                         metadata=metadata_to_companion, local_file_path=f.name
                     )
                 )
             except Exception as ex:
                 raise IdbException(
                     f"Failed to connect to companion at address {address}: {ex}"
                 )
         companion = companion_to_py(
             companion=response.companion, address=address, is_local=is_local
         )
         if exchange_metadata:
             metadata_from_companion = {
                 key: value
                 for (key, value) in companion.metadata.items()
                 if isinstance(value, str)
             }
             plugin.append_companion_metadata(
                 logger=logger, metadata=metadata_from_companion
             )
         yield Client(stub=stub, companion=companion, logger=logger)
예제 #20
0
async def main(*, host: str = 'localhost', port: int = 50051) -> None:
    channel = Channel(host, port, ssl=create_secure_context(
        CLIENT_CERT, CLIENT_KEY, trusted=SERVER_CERT,
    ))
    stub = HealthStub(channel)
    response = await stub.Check(HealthCheckRequest())
    print(response)
    channel.close()
예제 #21
0
파일: client.py 프로젝트: zidanessf/PSJULIA
async def main():
    channel = Channel('127.0.0.1', 19999)
    srvc = TestMathStub(channel)

    reply: BinaryOpResp = await srvc.Add(BinaryOpReq(i1=1, i2=1))
    print(reply.result)

    channel.close()
예제 #22
0
async def run_info(address, port):

    channel = Channel(address, port)
    stub = gym_grpc.AgentStub(channel)
    info_reply = await call_info(stub)
    channel.close()

    return info_reply
예제 #23
0
파일: client.py 프로젝트: twds/grpclib
async def main() -> None:
    channel = Channel('127.0.0.1', 50051)
    greeter = GreeterStub(channel)

    reply: HelloReply = await greeter.SayHello(HelloRequest(name='Dr. Strange'))
    print(reply.message)

    channel.close()
예제 #24
0
 def __init__(self, server_url: str, **kwargs):
     self._server_url = server_url
     parsed = urlparse(self._server_url)
     self._username = parsed.username or ''
     self._password = parsed.password or ''
     host, p = parsed.netloc.split(':')
     port = int(p)
     self._channel = Channel(host, port, **kwargs)
     self.handlers = {}
예제 #25
0
    def __init__(self, host='127.0.0.1', port=2379, *, ttl=10):
        self._channel = Channel(host, port)

        self._kvstub = _etcd.KVStub(self._channel)
        self._leasestub = _etcd.LeaseStub(self._channel)
        self._watchstub = _etcd.WatchStub(self._channel)

        from .lease import Lease
        self._session = Lease(ttl, client=self)
예제 #26
0
 async def _get_ssh_key(self, ip_addr):
     channel = Channel(ip_addr, self._ee_service_port)
     try:
         stub = FrontendExecutorStub(channel)
         self.log.debug("get ssh key, ip_addr: {}".format(ip_addr))
         reply: SshKeyReply = await stub.GetSshKey(SshKeyRequest())
         return reply.message
     finally:
         channel.close()
예제 #27
0
def server(socket, use_ssl=False):
    channel = Channel(path=socket, ssl=use_ssl)
    client = PoreRefinerStub(channel)
    try:
        yield client
    except GRPCError as e:
        raise
    finally:
        channel.close()
예제 #28
0
async def main() -> None:
    channel = Channel('127.0.0.1', 50051)
    listen(channel, SendRequest, on_send_request)

    stub = GreeterStub(channel)
    response = await stub.SayHello(HelloRequest(name='World'))
    print(response.message)

    channel.close()
async def rec() -> dict:
    recommendations_channel = Channel(host="127.0.0.1", port=50051)
    service = RecommendationsStub(recommendations_channel)
    response = await service.recommend(user_id=1,
                                       category=BookCategory.MYSTERY,
                                       max_results=3)
    recommendations_channel.close()

    return response.to_dict()
예제 #30
0
 async def _companion_to_target(
         self, companion: CompanionInfo) -> TargetDescription:
     channel = Channel(companion.host,
                       companion.port,
                       loop=asyncio.get_event_loop())
     stub = CompanionServiceStub(channel=channel)
     response = await stub.describe(TargetDescriptionRequest())
     channel.close()
     return target_to_py(response.target_description)
예제 #31
0
파일: __init__.py 프로젝트: icgood/pymap
async def run(parser: ArgumentParser, args: Namespace,
              command_cls: Type[ClientCommand]) -> int:
    loop = asyncio.get_event_loop()
    path = args.socket or AdminService.get_socket_path()
    channel = Channel(path=path, loop=loop)
    stub = AdminStub(channel)
    command = command_cls(stub, args)
    try:
        code = await command.run(args.outfile)
    finally:
        channel.close()
    return code