def service(): if GRPC: server = serve() server.service = Service() connect_str = f"localhost:{os.environ.get('PORT', '50051')}" with grpc.insecure_channel(connect_str) as ch: yield Client(channel=ch) server.stop(0) else: yield Service()
class Servicer(hbi_pb2_grpc.HostInventoryServicer): def __init__(self): self.service = Service() def CreateOrUpdate(self, host_list, context): hosts = [Host.from_pb(h) for h in host_list.hosts] ret = self.service.create_or_update(hosts) return hbi_pb2.HostList(hosts=[h.to_pb() for h in ret]) def Get(self, filter_list, context): filters = [Filter.from_pb(f) for f in filter_list.filters] ret = self.service.get(filters) return hbi_pb2.HostList(hosts=[h.to_pb() for h in ret])
def service(): if MODE == "grpc": server = serve() connect_str = f"localhost:{os.environ.get('PORT', '50051')}" with grpc.insecure_channel(connect_str) as ch: yield Client(channel=ch) server.stop(0) elif MODE == "tornado": app.service.reset() yield TornadoClient() else: yield Service()
def createClient(mode, server, port): if mode == "grpc": import grpc from hbi.client import Client print("Running in gRPC mode") return Client(host=server, port=port) elif mode == "tornado": from hbi.client import TornadoClient print("Running in REST mode") return TornadoClient(host=server, port=port) elif mode == "native": return Service() else: raise RuntimeError("The MODE envrionment property was not set")
def serve_tornado(): app = tornado.web.Application([ (r"/", RootHandler), (r"/entities/search", EntitiesSearcher), (r"/entities", EntitiesPoster), ]) app.listen(int(os.environ.get("PORT", "50051"))) app.service = Service() loop = IOLoop.current() class TornadoRunThread(Thread): def run(self): loop.start() TornadoRunThread().start() return app, loop
def service(): return Service()
def __init__(self): self.service = Service()