コード例 #1
0
def get_yatai_service(
    channel_address=None,
    access_token=None,
    db_url=None,
    repo_base_url=None,
    s3_endpoint_url=None,
    default_namespace=None,
):
    channel_address = channel_address or config('yatai_service').get('url')
    access_token = access_token or config('yatai_service').get('access_token')
    channel_address = channel_address.strip()
    if channel_address:
        from bentoml.yatai.proto.yatai_service_pb2_grpc import YataiStub

        if any([db_url, repo_base_url, s3_endpoint_url, default_namespace]):
            logger.warning(
                "Using remote YataiService at `%s`, local YataiService configs "
                "including db_url, repo_base_url, s3_endpoint_url and default_namespace"
                "will all be ignored.",
                channel_address,
            )

        logger.debug("Connecting YataiService gRPC server at: %s",
                     channel_address)
        scheme, addr = parse_grpc_url(channel_address)
        header_adder_interceptor = header_client_interceptor.header_adder_interceptor(
            'access_token', access_token)
        if scheme in ('grpcs', 'https'):
            client_cacert_path = (
                config().get('yatai_service', 'client_certificate_file')
                or certifi.where()  # default: Mozilla ca cert
            )
            with open(client_cacert_path, 'rb') as ca_cert_file:
                ca_cert = ca_cert_file.read()
            credentials = grpc.ssl_channel_credentials(ca_cert, None, None)
            channel = grpc.intercept_channel(
                grpc.secure_channel(addr, credentials),
                header_adder_interceptor)
        else:
            channel = grpc.intercept_channel(grpc.insecure_channel(addr),
                                             header_adder_interceptor)
        return YataiStub(channel)
    else:
        from bentoml.yatai.yatai_service_impl import YataiService

        logger.debug("Creating local YataiService instance")
        return YataiService(
            db_url=db_url,
            repo_base_url=repo_base_url,
            s3_endpoint_url=s3_endpoint_url,
            default_namespace=default_namespace,
        )
コード例 #2
0
ファイル: __init__.py プロジェクト: shakirck/BentoML
def get_yatai_service(channel_address=None,
                      db_url=None,
                      repo_base_url=None,
                      default_namespace=None):
    channel_address = channel_address or config().get('yatai_service', 'url')
    if channel_address:
        import grpc
        from bentoml.proto.yatai_service_pb2_grpc import YataiStub

        if db_url is not None:
            logger.warning(
                "Config 'db_url' is ignored in favor of remote YataiService at `%s`",
                channel_address,
            )
        if repo_base_url is not None:
            logger.warning(
                "Config 'repo_base_url:%s' is ignored in favor of remote YataiService "
                "at `%s`",
                repo_base_url,
                channel_address,
            )
        if default_namespace is not None:
            logger.warning(
                "Config 'default_namespace:%s' is ignored in favor of remote "
                "YataiService at `%s`",
                default_namespace,
                channel_address,
            )
        logger.debug("Using BentoML with remote Yatai server: %s",
                     channel_address)

        channel = grpc.insecure_channel(channel_address)
        return YataiStub(channel)
    else:
        from bentoml.yatai.yatai_service_impl import YataiService

        logger.debug("Using BentoML with local Yatai server")

        default_namespace = default_namespace or config().get(
            'deployment', 'default_namespace')
        repo_base_url = repo_base_url or config().get(
            'default_repository_base_url')
        db_url = db_url or config().get('db', 'url')

        return YataiService(
            db_url=db_url,
            repo_base_url=repo_base_url,
            default_namespace=default_namespace,
        )
コード例 #3
0
def start_yatai_service_grpc_server(db_url, repo_base_url, grpc_port, ui_port,
                                    with_ui, s3_endpoint_url):
    track_server('yatai-service-grpc-server')
    from bentoml.yatai.yatai_service_impl import YataiService

    yatai_service = YataiService(
        db_url=db_url,
        repo_base_url=repo_base_url,
        s3_endpoint_url=s3_endpoint_url,
    )
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    add_YataiServicer_to_server(yatai_service, server)
    debug_mode = config().getboolean('core', 'debug')
    if debug_mode:
        try:
            logger.debug('Enabling gRPC server reflection for debugging')
            from grpc_reflection.v1alpha import reflection
            from bentoml.proto import yatai_service_pb2

            SERVICE_NAMES = (
                yatai_service_pb2.DESCRIPTOR.services_by_name['Yatai'].
                full_name,
                reflection.SERVICE_NAME,
            )
            reflection.enable_server_reflection(SERVICE_NAMES, server)
        except ImportError:
            logger.debug(
                'Failed enabling gRPC server reflection, missing required package: '
                '"pip install grpcio-reflection"')
    server.add_insecure_port(f'[::]:{grpc_port}')
    server.start()
    if with_ui:
        web_ui_log_path = os.path.join(
            config("logging").get("BASE_LOG_DIR"),
            config('logging').get("yatai_web_server_log_filename"),
        )

        ensure_node_available_or_raise()
        yatai_grpc_server_addess = f'localhost:{grpc_port}'
        async_start_yatai_service_web_ui(yatai_grpc_server_addess, ui_port,
                                         web_ui_log_path, debug_mode)

    # We don't import _echo function from click_utils because of circular dep
    click.echo(
        f'* Starting BentoML YataiService gRPC Server\n'
        f'* Debug mode: { "on" if debug_mode else "off"}\n'
        f'* Web UI: {f"running on http://127.0.0.1:{ui_port}" if with_ui else "off"}\n'
        f'* Running on 127.0.0.1:{grpc_port} (Press CTRL+C to quit)\n'
        f'* Usage:\n'
        f'*  Set config: `bentoml config set yatai_service.url=127.0.0.1:{grpc_port}`\n'
        f'*  Set env var: `export BENTOML__YATAI_SERVICE__URL=127.0.0.1:{grpc_port}`\n'
        f'* Help and instructions: '
        f'https://docs.bentoml.org/en/latest/guides/yatai_service.html\n'
        f'{f"* Web server log can be found here: {web_ui_log_path}" if with_ui else ""}'
    )

    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        logger.info("Terminating YataiService gRPC server..")
        server.stop(grace=None)