-
Notifications
You must be signed in to change notification settings - Fork 0
/
zooget.py
executable file
·43 lines (34 loc) · 1.28 KB
/
zooget.py
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
#!/usr/bin/python3
import argparse
from argparse import RawDescriptionHelpFormatter
from kazoo.client import KazooClient
import kazoo.exceptions
parser = argparse.ArgumentParser(description='Zookeeper read client.\nReturns zookeeper item data in UTF-8 format to STDOUT. Returns "none" if no item exists', formatter_class=RawDescriptionHelpFormatter)
parser.add_argument('item', action="store", help='/path/to/item')
group = parser.add_mutually_exclusive_group()
group.add_argument('-l', dest="listing", action="store_true", default=False, help='List nested items on /path')
group.add_argument('-d', dest="delete", action="store_true", default=False, help='Delete items on /path recursively')
args = parser.parse_args()
zookeeper = {
'server':'127.0.0.1',
'port':'2181'
}
zk = KazooClient(hosts='%(server)s:%(port)s' % zookeeper)
zk.start()
if args.listing:
try:
answer = zk.get_children(args.item)
print(answer)
except kazoo.exceptions.NoNodeError:
print('ERROR: No such path!')
elif args.delete:
zk.delete(args.item, recursive=True)
print(args.item + ' deleted!')
else:
try:
answer = zk.get(args.item)
data = answer[0].decode('utf-8')
print(data)
except kazoo.exceptions.NoNodeError:
print('none')
zk.stop()