|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import logging |
| 6 | +from pprint import pprint |
| 7 | + |
| 8 | +from kafka.admin.client import KafkaAdminClient |
| 9 | +from .cluster import ClusterSubCommand |
| 10 | +from .configs import ConfigsSubCommand |
| 11 | +from .consumer_groups import ConsumerGroupsSubCommand |
| 12 | +from .log_dirs import LogDirsSubCommand |
| 13 | +from .topics import TopicsSubCommand |
| 14 | + |
| 15 | +def main_parser(): |
| 16 | + parser = argparse.ArgumentParser( |
| 17 | + prog='python -m kafka.admin', |
| 18 | + description='Kafka admin client', |
| 19 | + ) |
| 20 | + parser.add_argument( |
| 21 | + '-b', '--bootstrap-servers', type=str, action='append', required=True, |
| 22 | + help='host:port for cluster bootstrap servers') |
| 23 | + parser.add_argument( |
| 24 | + '-c', '--extra-config', type=str, action='append', |
| 25 | + help='additional configuration properties for admin client') |
| 26 | + parser.add_argument( |
| 27 | + '-l', '--log-level', type=str, |
| 28 | + help='logging level, passed to logging.basicConfig') |
| 29 | + parser.add_argument( |
| 30 | + '-f', '--format', type=str, default='raw', |
| 31 | + help='output format: raw|json') |
| 32 | + return parser |
| 33 | + |
| 34 | + |
| 35 | +_LOGGING_LEVELS = {'NOTSET': 0, 'DEBUG': 10, 'INFO': 20, 'WARNING': 30, 'ERROR': 40, 'CRITICAL': 50} |
| 36 | + |
| 37 | + |
| 38 | +def build_kwargs(props): |
| 39 | + kwargs = {} |
| 40 | + for prop in props or []: |
| 41 | + k, v = prop.split('=') |
| 42 | + try: |
| 43 | + v = int(v) |
| 44 | + except ValueError: |
| 45 | + pass |
| 46 | + if v == 'None': |
| 47 | + v = None |
| 48 | + elif v == 'False': |
| 49 | + v = False |
| 50 | + elif v == 'True': |
| 51 | + v = True |
| 52 | + kwargs[k] = v |
| 53 | + return kwargs |
| 54 | + |
| 55 | + |
| 56 | +def run_cli(args=None): |
| 57 | + parser = main_parser() |
| 58 | + subparsers = parser.add_subparsers(help='subcommands') |
| 59 | + for cmd in [ClusterSubCommand, ConfigsSubCommand, LogDirsSubCommand, |
| 60 | + TopicsSubCommand, ConsumerGroupsSubCommand]: |
| 61 | + cmd.add_subparser(subparsers) |
| 62 | + |
| 63 | + config = parser.parse_args(args) |
| 64 | + if config.log_level: |
| 65 | + logging.basicConfig(level=_LOGGING_LEVELS[config.log_level.upper()]) |
| 66 | + if config.format not in ('raw', 'json'): |
| 67 | + raise ValueError('Unrecognized format: %s' % config.format) |
| 68 | + logger = logging.getLogger(__name__) |
| 69 | + |
| 70 | + kwargs = build_kwargs(config.extra_config) |
| 71 | + client = KafkaAdminClient(bootstrap_servers=config.bootstrap_servers, **kwargs) |
| 72 | + try: |
| 73 | + result = config.command(client, config) |
| 74 | + if config.format == 'raw': |
| 75 | + pprint(result) |
| 76 | + elif config.format == 'json': |
| 77 | + print(json.dumps(result)) |
| 78 | + return 0 |
| 79 | + except AttributeError: |
| 80 | + parser.print_help() |
| 81 | + return 2 |
| 82 | + except Exception: |
| 83 | + logger.exception('Error!') |
| 84 | + return 1 |
| 85 | + |
| 86 | +if __name__ == '__main__': |
| 87 | + import sys |
| 88 | + sys.exit(run_cli()) |
| 89 | + |
| 90 | + |
| 91 | +# Commands TODO: |
| 92 | + # [acls] |
| 93 | + # describe |
| 94 | + # create |
| 95 | + # delete |
| 96 | + |
| 97 | + # [configs] |
| 98 | + # alter |
| 99 | + # IncrementalAlterConfigs (not supported yet) |
| 100 | + |
| 101 | + # [partitions] |
| 102 | + # create |
| 103 | + # alter-reassignments (AlterPartitionReassignments - not supported yet) |
| 104 | + # list-reassignments (ListPartitionReassignments - not supported yet) |
| 105 | + |
| 106 | + # [records] |
| 107 | + # delete |
| 108 | + |
| 109 | + # [consumer-groups] |
| 110 | + # remove-members (not supported yet) |
| 111 | + # delete-offsets (not supported yet) |
| 112 | + # alter-offsets (not supported yet) |
| 113 | + |
| 114 | + # [offsets] |
| 115 | + # list (not supported yet) |
| 116 | + # delete (OffsetDelete - not supported yet) |
| 117 | + |
| 118 | + # leader-election |
| 119 | + # perform_leader_election |
| 120 | + |
| 121 | + # [log-dirs] |
| 122 | + # describe (currently broken) |
| 123 | + # alter (AlterReplicaLogDirs - not supported yet) |
| 124 | + |
| 125 | + # [client-quotas] |
| 126 | + # describe (DescribeClientQuotas - not supported yet) |
| 127 | + # alter (AlterClientQuotas - not supported yet) |
| 128 | + |
| 129 | + # DescribeQuorum (not supported yet) |
| 130 | + |
| 131 | + # [producers] |
| 132 | + # describe (DescribeProducers - not supported yet) |
| 133 | + |
| 134 | + # [transactions] |
| 135 | + # describe (DescribeTransactions - not supported yet) |
| 136 | + # list (ListTransactions - not supported yet) |
| 137 | + # abort (not supported yet) |
| 138 | + |
| 139 | + # [topics] |
| 140 | + # describe-partitions (DescribeTopicPartitions - not supported yet) |
| 141 | + |
| 142 | + # [cluster] |
| 143 | + # describe-features (DescribeFeatures - not supported yet) |
| 144 | + # update-features (UpdateFeatures - not supported yet) |
| 145 | + # version |
| 146 | + # api-versions |
| 147 | + |
| 148 | + |
| 149 | + |
0 commit comments