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