Beispiel #1
0
def serve(cert_file, key_file, gnmi_url, servicer, max_workers=10):
    """Create and run a gNMI service"""
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers))
    add_gNMIServicer_to_server(servicer, server)
    with open(cert_file) as certs:
        cert = certs.read().encode('utf8')
    with open(key_file) as keys:
        key = keys.read().encode('utf8')
    credentials = ssl_server_credentials([(key, cert)])
    server.add_secure_port(gnmi_url, credentials)
    server.start()
    try:
        while True:
            sleep(3600)  # how long should we sleep? Hmm
    except KeyboardInterrupt:
        server.stop(0)
    def __init__(self, targetUrl, tlsEnabled, caCertPath, privKeyPath):
        self.grpcServer = grpc.server(
            futures.ThreadPoolExecutor(max_workers=10))
        gnmi_pb2_grpc.add_gNMIServicer_to_server(gNMITargetServicer(),
                                                 self.grpcServer)

        if tlsEnabled == True:
            # secure connection
            print privKeyPath, caCertPath
            with open(privKeyPath) as f:
                privateKey = f.read()
            with open(caCertPath) as f:
                certChain = f.read()
            credentials = grpc.ssl_server_credentials(((
                privateKey,
                certChain,
            ), ))
            self.grpcServer.add_secure_port(targetUrl, credentials)
        else:
            # insecure connection
            self.grpcServer.add_insecure_port(targetUrl)