-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Kafka commands in console lib (#775)
Add basic Kafka command structure to console library root@0abbc4d082d3:~# console kafka describe-topic-records TOPIC PARTITION RECORDS logging-traffic-topic 0 5 --------- Signed-off-by: Tanner Lewis <[email protected]>
- Loading branch information
Showing
11 changed files
with
299 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...kerSolution/src/main/docker/migrationConsole/lib/console_link/console_link/logic/kafka.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import logging | ||
from console_link.models.kafka import Kafka | ||
from console_link.models.command_result import CommandResult | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def create_topic(kafka: Kafka, topic_name: str) -> CommandResult: | ||
result = kafka.create_topic(topic_name=topic_name) | ||
return result | ||
|
||
|
||
def delete_topic(kafka: Kafka, topic_name: str) -> CommandResult: | ||
result = kafka.delete_topic(topic_name=topic_name) | ||
return result | ||
|
||
|
||
def describe_consumer_group(kafka: Kafka, group_name: str) -> CommandResult: | ||
result = kafka.describe_consumer_group(group_name=group_name) | ||
return result | ||
|
||
|
||
def describe_topic_records(kafka: Kafka, topic_name: str) -> CommandResult: | ||
result = kafka.delete_topic(topic_name=topic_name) | ||
return result |
170 changes: 170 additions & 0 deletions
170
...erSolution/src/main/docker/migrationConsole/lib/console_link/console_link/models/kafka.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import subprocess | ||
from typing import List | ||
|
||
from cerberus import Validator | ||
import logging | ||
from abc import ABC, abstractmethod | ||
from console_link.models.command_result import CommandResult | ||
from console_link.models.schema_tools import contains_one_of | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
MSK_SCHEMA = { | ||
"nullable": True, | ||
} | ||
|
||
STANDARD_SCHEMA = { | ||
"nullable": True, | ||
} | ||
|
||
SCHEMA = { | ||
'kafka': { | ||
'type': 'dict', | ||
'schema': { | ||
'broker_endpoints': {"type": "string", "required": True}, | ||
'msk': MSK_SCHEMA, | ||
'standard': STANDARD_SCHEMA | ||
}, | ||
'check_with': contains_one_of({'msk', 'standard'}) | ||
} | ||
} | ||
|
||
|
||
def get_result_for_command(command: List[str], operation_name: str) -> CommandResult: | ||
try: | ||
cmd_output = subprocess.run(command, capture_output=True, text=True, check=True) | ||
output = cmd_output.stdout | ||
message = f"{operation_name} command completed successfully" | ||
logger.info(message) | ||
if not output: | ||
output = f"Command for {operation_name} completed successfully.\n" | ||
return CommandResult(success=True, value=output) | ||
except subprocess.CalledProcessError as e: | ||
message = f"Failed to perform {operation_name} command: {str(e)} Standard Error Output: {e.stderr}" | ||
logger.info(message) | ||
output = e.stdout | ||
return CommandResult(success=False, value=output) | ||
|
||
|
||
def pretty_print_kafka_record_count(data: str) -> str: | ||
# Split the data into lines | ||
lines = data.split("\n") | ||
|
||
# Define headers | ||
headers = ["TOPIC", "PARTITION", "RECORDS"] | ||
|
||
# Initialize the formatted output with headers | ||
formatted_output = "{:<30} {:<10} {:<10}".format(*headers) + "\n" | ||
|
||
# Format each line of data | ||
for line in lines: | ||
if line and line.count(":") == 2: | ||
topic, partition, records = line.split(":") | ||
formatted_output += "{:<30} {:<10} {:<10}".format(topic, partition, records) + "\n" | ||
return formatted_output | ||
|
||
|
||
class Kafka(ABC): | ||
""" | ||
Interface for Kafka command line operations | ||
""" | ||
|
||
def __init__(self, config): | ||
logger.info(f"Initializing Kafka with config: {config}") | ||
v = Validator(SCHEMA) | ||
if not v.validate({'kafka': config}): | ||
logger.error(f"Invalid config: {v.errors}") | ||
raise ValueError(v.errors) | ||
self.brokers = config.get('broker_endpoints') | ||
|
||
@abstractmethod | ||
def delete_topic(self, topic_name='logging-traffic-topic') -> CommandResult: | ||
pass | ||
|
||
@abstractmethod | ||
def create_topic(self, topic_name='logging-traffic-topic') -> CommandResult: | ||
pass | ||
|
||
@abstractmethod | ||
def describe_consumer_group(self, group_name='logging-group-default') -> CommandResult: | ||
pass | ||
|
||
@abstractmethod | ||
def describe_topic_records(self, topic_name='logging-traffic-topic') -> CommandResult: | ||
pass | ||
|
||
|
||
class MSK(Kafka): | ||
""" | ||
AWS MSK implementation of Kafka operations | ||
""" | ||
|
||
def __init__(self, config): | ||
super().__init__(config) | ||
|
||
def delete_topic(self, topic_name='logging-traffic-topic') -> CommandResult: | ||
command = ['/root/kafka-tools/kafka/bin/kafka-topics.sh', '--bootstrap-server', f'{self.brokers}', '--delete', | ||
'--topic', f'{topic_name}', '--command-config', '/root/kafka-tools/aws/msk-iam-auth.properties'] | ||
logger.info(f"Executing command: {command}") | ||
return get_result_for_command(command, "Delete Topic") | ||
|
||
def create_topic(self, topic_name='logging-traffic-topic') -> CommandResult: | ||
command = ['/root/kafka-tools/kafka/bin/kafka-topics.sh', '--bootstrap-server', f'{self.brokers}', '--create', | ||
'--topic', f'{topic_name}', '--command-config', '/root/kafka-tools/aws/msk-iam-auth.properties'] | ||
logger.info(f"Executing command: {command}") | ||
return get_result_for_command(command, "Create Topic") | ||
|
||
def describe_consumer_group(self, group_name='logging-group-default') -> CommandResult: | ||
command = ['/root/kafka-tools/kafka/bin/kafka-consumer-groups.sh', '--bootstrap-server', f'{self.brokers}', | ||
'--timeout', '100000', '--describe', '--group', f'{group_name}', '--command-config', | ||
'/root/kafka-tools/aws/msk-iam-auth.properties'] | ||
logger.info(f"Executing command: {command}") | ||
return get_result_for_command(command, "Describe Consumer Group") | ||
|
||
def describe_topic_records(self, topic_name='logging-traffic-topic') -> CommandResult: | ||
command = ['/root/kafka-tools/kafka/bin/kafka-run-class.sh', 'kafka.tools.GetOffsetShell', '--broker-list', | ||
f'{self.brokers}', '--topic', f'{topic_name}', '--time', '-1', '--command-config', | ||
'/root/kafka-tools/aws/msk-iam-auth.properties'] | ||
logger.info(f"Executing command: {command}") | ||
result = get_result_for_command(command, "Describe Topic Records") | ||
if result.success and result.value: | ||
pretty_value = pretty_print_kafka_record_count(result.value) | ||
return CommandResult(success=result.success, value=pretty_value) | ||
return result | ||
|
||
|
||
class StandardKafka(Kafka): | ||
""" | ||
Standard Kafka distribution implementation of Kafka operations | ||
""" | ||
|
||
def __init__(self, config): | ||
super().__init__(config) | ||
|
||
def delete_topic(self, topic_name='logging-traffic-topic') -> CommandResult: | ||
command = ['/root/kafka-tools/kafka/bin/kafka-topics.sh', '--bootstrap-server', f'{self.brokers}', '--delete', | ||
'--topic', f'{topic_name}'] | ||
logger.info(f"Executing command: {command}") | ||
return get_result_for_command(command, "Delete Topic") | ||
|
||
def create_topic(self, topic_name='logging-traffic-topic') -> CommandResult: | ||
command = ['/root/kafka-tools/kafka/bin/kafka-topics.sh', '--bootstrap-server', f'{self.brokers}', '--create', | ||
'--topic', f'{topic_name}'] | ||
logger.info(f"Executing command: {command}") | ||
return get_result_for_command(command, "Create Topic") | ||
|
||
def describe_consumer_group(self, group_name='logging-group-default') -> CommandResult: | ||
command = ['/root/kafka-tools/kafka/bin/kafka-consumer-groups.sh', '--bootstrap-server', f'{self.brokers}', | ||
'--timeout', '100000', '--describe', '--group', f'{group_name}'] | ||
logger.info(f"Executing command: {command}") | ||
return get_result_for_command(command, "Describe Consumer Group") | ||
|
||
def describe_topic_records(self, topic_name='logging-traffic-topic') -> CommandResult: | ||
command = ['/root/kafka-tools/kafka/bin/kafka-run-class.sh', 'kafka.tools.GetOffsetShell', '--broker-list', | ||
f'{self.brokers}', '--topic', f'{topic_name}', '--time', '-1'] | ||
logger.info(f"Executing command: {command}") | ||
result = get_result_for_command(command, "Describe Topic Records") | ||
if result.success and result.value: | ||
pretty_value = pretty_print_kafka_record_count(result.value) | ||
return CommandResult(success=result.success, value=pretty_value) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ snapshot: | |
metadata_migration: | ||
from_snapshot: | ||
min_replicas: 0 | ||
kafka: | ||
broker_endpoints: "kafka:9092" | ||
standard: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.