Пример #1
0
 def serve(self):
     # We need to build a health service to work with go-plugin
     health = HealthServicer()
     health.set(
         "command",
         health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))
     # Start the server.
     server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
     plugin_interface_pb2_grpc.add_PluginInterfaceServicer_to_server(
         _PluginServicer(server, self), server)
     health_pb2_grpc.add_HealthServicer_to_server(health, server)
     print("Address: " + self.listener_addr, flush=True)
     address = self.listener_addr.split("://")
     if len(address) != 2:
         raise RuntimeError('Address format nor correct')
     if address[0] == "unix":
         realAddr = "unix:" + address[1]
     elif address[0] == "tcp" or address[0] == "tcp4" or address[
             0] == "tcp6":
         realAddr = address[1]
     else:
         raise RuntimeError('Address not support')
     server.add_insecure_port(realAddr)
     server.start()
     print("Started", flush=True)
     dataOut = os.fdopen(3, "w")
     print("%s: %s" % (MSG_COREVERSION, "1.0.0"), file=dataOut, flush=True)
     print("%s: %s" % (MSG_VERSION, self.request_version),
           file=dataOut,
           flush=True)
     print("%s: %s" % (MSG_ADDRESS, self.listener_addr),
           file=dataOut,
           flush=True)
     print("%s: %s" % (MSG_RPC_PROTOCOL, "1"), file=dataOut, flush=True)
     print("%s: %s" % (MSG_DONE, ""), file=dataOut, flush=True)
Пример #2
0
def serve():
    # We need to build a health service to work with go-plugin
    health = HealthServicer()
    health.set(
        "plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    # Start the server.
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    health_pb2_grpc.add_HealthServicer_to_server(health, server)

    cli_plugin_pb2_grpc.add_CLIServicer_to_server(mine(), server)

    SERVICE_NAMES = (
        cli_plugin_pb2.DESCRIPTOR.services_by_name['CLI'].full_name,
        reflection.SERVICE_NAME,
    )
    reflection.enable_server_reflection(SERVICE_NAMES, server)

    server.add_insecure_port(f'{plugin_host}:{plugin_port}')

    server.start()

    sys.stdout.write(
        f"{go_plugin_version}|{app_protocol_version}|tcp|{plugin_host}:{plugin_port}|grpc\n"
    )

    sys.stdout.flush()

    try:
        server.wait_for_termination()
    except KeyboardInterrupt:
        server.stop(0)
Пример #3
0
    def serve(self):

        # We need to build a health service to work with go-plugin
        logging.basicConfig(level=logging.DEBUG)

        if self.isRunning():
            print("1|1|tcp|127.0.0.1:1234|grpc")  #DO NOT REMOTE THIS LINE
            sys.stdout.flush()  #DO NOT REMOTE THIS LINE
            logging.info("Already running / Not starting new server")
            return

        health = HealthServicer()
        health.set(
            "plugin",
            health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

        # Start the server.
        server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

        self.server = server
        self.e = threading.Event()

        #print (inspect.getsourcefile(server.start))

        pyvcloudprovider_pb2_grpc.add_PyVcloudProviderServicer_to_server(
            PyVcloudProviderServicer(self), server)

        disk_pb2_grpc.add_IndependentDiskServicer_to_server(
            IndependentDiskServicer(self), server)

        org_pb2_grpc.add_OrgServicer_to_server(OrgServicer(self), server)

        user_pb2_grpc.add_UserServicer_to_server(UserServicer(self), server)

        vdc_pb2_grpc.add_VdcServicer_to_server(VdcServicer(self), server)

        vapp_vm_pb2_grpc.add_VappVmServicer_to_server(VappVmServicer(self),
                                                      server)

        health_pb2_grpc.add_HealthServicer_to_server(health, server)
        server.add_insecure_port('127.0.0.1:1234')
        ## check before start if server is running at 1234
        ## if server running connect to the server and execute stop
        server.start()

        # Output information
        print("1|1|tcp|127.0.0.1:1234|grpc")
        sys.stdout.flush()

        self.e.wait(
        )  # This event if expected from the pluger_stop functionality
        self.server.stop(0)
    def serve(self):

        # We need to build a health service to work with go-plugin
        logging.basicConfig(level=logging.DEBUG)

        if self.isRunning():
            print("1|1|tcp|127.0.0.1:1234|grpc")  #DO NOT REMOTE THIS LINE
            sys.stdout.flush()  #DO NOT REMOTE THIS LINE
            logging.info("Already running / Not starting new server")
            return

        health = HealthServicer()
        health.set(
            "plugin",
            health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

        # Start the server.
        server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

        self.server = server
        self.e = threading.Event()

        #print (inspect.getsourcefile(server.start))

        pyvcloudprovider_pb2_grpc.add_PyVcloudProviderServicer_to_server(
            PyVcloudProviderServicer(self), server)

        disk_pb2_grpc.add_IndependentDiskServicer_to_server(
            IndependentDiskServicer(self), server)

        org_pb2_grpc.add_OrgServicer_to_server(OrgServicer(self), server)

        user_pb2_grpc.add_UserServicer_to_server(UserServicer(self), server)

        vdc_pb2_grpc.add_VdcServicer_to_server(VdcServicer(self), server)

        vapp_vm_pb2_grpc.add_VappVmServicer_to_server(
            VappVmServicer(self), server)

        health_pb2_grpc.add_HealthServicer_to_server(health, server)
        server.add_insecure_port('127.0.0.1:1234')
        ## check before start if server is running at 1234
        ## if server running connect to the server and execute stop
        server.start()

        # Output information
        print("1|1|tcp|127.0.0.1:1234|grpc")
        sys.stdout.flush()

        self.e.wait(
        )  # This event if expected from the pluger_stop functionality
        self.server.stop(0)
Пример #5
0
def main():
    config = configs.LocalDockerConfig()

    server = factory.create_server(config)
    manager = factory.create_zookeeper_cluster_manager(config)
    manager.startup()

    servicers_pb2_grpc.add_MergerServicer_to_server(MergerServicer(config, manager), server)

    health = HealthServicer()
    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    health.set("Merger", health_pb2.HealthCheckResponse.SERVING)

    factory.serve(server)
Пример #6
0
def serve():
    logger = get_logger()
    logger.info("FastText server starting ...")

    # Read gRPC options
    config = get_config()
    grpc_port = config["grpc"].get("port", 50051)
    grpc_max_workers = config["grpc"].get("max_workers", 2)
    grpc_maximum_concurrent_rpcs = config["grpc"].get(
        "maximum_concurrent_rpcs", 25)
    logger.info("Concurrent workers: {}".format(grpc_max_workers))
    logger.info("gRPC queue size: {}".format(grpc_maximum_concurrent_rpcs))

    # Read gRPC channel options
    grpc_options = []
    for option in config["grpc"].get("channel_options", {}).items():
        logger.info("gRPC channel option: {}".format(option))
        grpc_options.append(option)

    # Create server
    server = grpc.server(
        futures.ThreadPoolExecutor(max_workers=grpc_max_workers),
        maximum_concurrent_rpcs=grpc_maximum_concurrent_rpcs,
        options=grpc_options,
    )

    # Add servicers
    servicer = FastTextServicer()
    service_pb2_grpc.add_FastTextServicer_to_server(servicer, server)
    health_servicer = HealthServicer()
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)

    # Run server
    address = "[::]:{}".format(grpc_port)
    server.add_insecure_port(address)
    server.start()
    logger.info("Listening incoming connections at {}".format(address))

    # Mark the server as running using gRPC health check protocol
    serving_status = health_pb2._HEALTHCHECKRESPONSE_SERVINGSTATUS
    status_code = serving_status.values_by_name["SERVING"].number
    health_servicer.set("", status_code)
    logger.info("gRPC health check protocol: {}".format(status_code))

    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)
        servicer = None
Пример #7
0
def serve() -> None:
    """Set up the grpc server and start serving."""
    parser = argparse.ArgumentParser()
    parser.add_argument("--config",
                        type=str,
                        nargs="?",
                        help="Location of config file")

    args = parser.parse_args()
    config = None
    if args.config and args.config != "":
        config = Config.from_yaml_file(args.config)
    else:
        config = Config()
    server = grpc.server(ThreadPoolExecutor(max_workers=10))
    server.add_insecure_port(f"127.0.0.1:{config.grpc_port}")

    mongo = MongoWrapper.from_uri(config.mongo_url)
    mongo.initialize_db()

    # Add all the grpc services to the server.
    span_writer = SpanWriterService(mongo)
    add_SpanWriterPluginServicer_to_server(span_writer, server)
    span_reader = SpanReaderService(mongo)
    add_SpanReaderPluginServicer_to_server(span_reader, server)
    dependency_reader = DependencyReaderService(mongo)
    add_DependenciesReaderPluginServicer_to_server(dependency_reader, server)

    # We need to add a health service to work with the go-plugin.
    # https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md#3-add-the-grpc-health-checking-service
    health = HealthServicer()
    health.set("plugin",
               health_pb2.HealthCheckResponse.ServingStatus.Value("SERVING"))
    health_pb2_grpc.add_HealthServicer_to_server(health, server)

    server.start()

    # # We need to output the handshake information so that the go plugin knows where to find us.
    # # https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md#4-output-handshake-information
    print(f"1|1|tcp|127.0.0.1:{config.grpc_port}|grpc")

    stdout.flush()

    server.wait_for_termination()
Пример #8
0
def serve():
    # We need to build a health service to work with go-plugin
    health = HealthServicer()
    health.set("plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    # Start the server.
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    kv_pb2_grpc.add_KVServicer_to_server(KVServicer(), server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    server.add_insecure_port('127.0.0.1:1234')
    server.start()

    # Output information
    print("1|1|tcp|127.0.0.1:1234|grpc")
    sys.stdout.flush()

    try:
        while True:
            time.sleep(60 * 60 * 24)
    except KeyboardInterrupt:
        server.stop(0)
Пример #9
0
def serve():
    # We need to build a health service to work with go-plugin
    health = HealthServicer()
    health.set("plugin",
               health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    # Start the server.
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    kv_pb2_grpc.add_KVServicer_to_server(KVServicer(), server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    server.add_insecure_port(':1234')
    server.start()

    # Output information
    print("1|1|tcp|127.0.0.1:1234|grpc")
    sys.stdout.flush()

    try:
        while True:
            time.sleep(60 * 60 * 24)
    except KeyboardInterrupt:
        server.stop(0)
Пример #10
0
def main():
    health = HealthServicer()
    health.set("plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    plugin = PluginServicer()

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    dashboard_pb2_grpc.add_PluginServicer_to_server(plugin, server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)

    port = get_open_port()

    server.add_insecure_port('[::]:' + port)
    server.start()

    print("1|1|tcp|127.0.0.1:"+port+"|grpc")
    sys.stdout.flush()

    try:
        server.wait_for_termination()
    except KeyboardInterrupt:
        pass
Пример #11
0
def serve(client_id, host='127.0.0.1', port='1234'):

    # create client registry server
    reg_server = RegistryServer()
    reg_server.start()

    reg_server.wait_complete()

    # create client registry
    registry = RegistryServer.GET_CLIENT_REGISTRY()

    # create a call counter for call to API
    call_counter = CallCounter()

    # only import command executor for server startup
    from apicmd import NetAppCommandExecutor
    # create new NetApp Command Executor
    executor = NetAppCommandExecutor()
    # create the servicer
    servicer = NetAppApiServicer(registry, call_counter, executor)

    # generate gRPC connection message and store in registry
    grpc_msg = "1|1|tcp|" + host + ":" + port + "|grpc"
    if not registry.set_grpc_msg(grpc_msg):
        LOGGER.error('could not set gRPC message in registry')

        registry.shutdown()

        reg_server.wait_complete()

        reg_server.terminate()
        reg_server.join()
        sys.exit(1)

    # register client
    if not register_client(registry, client_id):
        LOGGER.error('register server start client in registry')

        registry.shutdown()

        reg_server.wait_complete()

        reg_server.terminate()
        reg_server.join()
        sys.exit(1)

    # We need to build a health service to work with go-plugin
    health = HealthServicer()
    health.set("plugin",
               health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    # Start the server.
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    grpcapi_pb2_grpc.add_GRPCNetAppApiServicer_to_server(servicer, server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    server.add_insecure_port(host + ':' + port)
    server.start()

    LOGGER.debug('started GRPC server @ %s:%s', host, port)

    # let GRPC know we are here and good...
    notify_grpc(client_id, registry)

    # set registry status to running
    if not registry.set_api_status(ApiStatus.running):
        LOGGER.error('could not set api status in registry')

        server.stop(0)
        registry.shutdown()

        reg_server.wait_complete()

        reg_server.terminate()
        reg_server.join()
        sys.exit(1)

    # create running status file
    # source: https://stackoverflow.com/a/12654798
    with open(RUNNING_FILE, 'a'):
        os.utime(RUNNING_FILE, None)

    LOGGER.debug('running file created')

    try:
        # check if counter thinks we should continue
        # either by init or calls received/pending
        while call_counter.stay_alive():
            # get the number of calls received + reset
            call_cnt = call_counter.get_call_cnt()
            LOGGER.debug("active calls, was needed %d times", call_cnt)

            # wait for CHECK_TIMEOUT to receive calls
            time.sleep(CHECK_TIMEOUT)

    except KeyboardInterrupt:
        pass

    # create stopping status file
    with open(STOPPING_FILE, 'a'):
        os.utime(STOPPING_FILE, None)

    LOGGER.debug("left while loop, issuing server.stop()")
    unregister_client(registry, client_id)
    server.stop(0)
    os.remove(RUNNING_FILE)  # making sure we are not running
    registry.shutdown()

    # sleep until server indicates shutdown complete
    reg_server.wait_complete()

    reg_server.terminate()
    reg_server.join()

    os.remove(STOPPING_FILE)  # making sure we also declare stopped

    LOGGER.debug("exiting netapp API serve()")
Пример #12
0
    def serve(self, wait=False):
        """
        Start serving the plugin grpc services, and return control
        to the called. If ``wait=True``, block until the server is stopped.

        If ``False`` is returned, caller can check `.error_msg` to read the
        last error message.

        Args:
            wait (bool): Block until server stops

        Returns:
            bool: Return True on successful start
        """
        self.stop()
        self._error = ''

        if not self.check_magic_key():
            return False

        self._server = server = self.server()

        # We need to build a health service to work with go-plugin
        health = HealthServicer()
        health.set(
            self.GRPC_SERVICE_NAME,
            health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))
        health_pb2_grpc.add_HealthServicer_to_server(health, server)

        # enable controller
        _controller_pb2_grpc.add_GRPCControllerServicer_to_server(
            ServerController(server), server)

        # instrument the server to capture the registration of the plugin
        # services, so that we can automatically add them for reflection
        _add_generic_rpc_handlers = server.add_generic_rpc_handlers
        plugin_service_names = set()

        def add_generic_rpc_handlers(self, handlers):
            plugin_service_names.update({h.service_name() for h in handlers})
            return _add_generic_rpc_handlers(handlers)

        server.add_generic_rpc_handlers = add_generic_rpc_handlers.__get__(
            server, server.__class__)

        # Register all plugins
        plugins = self._cfg.plugins
        for name in plugins:
            plugin = plugins[name]
            plugin.server_register(server)

        # reset the handler and set up reflection
        server.add_generic_rpc_handlers = _add_generic_rpc_handlers
        if plugin_service_names:
            names = list(plugin_service_names)
            logging.info(
                "plugin server installing grpc reflection for plugins: %s",
                names)
            names.append(reflection.SERVICE_NAME)
            reflection.enable_server_reflection(names, server)

        # configure server endpoint
        if os.name == 'posix':
            fd, sock_path = tempfile.mkstemp(suffix=".sock", prefix="plugin_")
            os.close(fd)
            os.unlink(sock_path)
            endpoint = os.path.abspath(sock_path)
            server.add_insecure_port("unix:" + endpoint)
            network = 'unix'
        else:
            port = 0
            port_opts = {}
            try:
                port_opts['min_port'] = int(
                    os.environ.get('PLUGIN_MIN_PORT', ''))
            except ValueError:
                pass
            try:
                port_opts['max_port'] = int(
                    os.environ.get('PLUGIN_MAX_PORT', ''))
            except ValueError:
                pass
            if port_opts:
                port = pygo_plugin.utils.find_free_port(**port_opts)
            port = server.add_insecure_port('127.0.0.1:{}'.format(port))
            network = 'tcp'
            endpoint = '127.0.0.1:%d' % port

        server.start()

        # Output information
        handshake = "{proto_ver}|{app_proto_ver}|{network}|{endpoint}|{protocol}".format(
            proto_ver=_GO_PLUGIN_PROTOCOL_VER,
            app_proto_ver=self._cfg.handshake_config.protocol_version,
            network=network,
            endpoint=endpoint,
            protocol='grpc',
        )
        # logging.info(handshake)
        print(handshake)
        sys.stdout.flush()

        if wait:
            server.wait_for_termination()

        return True
Пример #13
0
def serve(jobs):
    # Cache the jobs list for later processing.
	# We first have to translate given jobs to different structure.
    for job in jobs:
        # Create proto object
        p = plugin_pb2.Job()

        # Manual interaction
        if job.interaction != None:
            p.interaction.description = job.interaction.description
            p.interaction.type = job.interaction.inputType
            p.interaction.value = job.interaction.value

        # Arguments
        args = []
        if job.args:
            for arg in job.args:
                a = plugin_pb2.Argument()
                a.description = arg.description
                a.type = arg.inputType.value
                a.key = arg.key
                a.value = arg.value

                args.append(a)

        # Set the rest of the fields
        jobTitle = bytes(job.title) if six.PY2 else bytes(job.title, 'utf8')
        p.unique_id = fnv1a_32(jobTitle)
        p.title = job.title
        p.description = job.description
        p.args.extend(args)

        # Resolve dependencies
        if job.dependsOn:
            for depJob in job.dependsOn:
                for currJob in jobs:
                    if depJob.lower() == currJob.title.lower():
                        title = bytes(currJob.title) if six.PY2 else bytes(currJob.title, 'utf8')
                        p.dependson.append(fnv1a_32(title))
                        foundDep = True
                        break

                if not foundDep:
                    raise Exception("job '" + job.title + "' has dependency '" + depJob + "' which is not declared")

        # job wrapper object for this job
        w = JobWrapper(job.handler, p)
        cachedJobs.append(w)

    # Check if two jobs have the same title which is restricted
    for x, job in enumerate(cachedJobs):
        for y, innerJob in enumerate(cachedJobs):
            if x != y and job.job.unique_id == innerJob.job.unique_id:
                raise Exception("duplicate job found (two jobs with same title)")

    # get certificate path from environment variables
    certPath = os.environ['GAIA_PLUGIN_CERT']
    keyPath = os.environ['GAIA_PLUGIN_KEY']
    caCertPath = os.environ['GAIA_PLUGIN_CA_CERT']

    # check if all certs are available
    if not os.path.isfile(certPath):
        raise Exception("cannot find path to certificate")
    if not os.path.isfile(keyPath):
        raise Exception("cannot find path to key")
    if not os.path.isfile(caCertPath):
        raise Exception("cannot find path to root certificate")

    # Open files
    private_key = open(keyPath).read()
    certificate_chain = open(certPath).read()
    root_cert = open(caCertPath).read()

    if six.PY3:
        private_key = bytes(private_key, 'utf8')
        certificate_chain = bytes(certificate_chain, 'utf8')
        root_cert = bytes(root_cert, 'utf8')

    # We need to build a health service to work with go-plugin
    health = HealthServicer()
    health.set("plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))

    # Start the server.
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=20))
    private_key_certificate_chain_pairs = ( (private_key, certificate_chain), )
    server_credentials = grpc.ssl_server_credentials(private_key_certificate_chain_pairs, root_cert, True)
    plugin_pb2_grpc.add_PluginServicer_to_server(GRPCServer(), server)
    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    port = server.add_secure_port('127.0.0.1:0', server_credentials)
    server.start()

    # Output information
    print("1|2|tcp|127.0.0.1:" + str(port) + "|grpc")
    sys.stdout.flush()

    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)
Пример #14
0
def serve(
    handshake,
    proto_versions,
    tls_credentials=None,
    signal_handlers=True,
):
    """
    Start up a plugin server for the given protocol versions and announce it
    on stdout using the rpcplugin handshake.

    This function blocks until the client asks the server to exit.
    """

    if handshake == None or handshake.cookie_key == None or handshake.cookie_value == None:
        raise ValueError(
            "handshake must be set and must have cookie key and value", )

    if proto_versions == None or len(proto_versions) == 0:
        raise ValueError(
            "proto_versions must be a map with at least one protocol version defined"
        )

    got_cookie_value = os.environ.get(handshake.cookie_key, "")
    if got_cookie_value != handshake.cookie_value:
        raise Exception("calling program is not an rpcplugin host", )

    try:
        (v, impl) = _negotiate_protocol_version(proto_versions)
    except TypeError:
        raise Exception(
            "cannot support any protocol versions offered by the plugin client"
        )

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    # Plugin healthcheck service is mandatory for rpcplugin servers
    health = HealthServicer()
    health.set(
        "plugin",
        health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'),
    )

    health_pb2_grpc.add_HealthServicer_to_server(health, server)
    impl(server)

    auto_cert_str = ""
    if tls_credentials == None:
        (creds, cert_str) = _auto_tls()
        tls_credentials = creds
        auto_cert_str = cert_str

    transports = os.environ.get("PLUGIN_TRANSPORTS", "tcp").split(",")
    port = None
    for transport in transports:
        if transport == "tcp":
            port = server.add_secure_port('0.0.0.0:0', tls_credentials)
            break
    if port is None:
        raise Exception(
            "cannot support any transports offered by the plugin client")

    if signal_handlers:
        # Ignore interrupt signals, because they're probably being sent to
        # the whole process group and we want the host program to handle
        # them, and decide for itself when it's time for this plugin server
        # to exit.
        signal.signal(signal.SIGINT, signal.SIG_IGN)

    server.start()

    # This handshake line tells the client where to connect and what protocol
    # to talk when it does.
    sys.stdout.write("1|%d|tcp|127.0.0.1:%d|grpc|%s\n" %
                     (v, port, str(auto_cert_str)))
    sys.stdout.flush()

    while True:
        time.sleep(60 * 60 * 24)