-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyZk.py
executable file
·85 lines (77 loc) · 2.74 KB
/
copyZk.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
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
#!/usr/bin/env python
##
# Copyright 2015 Rajan
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from kazoo.client import KazooClient
import sys
import json
import argparse
import ConfigParser
import os
def traverse(srcZk, parent, children, targetZk, targetParent, dryRun):
for child in children:
targetChild = targetParent + "/"+ child
child = parent + "/"+ child
print ("child= "+child+", targetChild= "+targetChild)
try:
data = srcZk.get(child)[0]
if (targetZk.exists(targetChild, None) is not None):
if not (dryRun):
targetZk.set(targetChild, data, -1)
else:
print ("DRY-RUN set data on ="+targetChild)
else:
if not (dryRun):
targetZk.create(targetChild, data, None, False, False, True)
else:
print ("DRY-RUN create path ="+targetChild)
print data
except Exception as e:
print ("No data for zk-node " + child + ", exception = " + repr(e))
try :
nextChildren = srcZk.get_children(child)
if nextChildren:
traverse(srcZk,child,nextChildren,targetZk,targetChild,dryRun)
else:
print ("completed zk-copy " + child)
except Exception as e:
print ("No children for zk-node "+ child + ", exception = " + repr(e))
def main(args):
sourceZkPath = args.sourceZkPath
targetZkPath = args.targetZkPath
dryRun = args.dry_run
srcString = sourceZkPath.split("#");
trgString = targetZkPath.split("#");
srcZkStr = srcString[0]
srcZkPath = srcString[1]
trgZkStr = trgString[0]
trgZkPath = trgString[1]
result = not (dryRun)
print result
print (srcZkStr + "," + srcZkPath)
# traverse all children of path
srcZk = KazooClient(hosts=srcZkStr)
srcZk.start()
targetZk = KazooClient(hosts=trgZkStr)
targetZk.start()
children = srcZk.get_children(srcZkPath)
traverse(srcZk, srcZkPath, children, targetZk, trgZkPath, dryRun)
if __name__ in '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--dry-run", "-r", action='store_true', default=False, help="Only prints update cluster data, does not update the zk")
parser.add_argument("--sourceZkPath", "-szkp", required=True, help="Source ZooKeeperServer:port#path")
parser.add_argument("--targetZkPath", "-tzkp", required=True, help="Target ZooKeeperServer:port#path")
args = parser.parse_args()
main(args)