-
Notifications
You must be signed in to change notification settings - Fork 2
/
zkutils.py
executable file
·53 lines (43 loc) · 1.05 KB
/
zkutils.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
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
import os,sys
import zookeeper
import time
from optparse import OptionParser
connected = False
zh = None
def check_connected(p1,p2,state,p4):
global connected
if state == zookeeper.CONNECTED_STATE:
connected = True
def initialize(zkservers):
global connected
global zh
zh = zookeeper.init(zkservers,check_connected)
while True:
if connected:
break
time.sleep(0.5)
def find(node_name):
global zh
yield node_name
children = zookeeper.get_children(zh,node_name)
for c in children:
for f in find(os.path.join(node_name,c)):
yield f
def find_leaves_first(node_name):
global zh
children = zookeeper.get_children(zh,node_name)
for c in children:
for n in find_leaves_first(os.path.join(node_name,c)):
yield n
yield node_name
def get_node(node_name):
global zh
return zookeeper.get(zh,node_name)
def delete_node(node_name):
global zh
return zookeeper.delete(zh,node_name)
def create_node(node_name,data_str):
global zh
stat, acl = zookeeper.get_acl(zh, '/')
return zookeeper.create(zh,node_name,data_str,acl,0)