Пример #1
0
async def fixture_rest_async(
    request,
    tmp_path: Path,
    kafka_servers: KafkaServers,
    registry_async_client: Client,
) -> AsyncIterator[Optional[KafkaRest]]:

    # Do not start a REST api when the user provided an external service. Doing
    # so would cause this node to join the existing group and participate in
    # the election process. Without proper configuration for the listeners that
    # won't work and will cause test failures.
    rest_url = request.config.getoption("rest_url")
    if rest_url:
        yield None
        return

    config_path = tmp_path / "karapace_config.json"

    config = set_config_defaults({
        "bootstrap_uri": kafka_servers.bootstrap_servers,
        "admin_metadata_max_age": 0
    })
    write_config(config_path, config)
    rest = KafkaRest(config_file_path=str(config_path), config=config)

    assert rest.serializer.registry_client
    assert rest.consumer_manager.deserializer.registry_client
    rest.serializer.registry_client.client = registry_async_client
    rest.consumer_manager.deserializer.registry_client.client = registry_async_client
    try:
        yield rest
    finally:
        rest.close()
        await rest.close_producers()
Пример #2
0
 def close(self):
     for cls in [KarapaceBase, KafkaRest, KarapaceSchemaRegistry]:
         try:
             cls.close(self)
         except:  # pylint: disable=bare-except
             pass
         KafkaRest.close_producers(self)
Пример #3
0
async def fixture_rest_async(
    session_tmpdir: TempDirCreator, kafka_server: Optional[KafkaConfig], registry_async_client: Client
) -> AsyncIterator[KafkaRest]:
    if not kafka_server:
        assert REST_URI in os.environ
        instance, _ = mock_factory("rest")()
        yield instance
    else:
        config_path = os.path.join(session_tmpdir(), "karapace_config.json")
        kafka_port = kafka_server.kafka_port

        config = set_config_defaults({
            "log_level": "WARNING",
            "bootstrap_uri": f"127.0.0.1:{kafka_port}",
            "admin_metadata_max_age": 0
        })
        write_config(config_path, config)
        rest = KafkaRest(config_file_path=config_path, config=config)

        assert rest.serializer.registry_client
        assert rest.consumer_manager.deserializer.registry_client
        rest.serializer.registry_client.client = registry_async_client
        rest.consumer_manager.deserializer.registry_client.client = registry_async_client
        try:
            yield rest
        finally:
            rest.close()
            await rest.close_producers()
Пример #4
0
def main() -> int:
    parser = argparse.ArgumentParser(
        prog="karapace rest",
        description="Karapace: Your Kafka essentials in one tool")
    parser.add_argument("--version",
                        action="version",
                        help="show program version",
                        version=karapace_version.__version__)
    parser.add_argument("config_file",
                        help="configuration file path",
                        type=argparse.FileType())
    arg = parser.parse_args()

    with closing(arg.config_file):
        config = read_config(arg.config_file)

    kc = KafkaRest(config_file_path=arg.config_file.name, config=config)
    try:
        kc.run(host=kc.config["host"], port=kc.config["port"])
    except Exception:  # pylint: disable-broad-except
        if kc.raven_client:
            kc.raven_client.captureException(tags={"where": "karapace_rest"})
        raise

    return 0
Пример #5
0
 def __init__(self, config):
     KarapaceBase.__init__(self, config)
     KafkaRest._init(self, config)
     KafkaRest._add_routes(self)
     KarapaceSchemaRegistry._init(self)
     KarapaceSchemaRegistry._add_routes(self)
     self.log = logging.getLogger("KarapaceAll")
     self.app.on_shutdown.append(self.close_by_app)
Пример #6
0
 def __init__(self, config_file_path: str, config: dict) -> None:
     KarapaceBase.__init__(self,
                           config_file_path=config_file_path,
                           config=config)
     KafkaRest._init(self, config=config)
     KafkaRest._add_routes(self)
     KarapaceSchemaRegistry._init(self, config=config)
     KarapaceSchemaRegistry._add_routes(self)
     self.log = logging.getLogger("KarapaceAll")
     self.app.on_shutdown.append(self.close_by_app)
Пример #7
0
def main():
    parser = argparse.ArgumentParser(prog="karapace rest", description="Karapace: Your Kafka essentials in one tool")
    parser.add_argument("--version", action="version", help="show program version", version=karapace_version.__version__)
    parser.add_argument("config_file", help="configuration file path")
    arg = parser.parse_args()

    if not os.path.exists(arg.config_file):
        print("Config file: {} does not exist, exiting".format(arg.config_file))
        return 1

    kc = KafkaRest(arg.config_file)
    try:
        return kc.run(host=kc.config["host"], port=kc.config["port"])
    except Exception:  # pylint: disable-broad-except
        if kc.raven_client:
            kc.raven_client.captureException(tags={"where": "karapace_rest"})
        raise
Пример #8
0
def main() -> int:
    parser = argparse.ArgumentParser(prog="karapace rest", description="Karapace: Your Kafka essentials in one tool")
    parser.add_argument("--version", action="version", help="show program version", version=karapace_version.__version__)
    parser.add_argument("config_file", help="configuration file path", type=argparse.FileType())
    arg = parser.parse_args()

    with closing(arg.config_file):
        config = read_config(arg.config_file)

    logging.basicConfig(level=logging.INFO, format=DEFAULT_LOG_FORMAT_JOURNAL)
    logging.getLogger().setLevel(config["log_level"])
    kc = KafkaRest(config=config)
    try:
        kc.run(host=kc.config["host"], port=kc.config["port"])
    except Exception:  # pylint: disable-broad-except
        if kc.raven_client:
            kc.raven_client.captureException(tags={"where": "karapace_rest"})
        raise

    return 0
Пример #9
0
async def fixture_rest_async(session_tmpdir, kafka_server,
                             registry_async_client):
    if REST_URI in os.environ:
        instance, _ = mock_factory("rest")()
        yield instance
    else:
        config_path = os.path.join(session_tmpdir(), "karapace_config.json")
        kafka_port = kafka_server["kafka_port"]
        write_config(
            config_path, {
                "log_level": "WARNING",
                "bootstrap_uri": f"127.0.0.1:{kafka_port}",
                "admin_metadata_max_age": 0
            })
        rest = KafkaRest(config_path)
        rest.serializer.registry_client.client = registry_async_client
        rest.consumer_manager.deserializer.registry_client.client = registry_async_client
        try:
            yield rest
        finally:
            rest.close()
            await rest.close_producers()
Пример #10
0
def main() -> int:
    parser = argparse.ArgumentParser(
        prog="karapace",
        description="Karapace: Your Kafka essentials in one tool")
    parser.add_argument("--version",
                        action="version",
                        help="show program version",
                        version=karapace_version.__version__)
    parser.add_argument("config_file",
                        help="configuration file path",
                        type=argparse.FileType())
    arg = parser.parse_args()

    with closing(arg.config_file):
        config = read_config(arg.config_file)

    config_file_path = arg.config_file.name

    logging.getLogger().setLevel(config["log_level"])

    kc: RestApp
    if config["karapace_rest"] and config["karapace_registry"]:
        info_str = "both services"
        kc = KarapaceAll(config_file_path=config_file_path, config=config)
    elif config["karapace_rest"]:
        info_str = "karapace rest"
        kc = KafkaRest(config_file_path=config_file_path, config=config)
    elif config["karapace_registry"]:
        info_str = "karapace schema registry"
        kc = KarapaceSchemaRegistry(config_file_path=config_file_path,
                                    config=config)
    else:
        print("Both rest and registry options are disabled, exiting")
        return 1

    print("=" * 100 + f"\nStarting {info_str}\n" + "=" * 100)

    try:
        kc.run(host=kc.config["host"], port=kc.config["port"])
    except Exception:  # pylint: disable-broad-except
        if kc.raven_client:
            kc.raven_client.captureException(tags={"where": "karapace"})
        raise
    return 0
Пример #11
0
def main():
    parser = argparse.ArgumentParser(
        prog="karapace",
        description="Karapace: Your Kafka essentials in one tool")
    parser.add_argument("--version",
                        action="version",
                        help="show program version",
                        version=karapace_version.__version__)
    parser.add_argument("config_file", help="configuration file path")
    arg = parser.parse_args()

    if not os.path.exists(arg.config_file):
        print("Config file: {} does not exist, exiting".format(
            arg.config_file))
        return 1
    config = None
    kc = None
    info_str = ""
    try:
        config = read_config(arg.config_file)
    except InvalidConfiguration:
        print("Config file: {} is invalid, exiting".format(arg.config_file))
        return 1
    if config["karapace_rest"] and config["karapace_registry"]:
        info_str = "both services"
        kc = KarapaceAll(arg.config_file)
    elif config["karapace_rest"]:
        info_str = "karapace rest"
        kc = KafkaRest(arg.config_file)
    elif config["karapace_registry"]:
        info_str = "karapace schema registry"
        kc = KarapaceSchemaRegistry(arg.config_file)
    else:
        print("Both rest and registry options are disabled, exiting")
        return 1
    print("=" * 100 + f"\nStarting {info_str}\n" + "=" * 100)
    try:
        return kc.run(host=kc.config["host"], port=kc.config["port"])
    except Exception:  # pylint: disable-broad-except
        if kc.raven_client:
            kc.raven_client.captureException(tags={"where": "karapace"})
        raise